Even-odd vector fill C

Asked

Viewed 1,158 times

-2

Good people are caught in a little problem here I will put the statement here: "In this problem you should read 15 values put them in 2 vectors as these values are even or odd. However, the size of each of the two vectors is 5 positions. Then, each time one of the two vectors fills in, you must print out the whole vector and use it again for the next numbers that are read. After reading, print the remaining contents on each of the two vectors, first printing the values of the odd vector. Each vector can be filled as many times as necessary." The final part of the vector reset to be filled and what’s complicating my code is so at the moment:

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

int main(){
    int par[5];
    int impar[5];
    int n,i;
    while(i==0){
        par[i]=0;
        impar[i]=0;
        for(i=0;i<15;i++){
            scanf("%d",&n);
            if(n%2==0){
                for(i=0;i<5;i++){
                    par[i]=n;
                    printf("par[%d] = %d\n",i,par[i]);
               }
            }else{
                for(i=0;i<5;i++){
                    impar[i]=n;
                    printf("impar[%d] = %d\n",i,impar[i]);
                }
            }

        }

    }
    return 0;
}

3 answers

0

You need to modularize your problem in different functions to not lose the thread of the skein. To initialize the vector, just use Designated Initializers from C99 to fill the vectors with starting value 0 at all positions or iterate by the vector setting the value manually. Your problem seems pretty trivial, so I took the liberty of offering a solution by prioritizing simple structures. Make your adaptations if you feel it necessary:

#include <stdio.h>

const int VET_SIZE = 15;
const int PARIMP_SIZE = 5;

void show (int* vect) { 
 for (int i = 0; i < PARIMP_SIZE; i++) {
        printf("%d\t", vect[i]);
    }
  puts("");
}

void reset (int* vect) {
 for(int i = 0; i < PARIMP_SIZE; i++) {
       vect[i] = 0;
    }
}

int main(void) {

   int PAR_N[PARIMP_SIZE]; 
   int IMPAR_N[PARIMP_SIZE];
   int current, PAR_POS  = 0, IMPAR_POS = 0;

   reset(PAR_N);
   reset(IMPAR_N);

  for (int i = 0; i < VET_SIZE; i++)  {
    printf ("%d - Entre com um valor: ", (i+1));
    scanf(" %d",&current);

    if (current % 2 == 0) {

       if (PAR_POS < 5) { 
        PAR_N[PAR_POS] = current;
        PAR_POS++;
       } else {
        PAR_POS = 0;
        PAR_N[PAR_POS] = current;
        puts("Vetor PAR - Cheio");
        show(PAR_N);
        reset(PAR_N);
      }

    } else {

      if (IMPAR_POS < 5) {
        IMPAR_N[IMPAR_POS] = current;
        IMPAR_POS++;
      } else { 
        IMPAR_POS = 0;
        IMPAR_N[IMPAR_POS] = current;
        puts("Vetor IMPAR - Cheio");
        show(IMPAR_N);
        reset(IMPAR_N);
     }

    }
  }

    puts("Vetor IMPAR");
    show(IMPAR_N);
    puts("Vetor PAR");
    show(PAR_N);

    return 0;
}

The function show() just scan the array and print the elements.

The function reset() just reset the elements of a vector.

main() uses two auxiliary integers to index the respective vectors to carry out the counting as the vectors are filled. When one of them reaches the expected amount the vector is displayed on the screen, its content is set to zero and the integer corresponding to the vector goes back to zero. At the end of the program, it displays both vectors in the current state.

  • Calm, calm, you’re too advanced for this, my

0

I managed to solve this one the right code:

#include <stdio.h>
int par[5], j, impar[5], i, P = 0, I = 0, x;

void main(void) {
    for (i = 0; i < 15; i++) {
        scanf("%d", &x);
        if (x % 2 == 0) {
            par[P] = x;
            P++;
            if (P == 5) {
                for (j = 0; j < 5; j++) {
                    printf("par[%d] = %d\n", j, par[j]);
                }
                P = 0;
            }
        } else {
            impar[I] = x;
            I++;
            if (I == 5) {
                for (j = 0; j < 5; j++) {
                    printf("impar[%d] = %d\n", j, impar[j]);
                }
                I = 0;
            }
        }
    }
    for (j = 0; j < I; j++) {
        printf("impar[%d] = %d\n", j, impar[j]);
    }
    for (j = 0; j < P; j++) {
        printf("par[%d] = %d\n", j, par[j]);
    }
}

0

The use of the % (module) operator can facilitate the control of when each vector was full and therefore must be printed, keeping the count of all numbers, even or odd, already found.

#include <stdio.h>
int main() {
    int par[5], impar[5], qtd_par=0, qtd_impar=0, i, j, x;
    for (i = 0; i < 15; i++) {
        scanf("%d", &x);
        if (x % 2 == 0) {
            par[qtd_par%5] = x;
            qtd_par++;
            if (qtd_par%5 == 0) {
                for (j = 0; j < 5; j++) {
                    printf("par[%d] = %d\t", j, par[j]);
                }
                printf("\n");
            }
        } else {
            impar[qtd_impar%5] = x;
            qtd_impar++;
            if (qtd_impar%5 == 0) {
                for (j = 0; j < 5; j++) {
                    printf("impar[%d] = %d\t", j, impar[j]);
                }
                printf("\n");
            }
        }
    }
    for (j = 0; j < qtd_impar%5; j++) {
        printf("impar[%d] = %d\t", j, impar[j]);
    }
    printf("\nQuantidade total de ímpares: %d\n\n", qtd_impar);
    for (j = 0; j < qtd_par%5; j++) {
        printf("par[%d] = %d\n", j, par[j]);
    }
    printf("\nQuantidade total de pares: %d\n", qtd_par);
    return 0;
}

Browser other questions tagged

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