how to get the average for odd and even of their sum

how to get the average i don't know what is wrong with my code.
i get my even and odds but i need to get the average of both odd and even in separate ways of their sum. help me please


{

int num [100], numin;
int sum=0, sum2=0;
int ave=0;
float ave2=0;

cout<<"Enter number of input: ";
cin>>numin;
cout<<endl;
for (int ctr=0;ctr<numin;ctr++) {
cout<<"Enter Input#: "<<ctr+1<<": ";
cin>>num[ctr];
}
cout<<endl;
cout<<"The even_numbers are: ";
for (int ctr=0;ctr<numin;ctr++) {

if (num[ctr]%2==0) {
cout<<num[ctr]<<", ";
sum = sum+num[ctr];


}
ave = sum/num[ctr];
}
cout<<endl;
cout<<"Total of even numbers: "<<sum;
cout<<endl;
cout<<"Average of Even: "<<ave;
cout<<endl;
cout<<"The odd_numbers are: ";
for (int size=0;size<numin;size++) {
if (num[size]%2==1) {
cout<<num[size]<<", ";
sum2 = sum2+num[size];


}
ave2 = sum2/num[size];
}
cout<<endl;
cout<<"Total of odd numbers: "<<sum2;
cout<<endl;
cout<<"Average of odd: "<<ave2;
cout<<endl;
cout<<"Below Number [10]: ";
for (int ctr=0;ctr<numin;ctr++) {
if (num[ctr]<=10) {
cout<<num[ctr]<<", ";


}
}

return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
  int sum_odds = 0, n_odds = 0;
  int sum_evens = 0, n_evens = 0;

  for (int n; std::cin >> n; )
  {
    if (n % 2 == 0) { sum_evens += n; ++n_evens; }
    else { sum_odds += n; ++n_odds; }
  }
 
  double const average_evens = sum_evens / static_cast<double>(n_evens);
  double const average_odds = sum_odds / static_cast<double>(n_odds);
  
  std::cout << "average of even numbers: " << average_evens << '\n';
  std::cout << "average of odd numbers: " << average_odds << '\n';
}
Last edited on
thank you for replying
sorry but i'm a beginner in c++ is there any problem with my codes? where should i put that or do i miss something with my code
the sum of even is average of even.
the sum of odd is average of odd.

i can't get them.
The average is the sum divided by the number of items.

For both even and odd numbers you have found the sum, but you haven't counted them.

What you are currently dividing by in forming ave or ave2 is NOT the number of items.

Please give complete, compileable code and put it in code tags.
example i put 5 input numbers even and odd
then they will be separated
like this
input numbers:12
input numbers:16
input numbers:33
input numbers:45
input numbers:5

the even numbers are: 12, 16
total of even numbers: 28
the average of total of even is: 14

the odd numbers are: 33, 45, 5
total of odd numbers: 83
the average of total of odd: 27.3333

below number [10]: 5

this should be the right output..
the 83 will be divided on 3 numbers i input in odd numbers
and the total of even numbers is divide into 2 numbers.. so its like counting the 2 numbers into 2 and the 3 numbers is 3 like that..sorry for my bad english
Last edited on
Post your most recent, full code. Select it and put it in code tags (first item on the formatting menu).

Please don't ignore our earlier posts.

If you divide an int by an int you will get truncation because of integer division. That is one problem. However, it wasn't the major problem with your earlier code.
Last edited on
sorry im a student and new user of dev c

cout<<"Enter number of input: ";
cin>>numin;
cout<<endl;
for (int ctr=0;ctr<numin;ctr++) {
cout<<"Enter Input#: "<<ctr+1<<": ";
cin>>num[ctr];
}
cout<<endl;
cout<<"The even_numbers are: ";
for (int ctr=0;ctr<numin;ctr++) {

if (num[ctr]%2==0) {
cout<<num[ctr]<<", "; // this should be the 16, and 12,
sum = sum+num[ctr]; // this should be the 28


}
ave = sum/num[ctr]; // this should be 28 and the 12,16 count as 2 item.. i dont know how to make them 2 items to get the average of 28
}
cout<<endl;
cout<<"Total of even numbers: "<<sum;
cout<<endl;
cout<<"Average of Even: "<<ave;
cout<<endl;
I repeat:
Post your most recent, full code. Select it and put it in code tags.

You are still ignoring everything said above.
how im sorry im just new here
this is my full code
#include<iostream>
#include<cstring>

using namespace std;

int main () {

int num [100], numin;
int sum=0, sum2=0;
int ave=0;
float ave2=0;

cout<<"Enter number of input: ";
cin>>numin;
cout<<endl;
for (int ctr=0;ctr<numin;ctr++) {
cout<<"Enter Input#: "<<ctr+1<<": ";
cin>>num[ctr];
}
cout<<endl;
cout<<"The even_numbers are: ";
for (int ctr=0;ctr<numin;ctr++) {

if (num[ctr]%2==0) {
cout<<num[ctr]<<", ";
sum = sum+num[ctr];


}
ave = sum/num[ctr];
}
cout<<endl;
cout<<"Total of even numbers: "<<sum;
cout<<endl;
cout<<"Average of Even: "<<ave;
cout<<endl;
cout<<"The odd_numbers are: ";
for (int size=0;size<numin;size++) {
if (num[size]%2==1) {
cout<<num[size]<<", ";
sum2 = sum2+num[size];


}
ave2 = sum2/num[size];
}
cout<<endl;
cout<<"Total of odd numbers: "<<sum2;
cout<<endl;
cout<<"Average of odd: "<<ave2;
cout<<endl;
cout<<"Below Number [10]: ";
for (int ctr=0;ctr<numin;ctr++) {
if (num[ctr]<=10) {
cout<<num[ctr]<<", ";


}
}

return 0;
}
what is code tag?
here sorry im new


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
#include<iostream>
#include<cstring>

using namespace std;

int main () {
	
 int num [100], numin;
 int sum=0, sum2=0;
 int ave=0;
 float ave2=0;
	 
	 cout<<"Enter number of input: ";
		 cin>>numin;
		 cout<<endl;
		 for (int ctr=0;ctr<numin;ctr++) {
	 		cout<<"Enter Input#: "<<ctr+1<<": ";
	 		cin>>num[ctr];
		 }
		 cout<<endl;
		 cout<<"The even_numbers are: ";
		 for (int ctr=0;ctr<numin;ctr++) {
	 	
	 		if (num[ctr]%2==0) {
	 		cout<<num[ctr]<<", ";
	 		sum = sum+num[ctr];	
	 		 		 	
		 }	
		 	ave = sum/num[ctr];
	 }
		cout<<endl;
		cout<<"Total of even numbers: "<<sum;
		cout<<endl;
		cout<<"Average of Even: "<<ave;
	 	cout<<endl;
	 	cout<<"The odd_numbers are: ";
		 	for (int size=0;size<numin;size++) {
	 			if (num[size]%2==1) {
	 			cout<<num[size]<<", ";
	 			sum2 = sum2+num[size];
	 						
		  }
			ave2 = sum2/num[size];	
	}
		 cout<<endl;
		 cout<<"Total of odd numbers: "<<sum2;
		 cout<<endl;
		 cout<<"Average of odd: "<<ave2;
		 cout<<endl;
	 	 cout<<"Below Number [10]: ";
			for (int ctr=0;ctr<numin;ctr++) {
	 		if (num[ctr]<=10) {
	 			cout<<num[ctr]<<", ";
	 		

		}
	}
		
	return 0;
}
Last edited on
Your calculation of the average is wrong. e.g.
ave = sum/num[ctr];
should be something like
ave = (double)sum/n_evens;
where you need to introduce a variable n_evens (like @mbozzi has done) and COUNT THE EVEN NUMBERS.

num[ctr] does NOT hold the number of even numbers.

The (double) is a casting operation, which ensures that integer division does not truncate. That needs to be done, but is less important than actually COUNTING YOUR EVEN NUMBERS.


Exactly similar applies to the odd numbers.
ave = sum/num[ctr];

this is not the average. average of something is a sum/total number of items.
for the evens, its something like this pseudocode:

1
2
3
4
5
6
7
8
9
10
for(all the numbers)
{
      if even
      {
          cout 
          sum += number
          divisor++;
       }
}
ave = sum/divisor;
Last edited on
Your calculation of the average is wrong. e.g.
ave = sum/num[ctr];
should be something like
ave = (double)sum/n_evens;
where you need to introduce a variable n_evens (like @mbozzi has done) and COUNT THE EVEN NUMBERS.

num[ctr] does NOT hold the number of even numbers.

The (double) is a casting operation, which ensures that integer division does not truncate. That needs to be done, but is less important than actually COUNTING YOUR EVEN NUMBERS.


Exactly similar applies to the odd numbers.


where should i put in or place it.. thank you for answering me and giving time on me.. this will be a big help for me.. to improve in dev c
LittleKnowledge wrote:
where should i put in or place it.


Create a variable, for example n_evens, initialised to 0 (in the same way, and in the same place, where you have created and initialised sum.

You already have a loop that goes through the even numbers. Here, you currently add the number to sum. Well, at the same point you can add 1 to n_evens.

Then you can use n_evens in the calculation of ave instead of the wrong num[ctr]. Remember also to cast with (double) or you will suffer from integer division.

That is enough explanation, I think.
You have:
1
2
3
4
5
6
7
8
9
int sum = 0;
float ave = 0;
for ( int ctr=0; ctr < numin; ctr++ ) {
  if ( num[ctr]%2==0 ) {
    cout << num[ctr]<<", ";
    sum = sum+num[ctr];
  }	
  ave = sum / num[ctr];
}

That loop does three things:
1. prints num[ctr] (if it is even)
2. adds num[ctr] to sum (if it is even)
3. divides sum-so-far with value of num[ctr]

There is no point to calculate average more than once. Do it after the loop.
sum = 0
count = 0
FOR EACH value IN num
  IF value IS EVEN
    add value to sum
    add 1 to count
    print value

AFTER LOOP
IF count is larger than 0
  calculate average
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
#include<iostream>
#include<cstring>

using namespace std;

int main() {

	int num[100], numin;
	int sum = 0, sum2 = 0;
	float ave = 0;
	float ave2 = 0;
	int count = 0;


	cout << "Enter number of input: ";
	cin >> numin;
	cout << endl;
	for (int ctr = 0; ctr < numin; ctr++) {
		cout << "Enter Input#: " << ctr + 1 << ": ";
		cin >> num[ctr];
	}
	cout << endl;
	cout << "The even_numbers are: ";
	for (int ctr = 0; ctr < numin; ctr++) {

		if (num[ctr] % 2 == 0) {
			cout << num[ctr] << ", ";
			sum = sum + num[ctr];
			count++;

		}
		if (count > 0) {
			ave = sum / count;
		}
		else {
			ave = sum;
		}
	}
	cout << endl;
	cout << "Total of even numbers: " << sum;
	cout << endl;
	cout << "Average of Even: " << ave;
	cout << endl;
	count = 0;
	cout << "The odd_numbers are: ";
	for (int size = 0; size < numin; size++) {
		if (num[size] % 2 == 1) {
			cout << num[size] << ", ";
			sum2 = sum2 + num[size];
			count++;

		}
		if (count > 0) {
			ave2 = sum2 / count;
		}
		else {
			ave2 = sum2;
		}
	}
	cout << endl;
	cout << "Total of odd numbers: " << sum2;
	cout << endl;
	cout << "Average of odd: " << ave2;
	cout << endl;
	cout << "Below Number [10]: ";
	for (int ctr = 0; ctr < numin; ctr++) {
		if (num[ctr] <= 10) {
			cout << num[ctr] << ", ";


		}
	}

	return 0;
}
Topic archived. No new replies allowed.