Dec 11, 2012 at 8:58pm UTC
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
Dec 12, 2012 at 3:48pm UTC
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
Dec 12, 2012 at 4:55pm UTC
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#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);
//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 <<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;
}
// This part works
int main()
{
int i;
int length;
cout << "How many values do you want to enter? \n" ;
cin >> 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 Dec 12, 2012 at 5:03pm UTC
Dec 12, 2012 at 5:00pm UTC
is there a way to fix the min average and lenght?
Dec 12, 2012 at 5:04pm UTC
thank you so much i got it
Dec 12, 2012 at 5:19pm UTC
I did not look at length, but Min and Average was working.