### urgent please help

can someone please help me fix my code so that the it shows the following output
SAMPLE OUTPUT

How many values do you want to enter?
5
Enter values separated by one space
3 5 2 4 1
You entered:
3 5 2 4 1
Max Entered: 5
Min Entered: 1
Average:3.0000
Length:7.4162
-------------------------------------------------------------------------------
my code:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

double find_max(double arr[], int length);
double find_min(double arr[], int length);
double find_average(double arr[], int length);
double find_length(double arr[], int length);

int main()
{
int i;
int length;
cout << "How many values do you want to enter? \n";
cin >> length;
double *myarray = new double[length];

for (i = 0; i < length; i++)
{
cout << "Enter values seperated by one space" << endl;
cin >> myarray[i];
}
cout <<"You Entered:" <<endl;
cout<< myarray[i];

cout << find_max(myarray,length) << endl;
cout << find_min(myarray,length) <<endl;
cout <<setprecision(4)<<fixed;
cout << find_average(myarray,length) <<endl;
cout << find_length(myarray,length) <<endl;

system("pause");
return 0;
}

//Find Max:
double find_max(double arr[], int length)
{
cout << "\nMax Entered:" << endl;
double max = 0;
int j;
for (j = 0;j < length; j++)
{
if (arr[j]>max)
{
max = arr[j];
}
}
return max;
}

//Find Min
double find_min(double arr[], int length)
{

double min = 999999;
int k;
for (k = 0; k < length; k++)
{
if (arr[k] < min)
{
min = arr[k];
cout << "\nMin Entered:"<< min << endl;
}
}
return min;
}

//Find Average
double find_average(double arr[], int length)
{

int l;
double sum = 0;
double avg;
for (l = 0;l < length; l++)
{
sum+=arr[l];
cout <<setprecision(4)<<fixed;
}
avg = sum/length;
cout<<"\nAverage:"<< avg<< endl;
return avg;
}

//find_length
double find_length(double arr[], int length)
{

int m;
double sumsq = 0;
double elength;
for (m = 0; m < length; m++)
{
sumsq+=(arr[m]*arr[m]);
}
elength = sqrt(sumsq);
cout << "\nLength:"<<elength << endl;
return elength;
}

thank you for your help
my output for the following are

Min Entered: 1
1

Average:3.0000
3.0000

Length:7.4162
7.4162

can someone please explain to me how to fix this
Please use code format tags in the future.

I fixed your loop problem in average by moving the cout down 2 lines I think.

I moved Main to the bottom because that is the way I was taught to do it.

I added one for loop to output the users input. You should have been able to do that if you wrote the for loop above it.

All the math seems to work.

NOTE: Your program will fail if someone enters a non numeric value.

Still a little to do formatting the output, which I'm sure you can do.

Here is the output as it looks now.

 How many values do you want to enter? 5 Enter values seperated by one space 3 5 2 4 1 You Entered: 3 5 2 4 1 Max Entered: 5 Min Entered: 1 1 Average: 3.0000 3.0000 Length: 7.4162 7.4162

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120`` ``````#include #include #include #include using namespace std; double find_max(double arr[], int length); double find_min(double arr[], int length); double find_average(double arr[], int length); double find_length(double arr[], int length); //Find Max: double find_max(double arr[], int length) { cout << "\nMax Entered: " ; double max = 0; int j; for (j = 0;j < length; j++) { if (arr[j]>max) { max = arr[j]; } } return max; } //Find Min double find_min(double arr[], int length) { double min = 999999; int k; for (k = 0; k < length; k++) { if (arr[k] < min) { min = arr[k]; // cout << "\nMin Entered:"<< min << endl; } } cout << "\nMin Entered: "<< min << endl; return min; } //Find Average double find_average(double arr[], int length) { int l; double sum = 0; double avg; for (l = 0;l < length; l++) { sum+=arr[l]; cout <> length; double *myarray = new double[length]; cout << "Enter values seperated by one space" << endl; for (i = 0; i < length; i++) { cin >> myarray[i]; } // cout << "***********************" << endl; cout << "You Entered: " ; for (i = 0; i < length; i++) { // cout << i << " "; cout << myarray[i] << " " ; } cout << endl; // cout << "***********************" << endl; cout << find_max(myarray,length) << endl; cout << find_min(myarray,length) << endl; cout << setprecision(4) << fixed; cout << find_average(myarray,length) << endl; cout << find_length(myarray,length) << endl; // system("pause"); return 0; }``````
Last edited on
is there a way to fix the min average and lenght?
thank you so much i got it
I did not look at length, but Min and Average was working.
Topic archived. No new replies allowed.