How to divide the even and odd number to their own array?

Im learning array right now
the output display the numbers given
but the memory put their own output to the data
that have no input


#include<stdio.h>
void main (void)
{
	 int oddNum[5];
	 int evenNum[5];
	 int i;
	 int n;


	 for(i=0;i<5;i++)
	 {
		printf("Enter Number %d :",i+1);
		scanf("%d",&n);

		if(n%2==0)
		evenNum[i]=n;

		else
		oddNum[i]=n;

	 }

	 printf("EvenNumber\t\OddNumber\n");

	 for(i=0;i<5;i++)
	 {
	  printf("%d\t\%d\n",evenNum[i],oddNum[i]);
	 }


}


Both of your arrays may store different amounts of numbers (if, for example, user enters 4 odds and only 1 even), so you need to remember (in variables) how many elements of either array are used and increment that when you add more.
i don't quite understand.could you give me some example.
wat hamsterman says is that we need to introduce two variables 'od' and 'eve' as shown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  
for(i=0;i<5;i++)
	 {
		printf("Enter Number %d :",i+1);
		scanf("%d",&n);

		if(n%2==0)
		evenNum[i]=n;
eve++;

		else
		oddNum[i]=n;
od++;

	 }

and then run two separate loops to display the odd and even numbers with 'od' and 'eve' giving the number of times this loop should run.
Last edited on
Almost. evenNum[i] should become evenNum[eve], or else it will contain uninitialized elements. Also, you need to wrap code in your if in {} because both lines belong to it. Lastly, the tag is [/code].
i'm using turbo c++ , can those above be used?
Last edited on
Do you have any thoughts why they couldn't? This is perfectly standard C++. Furthermore, this doesn't really add anything you didn't have yet.
i've done what you have suggested but it still havent work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
void main (void)

{
	 int oddNum[5];
	 int evenNum[5];
	 int eve=0,od=0;
	 int i;
	 int n;




for(i=0;i<5;i++)
	 {
		printf("Enter Number %d :",i+1);
		scanf("%d",&n);

		if(n%2==0)
		{
		evenNum[eve]=n;
eve++;
		}
		else
		{
		oddNum[od]=n;
od++;
		}
	 }




	 printf("EvenNumber\t\OddNumber\n");

	 for(i=0;i<5;i++)
	 {
	  printf("%d\t\t\%d\n",evenNum[i],oddNum[i]);
	 }




}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
int main()
{
	 int oddNum[5] = {0,0,0,0,0}; //Set these to zero!
	 int evenNum[5] = {0,0,0,0,0};//Set these to zero! 
	 int i = 0; //In fact, set all integers to 0. 
	 int n = 0;
	 int odd = 0;
	 int even = 0;
	 for(i=0;i<5;++i) //Treat the arrays of even and odd numbers SEPERATELY with the new integers even and odd.
	 {
		printf("Enter Number %d :",i+1);
		scanf("%d",&n);

		if(n % 2 == 0)
		{
		    evenNum[even] = n;
		    ++even;
		}

		else
		{
		    oddNum[odd] = n;
		    ++odd;
		}
	 } 
	 printf("Even numbers: ");
	 for(even = 0; even < 5; ++even)
	 {
	     if(evenNum[even] < 1) //If the number is less than 1, we have hit the end of the array, so we do not need to be in the loop.
	     {
	         break;
	     }
	     else
	     {
	         printf("%d ",evenNum[even]); //Otherwise, print the even number.
	     }
	 }
	 printf("\nOdd numbers: ");
	 for(odd = 0; odd < 5; ++odd) //Copy of above for loop, with odd numbers instead. 
	 {
	     if(oddNum[odd] < 1)
	     {
	         break;
	     }
	     else
	     {
	         printf("%d ",oddNum[odd]);
	     }
	 }
	 return 0;
}


Always set variables to zero. Not all compilers will set them to zero as default. Ask about anything you do not understand. This is the kind of pretty basic stuff that you are going to encounter constantly in C, so it is important to understand it.
i'm truly a beginner in c++ ,there is a lot of problem i faced when doing array.

does the coding can be used by programming

int NumList[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int oddNum[10], evenNum[10];

the program needs to identify an odd and even numbers based on intNumList[].If a numbers are even,you need to store in evenNum array.If a numbers are odd,you need to store in oddNum array.Then display the output.

ive tried to modify the code mat has given
but i couldnt receive any output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
void main(void)
{

	 int NumList[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
	 int oddNum[10], evenNum[10];
	 int i,n=0;
	 int even=0,odd=0;

	 for(i=0;i<20;i++)
	 {

		if(NumList[i]% 2 == 0)
		{
			 evenNum[i] = n;
			 ++even;
		}

		else
		{
			 oddNum[i] = n;
			 ++odd;
		}

	 }
printf("Even numbers: ");
	 for(even = 0; even < 10; ++even)
	 {
		  if(evenNum[even] < 1)
		  {
				break;
		  }
		  else
		  {
				printf("%d ",evenNum[even]);
		  }
	 }
	 printf("\nOdd numbers: ");
	 for(odd = 0; odd < 10; ++odd)
	 {
		  if(oddNum[odd] < 1)
		  {
				break;
		  }
		  else
		  {
				printf("%d ",oddNum[odd]);
		  }
	 }

}
1 Why would you access evenNum[i] and oddNum[i]. You know that evenNum and oddNum only have 10 elements each and i goes up to 20. What is going to happen when i > 10? Clearly, this is not how it's supposed to be..
2 Why would you assign n to evenNum and oddNum? You never give any value other than 0, so you're just filling your arrays with 0s.
3 When printing numbers, why do you trust the sequence to be terminated with a number less than 1? Memory garbage could have any value. You never fill your arrays with 0s. And you shouldn't. Variables even and odd tell you how many elements are used. Instead of using them as counters, write for(i = 0; i < even; i++).

edit:
Also, your previous code is almost good (it prints all 5 elements of eleNum and oddNum, ignoring the real values of eve and od. You should instead have had two for loops, like in ly last paragraph). What didn't you like about it?

@Mats, You really don't need to set all variables to zero. Just think whether what you read is initialized. There isn't really anything bad with it, it's just silly and ugly. Also, to zero an array, you can int arr[50] = {0};
Last edited on
Topic archived. No new replies allowed.