Problem in language C, question with prime numbers

2

I'm solving exercises, in C language, on vectors. The exercise is as follows: Make an algorithm that uses a menu with the following options: order a vector of 5 increasing positions, order a vector of 5 decreasing positions and store in a vector 2, the prime numbers. The problem occurs when the third menu option is selected, which is to store the prime numbers in a second vector. I wanted you to take a look at my 'case 3' switch and point me the error (s). Thank you in advance.

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

int main(){
setlocale(LC_ALL,"portuguese");
int op, vetor[5], i, x, y, w=0, aux, cont=0, primos[5]; 

    do{

        printf("\n      Menu    \n");
        printf("0. Sair. \n");
        printf("1. Ordenar de forma crescente. \n");
        printf("2. Ordenar de forma decrescente. \n");
        printf("3. Armazenar os números primos. \n");

        printf("Digite a opção desejada: ");
        scanf("%d", &op);

        if(op==0){

            break;

        }

        system("cls");

        switch(op){

            case 1: printf("Você escolheu ordenar de forma crescente. \n");

                    for(i=0; i<5; i++){

                        printf("Digite o elemento [%d]: ", i);
                        scanf("%d", &vetor[i]);

                    }

                    for(i=0; i<5; i++){
                        for(x=0; x<=5; x++){

                            if(vetor[i]<vetor[x]){

                                aux = vetor[i];
                                vetor[i] = vetor[x]; 
                                vetor[x] = aux;

                            }                               
                        }                           
                    }

                    for(i=0; i<5; i++){
                        printf("\nv[%d] = %d\n", i, vetor[i]); 
                    }

                    break;

            case 2: printf("Você escolheu ordenar de forma decrescente. \n");

                    for(i=0; i<5; i++){

                        printf("Digite o elemento [%d]: ", i);
                        scanf("%d", &vetor[i]);

                    }

                    for(i=0; i<5; i++){
                        for(x=0; x<=4; x++){

                            if(vetor[x]<vetor[i]){

                                aux = vetor[x];
                                vetor[x] = vetor[i]; 
                                vetor[i] = aux;

                            }                               
                        }                           
                    }

                    for(i=0; i<5; i++){
                        printf("\nv[%d] = %d\n", i, vetor[i]); 
                    }

                    break;

            case 3: printf("Você escolheu armazenar os números primos. \n");

                    for(i=0; i<5; i++){

                        printf("Digite o elemento [%d]: ", i);
                        scanf("%d", &vetor[i]);

                    }

                    for(i=0; i<5; i++){
                        for(y=1; y<=vetor[i]; i++){

                            if(vetor[i]%y==0){
                                cont++;
                            }
                            w=0;
                            if(cont==2){

                                primos[w] = vetor[i];
                                w++; 

                            }


                        }

                        for(i=0; i<5; i++){

                            printf("primo[%d] = %d\n", i, primos[i]);

                        }

                    break;



                    }

        }

        }while(op!=0);  

system("pause");
return 0;
}
    
asked by anonymous 01.08.2016 / 00:32

1 answer

4

Well, first of all I recommend you learn how to indent your code properly. Another point is that you exaggerate in the use of blank lines.

But for errors, the first one is in case 1 :

for(x=0; x<=5; x++){

There, the <=5 should be < 5 or <= 4 .

Now, let's take a look at your case 3 . I have already arranged in the excess of blank lines and arranged the indentation to understand better:

printf("Você escolheu armazenar os números primos. \n");

for (i = 0; i < 5; i++) {
    printf("Digite o elemento [%d]: ", i);
    scanf("%d", &vetor[i]);
}

for (i = 0; i < 5; i++) {
    for (y = 1; y <= vetor[i]; i++) {
        if (vetor[i] % y == 0) {
            cont++;
        }
        w = 0;
        if (cont == 2) {
            primos[w] = vetor[i];
            w++; 
        }
    }

    for (i = 0; i < 5; i++) {
        printf("primo[%d] = %d\n", i, primos[i]);
    }
    break;
}

And look at what the correct indentation told us: You have a loop iterating in i within another loop iterating also in i . Also, the fact that break is within for when it should be breaking switch instead of for shows that there is something very wrong about it. The lesson learned here is to avoid excess blank lines and to indent correctly, as these two simple steps were enough to make this problem obvious.

Okay, let's fix this. The first loop containing scanf s is correct. The second loop is closing very late, since it should contain only the inner loop that iterates in y and nothing else. So, your code for now looks like this:

printf("Você escolheu armazenar os números primos. \n");

for (i = 0; i < 5; i++) {
    printf("Digite o elemento [%d]: ", i);
    scanf("%d", &vetor[i]);
}

for (i = 0; i < 5; i++) {
    for (y = 1; y <= vetor[i]; i++) {
        if (vetor[i] % y == 0) {
            cont++;
        }
        w = 0;
        if (cont == 2) {
            primos[w] = vetor[i];
            w++; 
        }
    }
}

for (i = 0; i < 5; i++) {
    printf("primo[%d] = %d\n", i, primos[i]);
}
break;

Another problem is w . The purpose of w is to mark how many primes you found. But the inner loop (that of y ) checks for every possibility of division and for each one it returns w to zero again, which causes it to always get stuck at zero at the end. In fact, w = 0; should be before this loop.

Incidentally, looking at the inner loop, you initialize y = 0 , check the stop condition with y <= vetor[i] , but increment with i++ instead of y++ . Obviously, the correct one is to increment with y++ .

Looking at if , its purpose is to record that a prime was found only if exactly two divisors were found in the y loop. However, this means that it should be after the y loop, not inside.

The last for traverses the array with the cousins found. How do you know how many cousins were found? Well, that's the purpose of w . This means that the last for should iterate through w array elements, not 5.

And finally, the variable cont should have its value reset to zero at each iteration of the intermediate loop that discovers if the number is prime. After all, if this value is not reset, it accumulates from one number to another and between different runs of case 3 and this is meaningless. So it has to be set to zero at the beginning of the intermediate loop.

So here's how your corrected code gets:

printf("Você escolheu armazenar os números primos. \n");

for (i = 0; i < 5; i++) {
    printf("Digite o elemento [%d]: ", i);
    scanf("%d", &vetor[i]);
}

w = 0;
for (i = 0; i < 5; i++) {
    cont = 0;
    for (y = 1; y <= vetor[i]; y++) {
        if (vetor[i] % y == 0) {
            cont++;
        }
    }
    if (cont == 2) {
        primos[w] = vetor[i];
        w++; 
    }
}

for (i = 0; i < w; i++) {
    printf("primo[%d] = %d\n", i, primos[i]);
}
break;
    
01.08.2016 / 02:26