2
I am solving exercises, in C language, on vectors. The exercise is as follows: Make an algorithm that uses a menu with the following options: sort an ascending 5-position vector, sort a descending 5-position vector, and store the prime numbers in a vector 2. The problem occurs when the third option of the menu is selected, being that of storing the primes in a second vector. I wanted you to take a look at my 'case 3' switch and point out 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;
}
But what’s the problem?
– Pablo Almeida