Hello everyone! I was given a problem for school and I need some help with it please. Here it is:
I must first prompt the user to enter 20 numbers then print out the values along with their average. Next I have to get the program to determine the value that is closest to the average and then the value that is the farthest away from the average. I also have to find the difference between the average and these values.
I am having some problems finding the numbers that are closest to and farthest from the average. If anyone could give me some help with this that would be great. Here is my code so far:
// This program prompts the user to enter 20 numbers then prints out
// these numbers, finds their average, determines the value that is closest
// to the average and prints it out. Also it finds the difference between that
// value and the average. It does the same for the value that is farthest
// away from the average as well.
#include <iostream>
usingnamespace std;
constint NUMVALS = 20;
void getVals(double vals[], int &sum);
void displayVals(double vals[]);
double average(double sum);
int main()
{
double vals[NUMVALS];
double sum = 0;
getVals(vals, sum);
cout << endl;
displayVals(vals);
cout << endl;
cout << "Here is the average of your values ---> ";
cout << average(sum);
}
void getVals(double vals[], int &sum);
{
int index;
for(index=0; index<NUMVALS; index++) {
cout << "Please enter the value of array item #" << index+1 << ": ";
cin >> vals[index];
sum+=vals[index];
}
}
void displayVals(double vals[])
{
int index;
for(index=0; index<NUMVALS; index++) {
cout << "Here is array item #" << index+1 << ": ";
cout << vals[index];
}
}
double average(double sum)
{
return sum/NUMVALS;
}
Thanks melkiy that worked for the closest, but I am wondering about how i would i find the farthest? I was never taught how to find this...Is there someplace that I could look online to help me find the farthest value from the average?
emmm... The task is simple enouhg to make it yourself.
You should make the slightest changes in the find_closest_to_average function.
1) "closest_to" replace with "fartherst_from" :)
2) min_difference=INF replace with max_difference=0
3) Some "<" sign must be replaced with ">".