tried putting the base case as if (Count < 0) because Count is the
size of the array so i thought about starting with it and decreasing
it by one everytime i want to call the function
this is a good start and the function parameters are the right ones for what you want to accomplish. the name SortArray
is a bit misleading, you are not sorting anything, you are counting, I think Count
would be more appropriate. also Size
is not really the size, but rather our "index":
void Count(int *Array, int Index, int *Negative, int *Odd)
the base case is right, we are scanning the array backwards, so when Index
is less than 0
there is nothing more left.
now, you want to update both your counters before calling the Count
function again. the update part is missing from your code, that is why you get wrong results.
I would do it this way:
int n = Array[Index]; /* current number */
if (n % 2 != 0) /* is it odd? */
*Odd += 1; /* if so, update odd counter by one */
if (n < 0) /* is it negative? */
*Negative += 1; /* if so, update negative counter by one */
at the end we call the Count
function again decreasing Index
by one, as you already guessed:
Count(Array, Index - 1, Negative, Odd);
putting all together:
void Count(int *Array, int Index, int *Negative, int *Odd)
{
if (Index < 0)
return;
int n = Array[Index];
if (n % 2 != 0)
*Odd += 1;
if (n < 0)
*Negative += 1;
Count(Array, Index - 1, Negative, Odd);
}
the function should be called the first time with the last index and counters should be set to 0
(as you already did). from main()
the first call would be:
int Negative = 0;
int Odd = 0;
Count(Array, Size - 1, &Negative, &Odd);
you could make a wrapper function to "hide" this (important) particulars.