I am trying to do my homework but I got stuck. They want me to take one array which is already given and separate it into two arrays, one of them holds the even numbers and the other holds the odd numbers. I wrote a void function that receives 6 parameters as I will show below. The if statement: (if ((arr[j]%2) == 0)
) in the function does not get executed for some reason. It just skips it. I don't really understand why and I'd appreciate any assistance.
Tried debugging, using different syntax for the pointers Arr1 and Arr2.
#include <stdio.h>
#include <malloc.h>
void separate(int* arr, int n, int* size1, int* size2, int* arr1, int* arr2);
int main()
{
int size1=0, size2=0;
int* newArr1 = NULL;
int* newArr2 = NULL;
int arr[] = { 6,57,14,21,11,3,22,42,9,15 };
printf("The array before change:\n");
for (int i = 0; i <10; i++)
{
printf(" %d", arr[i]);
}
printf("\n");
separate(arr, 10, &size1, &size2, newArr1, newArr2);
printf("The even array is:\n");
for (int i = 0; i <size1; i++)
{
printf(" %d", newArr1[i]);
}
printf("\n");
printf("The odd array is:\n");
for (int i = 0; i <size2; i++)
{
printf(" %d", newArr2[i]);
}
printf("\n");
system("pause");
return 0;
}
void separate(int* arr, int n, int* size1, int* size2, int* arr1, int* arr2)
{
int i, j;
for (i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
(*size1)++;
else
(*size2)++;
}
printf("\n");
printf("size1: %d size2: %d", (*size1),(*size2));
arr1 = (int*)calloc((*size1), sizeof(int));
arr2 = (int*)calloc((*size2), sizeof(int));
for (j = 0; j < n; j++)
{
if ((arr[j]%2) == 0)
arr1[j] == arr[j];
}
for (j = 0; j < n; j++)
{
if (arr[j] % 2 != 0)
arr2[j]== arr[j];
}
return;
}
Does not compile