- #1
odinx
- 9
- 0
Code:
#include <iostream>
#include <cstdlib>
#include <cmath>
using std::cout;
using std::endl;
using std::cin;
double sort(double input[], int N)
{
double temp;
int i, j;
for (i=0; i<=8; i++)
{
for (j=i+1; j<=9; j++)
{
if(input[i]>input[j])
{
temp=input[i];
input[i]=input[j];
input[j]=temp;
}
}
}
return input[N];
}
int main()
{
const int N=10;
double x, y, sum, total, input[N];
int i;
sum=0;
x=1;
cout << "Enter 10 numbers: " << endl;
for (i=0; i<=9; i++)
{
if (x<=10)
{
cout << "Number " << x << ": ";
cin >> input[i];
x++;
}
else if (x==10)
{
break;
}
}
sort(input, N);
cout << "Here are the numbers in sorted order:" << endl;
for (i=0; i<=9; i++)
{
cout << input[i] << " ";
}
y=((input[N/2] + input[N/2-1])/2);
cout <<"\nThe median is " << y << endl;
for(i=0; i<=9; i++)
{
sum=sum+input[i];
}
total=(sum/10);
cout << "The mean is " << total << endl;
return EXIT_SUCCESS;
}
I'm trying to give an error message when the user doesn't enter a number, but I haven't been able to get it to work.
this is what I tried:
Code:
cout << "Enter 10 numbers: " << endl;
for (i=0; i<=9; i++)
{
if (x<=10)
{
cout << "Number " << x << ": ";
cin >> input[i];
if (!cin)
{
cin.clear();
cout << "Error: You Must Enter A Number. " << endl;
cin >> input[i];
}
x++;
}
else if (x==10)
{
break;
}
}