problem with my array help please
May 18, 2013 at 6:28pm UTC
I'm having difficulty implementing the mean/median/max/min.
I get the fact that the first in my sorted would be the min, but how would i get the max if its based on user control so i don't know what the last one is?
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
#include <iostream>
using namespace std;
void sort (double x[], int length)
{
bool continueSort = true ;
int i;
double temp;
while (continueSort == true )
{
continueSort = false ;
for (i=0;i< length-1; i++)
{
if (x[i] > x [i+1])
{
temp = x [i];
x[i] = x[i+1];
x[i+1] = temp;
continueSort = true ;
}
}
}
}
int main()
{
int i,scoreCount,sum=0;
double mean, median, min, max;
cout << "Enter Score Count <1-20> " ;
cin >> scoreCount;
if (scoreCount >= 1 && scoreCount <= 20)
{
double x[scoreCount];
for (i=0;i<scoreCount;i++)
{
cout << "Enter Score #" << i + 1 << ": " ;
cin >> x[i];
sum = sum + x[i];
}
cout << "Original Data: " << endl;
for (i=0;i<scoreCount;i++)
{
cout << x[i] << " " ;
}
cout << endl;
sort( x, scoreCount);
cout << "Sorted Data: " << endl;
for (i=0;i<scoreCount;i++)
{
cout << x[i] << " " ;
}
min = x[0];
max = x[?]
mean = sum/scoreCount;
cout << "Min Value: " << min << endl;
cout << "Max Value: " << max << endl;
cout << "Mean: " << mean << endl;
cout << "Median: " << median << endl;
}
else
{
cout << "Class size is NOT within required range. The required range is 1 to 20." << endl;
}
return 0;
May 18, 2013 at 6:49pm UTC
max:
x[?]
=>
x[scoreCount - 1]
median:
1 2 3 4
#include <cmath>
/*...*/
int middle = scorecount / 2;
median = (scoreCount%2) ? x[middle] : ((x[middle] + x[middle+1]) / 2);
1 2
cin >> scoreCount;
double x[scoreCount];
This is not standard C++ and should not work. I recommend to turn off your compiler extension which allows this.
Last edited on May 18, 2013 at 6:50pm UTC
Topic archived. No new replies allowed.