# Splitting odd and even number for given range using recursion

–1 vote
When i run this program, it executes. However, the value of a is not stored in my array instead all it gives is 0

#include <stdio.h>
#define MAX 200

int a,b,x;
void sort(int a, int b);

int main(void)
{
printf("Smaller Number: ");
scanf("%d", &a);

printf("Bigger Number: ");
scanf("%d", &b);

sort(a,b);

}

void sort(int a, int b)
{
int even[MAX], odd[MAX];
int static i,j;

if(b < a)
{
printf("The even numbers are : ");
for(int k = 0; k < i; k++)
{
printf("%d ", even[k]);
}

printf("\n The odd numbers are : ");
for(int l = 0; l < j; l++)
{
printf("%d ", odd[l]);
}

return;
}

if(a % 2 == 0)
{
even[i] = a;
i++;
sort(++a,b);
}
else
{
odd[j] = a;
j++;
sort(++a,b);
}

answered Mar 13, 2021 by (9,400 points)
Didnt get the logic of sorting, though i see uninitialised static variables i& j in code.

assigning them proper values would run the code.

Also arrays even,odd who populates them ??
commented Mar 13, 2021 by (140 points)
dude is just a noob who decided to call his function "sort" since it sorts the values between a and b into arrays by even or odd parity... lol
answered Mar 13, 2021 by (140 points)
TLDR: just stick the word "static" in front of "int even[MAX], odd[MAX];" and problem solved!

ya see how in your "void sort" function you've locally declared the integers i and j with the storage class specifier called "static" ... this means when you go down into another recursive call (or return from one) the values for i and j will still be there in memory upon return and even after another function call...

but since your integer arrays for even and odd are only declared with the type specifier of "integer pointer" the storage class specifier that the compiler will classify those with will be "auto"...

automatic variables only exist within the blocks in which they are defined, and upon entry or exit (this includes the function calling and returning to itself), the storage space in memory allocated for them is deallocated (or "freed") and then possibly written over depending on what the current activation frame and stack pointer are doing to the stack...

TLDR: just stick the word "static" in front of "int even[MAX], odd[MAX];" and problem solved!

also; some other issues:

you're missing a '}' at the end of your code (possible copy/paste error)

you've declared integers a, b, x outside of the main function scope as global variables when it is sufficient to place them at the top of main as locals since they are not needed in any other scope other than to pass (by value NOT by reference) a and b along to the recursive sort function

declare variables i and j as "static int i,j;" since its just good programming practice to order your type specifiers by integral promotion rules... speaking of which it is part of the ansi-C standard that declarations with the form:

"signed a = -2;" or "long b = 9e22;" or "static c = 1;" the compiler will infer the "int" token such that the previous three declarations are equivalent to: "signed int a = -2;" or "long int b = 9e22;" or "static int c = 1;"

...

hope this helps... :)