C arrays problem

Asked

Viewed 37 times

-2

Good afternoon, basically I am trying to make a program that will ask the user to enter 10 numbers between 1 and 100 and with a menu in which the menu will have 5 options at this time I am in option 3 where I wanted when I clicked on option 3 the program would take vector 2 and present-if the odd numbers and then the even numbers, but for some reason he is entering Random values when I will present the numbers.

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

main(){

int i;
int b;
int c;
int d;
int e;
int vetor1[10];
int vetor2[10];
int vetor3[10];
int vetorpar[10];
int vetorimpar[10];
int soma = 0;
int perg = 1;
int media;


setlocale(LC_ALL,"");

for(i = 0 ; i < 10 ; i++){ //introdução de 10 valores
     printf("\nEscreva um numero de 1 a 100 : ");
     scanf("%i", &vetor1[i]);
      }

for(b = 0; b < 10 ; b++){ //Armazenar o vetor1 no vetor2 e ver se são impares ou pares os numeros
    vetor2[b] = vetor1[b];

    if(vetor2[b] % 2 != 0)
    {
        vetorimpar[b] = vetor2[b];
    }
    else{
        vetorpar[b] = vetor2[b];
    }

       }



 while(perg != 0)
{
    printf("\n---------------------------------------------------------");

    printf("\n 0 - Sair\n 1 - Soma\n 2 - Inversa do Vetor\n 3 - Numeros impares e Pares\n 4 - Arroz\n 5 - Rand\n Digite a opção desejada :  ");
    scanf("%i", &perg);
    printf("\n---------------------------------------------------------");


               switch (perg){

          case 0:
            printf("\n Obrigado pela sua escolha, volte sempre.\n\n");
            system("pause");
            break;

        case 1:

            soma = vetor1[0] + vetor1[1] + vetor1[2] + vetor1[3] + vetor1[4] + vetor1[5] + vetor1[6] + vetor1[7] + vetor1[8] + vetor1[9]; //soma de todos os valores do vetor1
            media = soma / 10; //media da soma dos valores do vetor1
            printf("\n A media é : %i", media);
            break;


        case 2:
            for(c = 9; c >= 0 ; c--){
            printf("\nOrdem inversa : %i", vetor2[c]); //inverso dos valores do vetor1
        }
            break;

        case 3:
             for(d = 0 ; d < 10 ; d++){
                    if(vetorpar[d] != NULL){
                         printf("\nval par : %i", vetorpar[d]); //apresentação dos valores pares do vetor2
                    }

                    else{
                        printf("\nval par : %i", vetorimpar[d]); //apresentação dos valores impares do vetor2
                    }

                      }

        break;

        case 4:
            printf("");
            break;

        case 5:
            printf("");
            break;

        default :
            printf("");

    }

 }

}
  • Show code and say it has problems without specifying what problems are these usually result in stackoverflow team alert.

  • tries to formulate your question a little better, it’s really complicated to interpret what you have as doubt.

  • As the even and odd quantity can be different use different indexes for vetorimpar and vetorpar starting from 0 and only increasing when finding an odd or a pair.

  • OK thanks for the help but I’ve managed to do it myself, maybe I misspelled yes, but the interpretation of the code is not so complicated basically the code will ask the user numbers between 1 and 100 and the user will enter these numbers 10 times after the code will provide 5 options and the user will have to choose one of the options 1 option makes the media the second does vector 2 inversion and the third was supposed to write the odd numbers first and then the pairs and what was happening I was trying to put even numbers in another array and odd numbers in another array

  • and then I just wanted to present with a cycle for the two arrays but I wasn’t giving because the array was assuming Random values for some reason and was displaying numbers that were not supposed to appear or if the even numbers were 2 4 6 8 10 it also presented numbers Random and it was 31231231, 2 , 2898328432, 4, 28927323 ,8, 12318231, 10

  • basically what I changed was the cycles so it looks like this :

Show 1 more comment

1 answer

0


Then the right code goes like this :

*Codigo Errado : *

for(b = 0; b < 10 ; b++){ //Armazenar o vetor1 no vetor2 e ver se são impares ou pares os numeros
vetor2[b] = vetor1[b];

if(vetor2[b] % 2 != 0)
{
    vetorimpar[b] = vetor2[b];
}
else{
    vetorpar[b] = vetor2[b];
}
   }
             

*Certain code : *

for(b = 0; b < 10 ; b++){ //Armazenar o vetor1 no vetor2
    vetor2[b] = vetor1[b];
}

*Part of Case 3 that was wrong : *

 case 3:
         for(d = 0 ; d < 10 ; d++){
                if(vetorpar[d] != NULL){
                     printf("\nval par : %i", vetorpar[d]); //apresentação dos valores pares do vetor2
                }

                else{
                    printf("\nval par : %i", vetorimpar[d]); //apresentação dos valores impares do vetor2
                }
                     }

*Code Corrected : *

 case 3:
             for(d = 0 ; d < 10 ; d++){
                    if(vetor2[d] % 2 != 0 )
                    {
                       printf("\n Numeros Impares : %i", vetor2[d]);
                    }

                    }

              for(d = 0 ; d < 10 ; d++){
                    if(vetor2[d] % 2 == 0 )
                    {
                       printf("\n Numeros Pares : %i", vetor2[d]);

                    }

                    }
                       break;

Do not forget to remove even and odd vectors from the code above that no need!

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • What I suggested, which allows until its input is 0, was: int qtpar=0, qtimpar=0; for(b = 0; b < 10 ; b++){ vector 2[b] = vector 1[b]; if(vector 2[b] %2 != 0) { odd vector[qtimpar++] = vector 2[b]; } Else { vector[qtpar++] = vector 2[b]; } } printf(" n%d odd n", qty); for (i=0; i<qtdimpar; i++) { printf(" t%d", odd vector[i]); } printf(" n%d pairs n", qtimpar); for (i=0; i<qtdpar; i++) { printf(" t%d", vector[i]); }

  • OK thank you very much thank you :))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.