Sort odd & even and display ascending/descending

The code works so far when entering ONLY odd or even numbers (test set was two numbers). however it runs faulty values as soon as I put in odd AND even values.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Accept n number of inputs from the keyboard and display the odd numbers in ascending order , even numbers in descending order
#include<iostream>

using namespace std;

int main()
{

int ecount, ocount, limit, no, i,j,t, odd[100], even[100];

ecount=0;
ocount=0;
no=0;


cout<<"Enter limit of numbers.\n";
cin>>limit;
for(i=1;i<=limit;i++)
	{
cout<<"enter "<<i<<"st no ";
cin>>no;

//Summing odd and even numbers
	if(no%2==0)
	{
//Sum Even numbers	
	even[i]=no;
	ecount=ecount+1;

	}
	else
	{
//Sum Even numbers
	odd[i]=no;
	ocount=ocount+1;
	}
	}

cout<<"The frequency of even numbers is "<<ecount<<" \n";
cout<<"The event of odd numbers is "<<ocount<<" \n";


//C1.2 III
//  Accept n number of inputs from the keyboard
//  Display the sum of all the given numbers.
//  Display the numbers in ascending order.


//Sorting in Ascending Order
for(i=1; i<=limit; i++)
		{
	for(j=i+1; j<=limit; j++)
		{
	if(odd[i] > odd[j])
		{
		t = odd[i];
		odd[i] = odd[j];
		odd[j] = t;
		}
		}
		}
//Sorting in Descending Order
for(i=1; i<=limit; i++)
	{
	for(j=i+1; j<=limit; j++)
		{
		if(even[i]<even[j])
			{
			t = even[i];
			even[i] = even[j];
			even[j] = t;
			}
		}
	}

//Display in Descending Order
cout<<"Even numbers (Descending): ";
for(i=1; i<=ecount; i++)
	{
	cout<<even[i]<<" ";
	}
	cout<<"\n";

//Display in Ascending Order
cout<<"Odd numbers (Ascending): ";
for(i=1; i<=ocount; i++)
	{
	cout<<odd[i]<<" ";
	}

	return 0;

}



When you are doing sorting and filling of even and odd arrays use ecount and ocount. As an example:
replace
1
2
3
4
5
6
7
8
9
10
11
12
13
	if(no%2==0)
	{
//Sum Even numbers	
	even[i]=no;
	ecount=ecount+1;

	}
	else
	{
//Sum Even numbers
	odd[i]=no;
	ocount=ocount+1;
	}

with
1
2
3
4
if(no%2==0)
	even[ecount++]=no;
else
	odd[ocount++]=no;


Current code allows garbage to sneak intro your data.
Revised it as follows:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Accept n number of inputs from the keyboard and display 
//the odd numbers in ascending order , even numbers in descending order
#include<iostream>

using namespace std;

int main()
{

	int ecount, ocount, limit, no, i,j,t,e,o, odd[100], even[100], second;

	ecount=0;
	ocount=0;
	no=0;


	cout<<"Enter limit of numbers.\n";
	cin>>limit;
	for(i=1;i<=limit;i++)
		{
		cout<<"enter "<<i<<"st no ";
		cin>>no;

	//Summing odd and even numbers
	//Sum Even numbers	
		if(no%2==0)
			{
			even[ecount++]=no;
			}
	//Sum Even numbers
		else
			{
			odd[ocount++]=no;
			}
		}

		cout<<"The frequency of even numbers is "<<ecount<<" \n";
		cout<<"The event of odd numbers is "<<ocount<<" \n";
		e=ecount;
		o=ocount;
	//C1.2 III
	//  Accept n number of inputs from the keyboard
	//  Display the sum of all the given numbers.
	//  Display the numbers in ascending order.


	//Sorting in Ascending Order (low first, then up)
/*
for(i=1; i<=limit; i++)
		{
	for(j=i+1; j<=limit; j++)
		{
	if(odd[i] > odd[j])
		{
		t = odd[i];
		odd[i] = odd[j];
		odd[j] = t;
		}
		}
		}*/

	for(ocount=o; ocount>=0; ocount--)
		{
		for(second=ocount-1; second>=0; second--)
			{
			if(odd[ocount] > odd[second])
				{
			t = odd[ocount];
			odd[ocount] = odd[second];
			odd[second] = t;
			
				}
		
			}
		}
//Sorting in Descending Order (high first, then down)
for(ecount=e; ecount>=0; ecount--)
	{
	for(second=ecount-1; second>=0; second--)
		{		
			if(even[ecount]<even[second])
			{
			t = even[ecount];
			even[ecount] = even[second];
			even[second] = t;
			
			}
		}
	}

//Display in Descending Order (high first, then down)
cout<<"Even numbers (Descending): ";
for(ecount=e; ecount>0; ecount--)
	{
	cout<<even[ecount]<<" ";
	}
	cout<<"\n";

//Display in Ascending Order
cout<<"Odd numbers (Ascending): ";
for(ocount=o; ocount>0; ocount--)
	{
	cout<<odd[ocount]<<" ";
	}

	return 0;

}


And yet, it works for 4 numbers, not more...

Last edited on
You are going beyond arrays index limits in lines 101, 93, 77 & 62.
Line 101 should be modified to
for(ocount = o - 1; ocount >= 0; ocount--)

PS if array has n elements then first element has index 0, the last one n-1
Topic archived. No new replies allowed.