Answer to Question #56520 in C++ for Benjamin Amoasi

Question #56520
Write an application that reads 20 numbers into an array. Calculate the average of the sed number entered as well as the average of the even numbers entered. Display the average of both odd and even numbers and all numbers entered into the array.
(Implement this using function).
1
Expert's answer
2016-01-19T08:28:40-0500
Answer on Question#56520 — Programming — C++

#include <iostream>
using namespace std;

double calculate_odd_average(int array[], int size) {
int sum=0;
int count=1;
for(int i=0; i<size; i++) {
if(array[i]%2==1) {
sum+=array[i];
count++;
}
}
return (double)sum/count;
}

double calculate_even_average(int array[], int size) {
int sum=0;
int count=1;
for(int i=0; i<size; i++) {
if(array[i]%2==0) {
sum+=array[i];
count++;
}
}
return (double)sum/count;
}

double calculate_all_average(int array[], int size) {
int sum=0;
for(int i=0; i<size; i++) {
sum+=array[i];
}
return (double)sum/size;
}

int main() {
int array[20];
for(int i=0; i<20; i++) {
cin>>array[i];
}
cout<<"Odd average: " << calculate_odd_average(array,20)<<endl;
cout<<"Even average: " << calculate_even_average(array,20)<<endl;
cout<<"All average: " << calculate_all_average(array,20)<<endl;
return 0;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS