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]<<", ";
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
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.
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;
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.
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
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.
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