Recently i was introduced into the concept of "Recursion" and it seemed fun untill i came upon an array in which i was asked to get an array from a user with it's size and Get the negative numbers and the odd numbers Count in the array , i have thought of some ways to do this but none worked , i tried making it with different conditions or loops but everytime i found myself resettings something either negative counter or odd counter or just an infinite loop , i can't seem to understand how passing an array through this recursive procedure works for some reason it keeps giving me wrong outputs so i resetted it and started all over with the base case , Is there like a general way to follow when making recursive functions , i know its about making your problem into smaller sub problems and creating the base case but i wasn't able to figure it out here , if anyone could guide me through it would be appreciate .
Down below is the code i made for getting the array and passing it to the recursive function with the conditions that worked with the iterative function , and 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 , but that didn't seem to work either , any ideas ?
Thanks in advance !
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int SortArray(int* Array, int Size, int* Negative, int* Odd)
{
if (Size < 0)
{
return;
}
if (Array[Size] % 2 == 1 || Array[Size] % 2 == -1)
{
return SortArray(*Array, Size - 1, *Negative, *Odd);
}
if (Array[Size] < 0)
{
}
}
int main()
{
int Size;
int Negative = 0;
int Odd = 0;
printf("Enter The Size Of The Array: \n");
scanf("%d", &Size);
int* Array = malloc(Size * sizeof(int));
if (Array == NULL)
{
printf("Malloc Failure ! \n");
return 0;
}
printf("Enter The Elements Of Your Array: \n");
for (int i = 0; i < Size; i++)
{
scanf("%d", &Array[i]);
}
SortArray(Array, Size,&Negative,&Odd);
printf("Odd Count:%d \n", Odd);
printf("Negative Count:%d \n", Negative);
free(Array);
return 0;
}