0

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • hello... it seems as though you have two problems if you say it does one thing when running and also that it doesn't compile... usually when you are in a case like that it is good to pay attention to your compiler warnings/errors, and once you fix those, sometimes things: just work™... – Grady Player Jul 19 '19 at 12:43
  • 1
    @David Bu What is the relation between "why is the if statement does not execute" and "does not compile"? – Vlad from Moscow Jul 19 '19 at 12:44
  • 1
    You cannot `return 0;` from `void main()`. You should use `int main(`void`)`. – Jonathan Leffler Jul 19 '19 at 12:48
  • @VladfromMoscow Well, "compiles" **is** a prerequisite for "executes". ;-) – Andrew Henle Jul 19 '19 at 12:50
  • @AndrewHenle Oh, thanks!:) I will know now.:) – Vlad from Moscow Jul 19 '19 at 12:51
  • 1
    When you call `separate`, `newarr1` and `newarr2` won't be modified on the caller side. This is a classic one, read carefully [this SO article](https://stackoverflow.com/q/766893/898348) – Jabberwocky Jul 19 '19 at 13:08

3 Answers3

3

Turn on warnings! You're trying to use a '==' for assignment - should be '='.

gcc -std=c99 -Wall    omg.c   -o omg
omg.c: In function 'main':
omg.c:32:5: warning: implicit declaration of function 'system' [-Wimplicit-function-declaration]
     system("pause");
     ^
omg.c: In function 'separate':
omg.c:55:9: warning: statement with no effect [-Wunused-value]
         arr1[j] == arr[j];
         ^
omg.c:61:13: warning: statement with no effect [-Wunused-value]
             arr2[j]== arr[j];
             ^
user234461
  • 1,133
  • 12
  • 29
  • 1
    yup, that is what my compilerizer says too. – Grady Player Jul 19 '19 at 12:47
  • No explanation necessary - it speaks for itself, and to the fact that OP clearly didn't have warnings turned on. Even a beginner, with a bit of initiative, should be able to look up the difference between `==` and `=`. – user234461 Jul 19 '19 at 12:48
1

This is wrong

for (j = 0; j < n; j++)
{
    if ((arr[j]%2) == 0)
    arr1[j] == arr[j];
}

Imagine j being the last one (n - 1). You will try to set arr1[n - 1] to whatever, but size of arr1 is size1 not n.

pmg
  • 106,608
  • 13
  • 126
  • 198
1

As others pointed out you are using == to try to assign values.

Your array is going out of bounds because you allocated only enough memory in your other arrays to hold the amount of even/odd numbers in the array that is being sorted. I left comments for you. Idk what compiler or ide your using but I got this working on Visual Studio, with some other changes to the code. I am also a student!

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));

    // Your assigning only enough space to hold the amount of even/odd numbers
    arr1 = (int*)calloc((*size1), sizeof(int));
    arr2 = (int*)calloc((*size2), sizeof(int));

    // If the index of the array is larger than what you allocated, crash..
    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]; // Use = to assign, not ==
    }

    return;
}
RobotMan
  • 605
  • 1
  • 5
  • 10