你可以用以下代码在两个数组中找到匹配的值并将它们打印出来:

huangapple go评论65阅读模式
英文:

How can I find matching values in two arrays and print then?

问题

Here's the translated code as requested:

#include <stdio.h>
#include <locale.h>

int main()
{
    setlocale(LC_ALL, "Portuguese");
    int vetor[10], vetor2[10], vetor3[10];
    int i, j = 0;

    printf("Digite 10 números inteiros e maiores que zero:\n");

    for (i = 0; i < 10; i++) {
        {
            printf("Número %d: ", i + 1);
            scanf("%d", &vetor[i]);

        }
        while (vetor[i] <= 0);
    }

    printf("\n\nDigite mais 10 números inteiros e maiores que zero:\n");

    for (i = 0; i < 10; i++) {
        {
            printf("Número %d: ", i + 1);
            scanf("%d", &vetor2[i]);

        }
        while (vetor2[i] <= 0)
            ;
    }

    printf("\nValores digitados no vetor 1:\n");
    for (i = 0; i < 10; i++) {
        printf("\n%d ", vetor[i]);
    }

    printf("\n\nValores digitados no vetor 2:\n");
    for (i = 0; i < 10; i++) {
        printf("\n%d ", vetor2[i]);
    }

    for (i = 0; i < 10; i++) {
        if (vetor[i] == vetor2[i]) {
            vetor3[j] = vetor[i];
            j++;
        }
    }

    printf("\n\nValores que fazem parte nos dois vetores:\n");
    for (i = 0; i < j; i++) {
        printf("\n%d ", vetor3[i]);
    }

    return 0;
}

Please note that I've translated the comments and code parts as requested. If you have any further questions or need additional assistance, feel free to ask.

英文:

How can I find matching values in two arrays and print then?

I need to make a program in c that the user should enter 10 integers greater than zero and store them in an array. Then the user must enter 10 more integer numerical values greater than zero and store them in another array. The program should display all the values that are part of both arrays, but I can't find a way to do this without making modular code, and I don't want to do this.
Can someone help me?

I tried to use this code but it didn't work

#include &lt;stdio.h&gt;
#include &lt;locale.h&gt;

int main() {	
	setlocale(LC_ALL, &quot;Portuguese&quot;);
    int vetor[10], vetor2[10], vetor3[10];
    int i, j , k = 0;

    printf(&quot;Digite 10 n&#250;meros inteiros e maiores que zero:\n&quot;);

    for (i = 0; i &lt; 10; i++) {
	{   	
        printf(&quot;N&#250;mero %d: &quot;, i + 1);
        scanf(&quot;%d&quot;, &amp;vetor[i]);

    }while (vetor[i] &lt;=0 );
	}	

	printf(&quot;\n\nDigite mais 10 n&#250;meros inteiros e maiores que zero:\n&quot;);

    for (i = 0; i &lt; 10; i++) {
	{   	
        printf(&quot;N&#250;mero %d: &quot;, i + 1);
        scanf(&quot;%d&quot;, &amp;vetor2[i]);

    }while (vetor2[i] &lt;=0 );
	}	
	
    printf(&quot;\nValores digitados no vetor 1:\n&quot;);
    for (i = 0; i &lt; 10; i++) {
        printf(&quot;\n%d &quot;, vetor[i]);
    }

    printf(&quot;\n\nValores digitados no vetor 2:\n&quot;);
    for (i = 0; i &lt; 10; i++) {
        printf(&quot;\n%d &quot;, vetor2[i]);
    }
    
	if (vetor[i] == vetor2[i]) {
    		vetor3[j] = vetor[1];
    		j++;
    for (i = 0; i &lt; j; i++) {
    	printf(&quot;\n\nValores que fazem parte nos dois vetores:\n&quot;);
        printf(&quot;\n%d &quot;, vetor3[j]);
    }
	}

    return 0;
}

@pmacfarlane i make some changes in the code, i don't know if this is what you want, because i didnt understand correctly what you want.

#include &lt;stdio.h&gt;
#include &lt;locale.h&gt;

int main()
{
setlocale(LC_ALL, &quot;Portuguese&quot;);
int vetor[10], vetor2[10], vetor3[10];
int i, j = 0;

printf(&quot;Enter 10 integers numbers greater than zero:\n&quot;);

for (i = 0; i &lt; 10; i++) {
    {
        printf(&quot;Number %d: &quot;, i + 1);
        scanf(&quot;%d&quot;, &amp;vetor[i]);

    }
    while (vetor[i] &lt;= 0);
}

printf(&quot;\n\nEnter 10 more integers numbers greater than zero:\n&quot;);

for (i = 0; i &lt; 10; i++) {
    {
        printf(&quot;N&#250;mero %d: &quot;, i + 1);
        scanf(&quot;%d&quot;, &amp;vetor2[i]);

    }
    while (vetor2[i] &lt;= 0)
        ;
}

printf(&quot;\nValues entered in vector 1:\n&quot;);
for (i = 0; i &lt; 10; i++) {
    printf(&quot;\n%d &quot;, vetor[i]);
}

printf(&quot;\n\nValues entered in vector 2:\n&quot;);
for (i = 0; i &lt; 10; i++) {
    printf(&quot;\n%d &quot;, vetor2[i]);
}


if (vetor[i] == vetor2[i]) {
   
    for (i = 0; i &lt; 10; i++) {
        printf(&quot;\n\nValues that are present in both arrays:\n&quot;);
        printf(&quot;\n%d &quot;, vetor[i]);
    }
}

return 0;
}

答案1

得分: 1

以下是已正确缩进您的代码的结果(我在Linux上使用了indent -kr,然后添加了几个换行符):

#include <stdio.h>
#include <locale.h>

int main()
{
    setlocale(LC_ALL, "Portuguese");
    int vetor[10], vetor2[10], vetor3[10];
    int i, j, k = 0;

    printf("Digite 10 números inteiros e maiores que zero:\n");

    for (i = 0; i < 10; i++) {
        {
            printf("Número %d: ", i + 1);
            scanf("%d", &vetor[i]);
        }

        while (vetor[i] <= 0)
            ;
    }

    printf("\n\nDigite mais 10 números inteiros e maiores que zero:\n");

    for (i = 0; i < 10; i++) {
        {
            printf("Número %d: ", i + 1);
            scanf("%d", &vetor2[i]);
        }

        while (vetor2[i] <= 0)
            ;
    }

    printf("\nValores digitados no vetor 1:\n");
    for (i = 0; i < 10; i++) {
        printf("\n%d ", vetor[i]);
    }

    printf("\n\nValores digitados no vetor 2:\n");
    for (i = 0; i < 10; i++) {
        printf("\n%d ", vetor2[i]);
    }

    if (vetor[i] == vetor2[i]) {
        vetor3[j] = vetor[1];
        j++;
        for (i = 0; i < j; i++) {
            printf("\n\nValores que fazem parte nos dois vetores:\n");
            printf("\n%d ", vetor3[j]);
        }
    }

    return 0;
}

您的第一个两个for循环的主体部分有一个奇怪的额外级别的{}。这些是否应该是do ... while()循环的内部?

你读取数组的两个for循环都会在检测到<= 0的数字时进入无限循环。我怀疑这不是您想要的。

在第二个打印数组的循环之后,您有if (vetor[i] == vetor2[i])。此时,i10,因此您正在越界访问数组。这会引发未定义的行为。

如果未定义的行为恰好导致if评估为真,那么您随后会执行vetor3[j] = vetor[1];。但此时j尚未初始化。这也会引发未定义的行为。我不确定为什么您要使用vetor[1]

然后,您运行一个从0j的循环,打印vetor3[]中的数字,尽管您只可能设置了一个数组成员。

我建议使用一个可以为您格式化代码的编辑器,并使用更多警告启用编译器来运行您的代码。

英文:

Here is the result of properly indenting your code. (I used indent -kr on Linux, then added a few newlines):

#include &lt;stdio.h&gt;
#include &lt;locale.h&gt;

int main()
{
    setlocale(LC_ALL, &quot;Portuguese&quot;);
    int vetor[10], vetor2[10], vetor3[10];
    int i, j, k = 0;

    printf(&quot;Digite 10 n&#250;meros inteiros e maiores que zero:\n&quot;);

    for (i = 0; i &lt; 10; i++) {
        {
            printf(&quot;N&#250;mero %d: &quot;, i + 1);
            scanf(&quot;%d&quot;, &amp;vetor[i]);
        }

        while (vetor[i] &lt;= 0)
            ;
    }

    printf(&quot;\n\nDigite mais 10 n&#250;meros inteiros e maiores que zero:\n&quot;);

    for (i = 0; i &lt; 10; i++) {
        {
            printf(&quot;N&#250;mero %d: &quot;, i + 1);
            scanf(&quot;%d&quot;, &amp;vetor2[i]);
        }

        while (vetor2[i] &lt;= 0)
            ;
    }

    printf(&quot;\nValores digitados no vetor 1:\n&quot;);
    for (i = 0; i &lt; 10; i++) {
        printf(&quot;\n%d &quot;, vetor[i]);
    }

    printf(&quot;\n\nValores digitados no vetor 2:\n&quot;);
    for (i = 0; i &lt; 10; i++) {
        printf(&quot;\n%d &quot;, vetor2[i]);
    }

    if (vetor[i] == vetor2[i]) {
        vetor3[j] = vetor[1];
        j++;
        for (i = 0; i &lt; j; i++) {
            printf(&quot;\n\nValores que fazem parte nos dois vetores:\n&quot;);
            printf(&quot;\n%d &quot;, vetor3[j]);
        }
    }

    return 0;
}

You have a weird extra level of {} in the body of your first two for loops. Are those meant to be do ... while() inside there?

Both of your for loops that read in the arrays will, on detecting a number that is &lt;= 0, enter an infinite loop. I suspect that is not what you want.

After your second loop that prints the arrays, you have if (vetor[i] == vetor2[i]). At this point, i is 10, and so you are indexing your arrays out of bounds. This invokes undefined behaviour.

If the undefined behaviour happens to result in the if evaluating true, you then do vetor3[j] = vetor[1];. But j is not initialised at this point. This also invokes undefined behaviour. I'm not sure why you want vetor[1] either.

You then run a loop from 0 to j, printing numbers from vetor3[], even though you only ever possibly set one member of that array.

I'd recommend using an editor that can format your code for you, and running your compiler with more warnings enabled.

huangapple
  • 本文由 发表于 2023年5月21日 06:02:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定