How to add elements from one array into another array of an undefined size based on a condition?

Viewed 116

I have been teaching myself C for just a few weeks, and am attempting to write a code that enables the user to decide the size and elements in an array which is then separated into two arrays - one for odd numbers, and one for even numbers.

I am pretty sure that dynamic allocation has something to do with this, but I am unsure of how to implement it. Here is the code so far:

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

int main()
{
  //User decides the size of the array of numbers-------------------------------
  int n;
  printf("How many numbers? ");
  scanf("%d",&n);

  //User inputs values into array the size of array[n]--------------------------
  int i;
  int array[n];
  printf("What are the numbers?\n");
  for(i=0; i<n; i++)
  {
    scanf("%d",&array[i]);
  }

  //loop goes through array, separates even and odds into 2 new arrays----------
  //use dynamic allocation??

  for(i=0;i<n;i++)
  {
    int *evenarray = malloc(sizeof(evenarray)); //not sure if this is setup correctly
    int *oddarray = malloc(sizeof(oddarray)); //not sure if this is setup correctly

    if(array[i] % 2 == 0) //if value in array CAN be divided by 2
    {
      printf("Test statement.\n");
    }
    else //if this is not true, append to odd array
    {
      printf("Second test statement.\n");
    }
  }
}

/*this program accepts a user chosen number of numbers
  then, the program separates the odd and even numbers into
  two different arrays*/
2 Answers

There is no magical way to get this information at one shot. You can however, try either of the below:

  • Loop over the first array to figure out the count of odd (or even) numbers, then, you know the count of elements for which memory has to be allocated, and you can use either a VLA (as the first array itself) or use a pointer and allocator functions to allocate memory.

    --> However, in this process, you have to perform the odd/even check twice - once to count the occurrence of odd/even numbers and next time, to actually decide and copy them to the new locations.

  • Allocate two chunks of memory similar to the first array size, and start filling the odd and even elements into the new memory, respectively. After all the elements are stored, take the counts, realloc() the allocated memories to the exact size.

    --> In this case, the pre-allocation is to be done, but the odd/even check needs to be carried out only once.

You could copy into the odd/even arrays and keep seperate counters to track it. i.e:

  //loop goes through array, separates even and odds into 2 new arrays----------
  //use dynamic allocation??

  int evencount =0;
  int oddcount =0;
  int *evenarray = malloc(sizeof(evenarray)); //not sure if this is setup correctly
  int *oddarray = malloc(sizeof(oddarray)); //not sure if this is setup correctly
  for(i=0;i<n;i++)
  {
    if(array[i] % 2 == 0) //if value in array CAN be divided by 2
    {
      printf("Printing to even array.\n");
      evenarray[evencount] = array[i];
      evencount++;
    }
    else //if this is not true, append to odd array
    {
      printf("Printing to odd array.\n");
      oddarray[oddcount] = array[i];
      oddcount++;
    }
  }

  printf("evenarray = ");
  for(i=0;i<evencount;i++){
    printf("%d, ", evenarray[i]);
  }
  printf("\n");
  printf("oddarray = ");
  for(i=0;i<oddcount;i++){
    printf("%d, ", oddarray[i]);
  }
  printf("\n");
Related