The program takes the input and finds mean, median, min, and max of the input numbers. I am having problems sorting an array. When it comes to sorting, the largest number gets put first, when in fact it should be smallest to largest. After the first number, the rest of the numbers are correctly sorted. Compiled with Microsoft Visual C++ 6.0.

Code:

//AS-30
#include <iostream>
#include <string>
using namespace std;
void main ()
{
double data [20],sdata[20];
int length,i;
double min,max,mean,median,sum,temp;
bool swapped;
//input data into array
cout <<"Enter data size: "<<endl;
cin >>length;
for (i=0; i<length;i++)
{
cout <<"Enter a data item: "<<endl;
cin >>data[i];
}
//copy the contents of data into sdata
for (i=0; i<length;i++)
{
sdata[i] = data[i];
}
//sort array sdata
swapped=true;
while (swapped)
{
swapped=false;
for (i=0;i<length;i++)
{
if (sdata[i] > sdata[i+1])
{
//swap
temp=sdata[i];
sdata[i] = sdata [i+1];
sdata [i+1] = temp;
swapped=true;
}
}
}
//compute min,max,mean,median
min=sdata[0];
max=sdata[length-1];
sum=0;
for (i=0; i<length;i++)
{
sum=sum+sdata[i];
}
mean=sum/length;
int indexHi,indexLo,index;
if ((length %2) != 0 )
{
index = length / 2;
median = sdata [index];
}
else
{
indexHi = length / 2;
indexLo = indexHi -1;
median = (sdata[indexLo] + sdata[indexHi] ) / 2;
}
//display original data
cout <<"Original data: "<<endl;
for (i=0; i<length;i++)
{
cout <<data[i] << " ";
}
cout <<endl;
//display sorted data
cout <<"Sorted data: "<<endl;
for (i=0; i<length;i++)
{
cout <<sdata[i] << " ";
}
cout <<endl;
cout <<"Min: "<<min<<endl;
cout <<"Max: "<<max<<endl;
cout <<"Mean: "<<mean<<endl;
cout <<"Median: "<<median<<endl;
}