| jxovi10 (10) | |
|
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 | |
|
|
|
| jxovi10 (10) | |
|
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 | |
|
|
|
| SamuelAdams (185) | ||||
|
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.
| ||||
|
Last edited on
|
||||
| jxovi10 (10) | |
|
is there a way to fix the min average and lenght? | |
|
|
|
| jxovi10 (10) | |
| thank you so much i got it | |
|
|
|
| SamuelAdams (185) | |
| I did not look at length, but Min and Average was working. | |
|
|
|