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; }
Comments
Leave a comment