#include <iostream>
#include <iomanip>
usingnamespace std;
void bubbleSort (int v[],int n);
double calcMean (int v[],int n);
double calcMedian (int v[],int n);
int main ()
{
constint size =10;
int array[size];
int i;
for (i = 0; i < size; i++)
{
//ask use for input
cout << "\nPlease Enter grade " << i+1 << " : ";
cin >> array[i];
//make sure that user enters a vlue between 0 and 100
while(array[i] < 0 || array[i] >100)
{
cout << "Wrong input. Please enter a value between 0 and 100 only." << endl;
cout << "\nPlease Reenter grade " << i+1 << " : ";
cin >> array[i];
}
}
//call function to calculate mean
double mean = calcMean (array, size);
cout << std::fixed << "\n\nThe mean of all the elements is : " << setprecision(2) << mean << endl;
//call function to sort the array
bubbleSort(array, size);
//output the sorted array with 5 grades per line.
cout << "\nThe sorted array is : " << endl;
for (i = 0; i < size; i++)
{
//put a line break after displaying 5 grades
if(i%5==0)
cout << "\n";
cout << array[i] << '\t';
}
//call function to calculate median
double median = calcMedian (array, size);
cout << "\n\nThe median of all the elements is : " << setprecision(2) << median << endl;
system("PAUSE");
return 0;
}
//function to calculate the mean of the array
double calcMean (int v[],int n)
{
int total =0;
//calculate the sum of all elements in the array
for(int i =0 ; i<n; i++)
total = total + v[i];
//calculate the mean of all the elements in the array
double average = total/n;
return average;
}
//function to calculate the median of the array
double calcMedian (int v[],int n)
{
double median;
//check if array size is even
if(n%2==0)
{ int middle = n/2;
median = v[middle];
}
//else if array size is odd
{
//there are two middle elements in array
int middle1 = n/2;
int middle2 = (n/2)-1;
//find average of the two middle elements
median = (v[middle1] + v[middle2])/2;
}
return median;
}
//function to sort the array
void bubbleSort (int v[],int n)
// exchange the values in the array so that they appear in ascending order
{
for (int i =0 ; i<n; i++)
{
for (int j = 0; j < n-i-1; j++)
{
// if out of order
if (v[j] > v[j+1])
{
// then swap
int temp = v[j];
v[j] = v[j + 1];
v[j + 1] = temp;
}
}
}
}
I was able to compile and run your program, with no problems, whatsoever. I am using Microsoft Visual C++ 2008 Express. What compiler are you using, and what errors do you get?
@IU1, I'm at an understanding loss here. I took your program as is, and was able to compile and run it, even using Dev-C++, after commenting out system("PAUSE"); ( Dev-C++ didn't understand it ) so I don't understand why it doesn't work for you, also.
One thing I learned about the Visual Studio software is that it has really unexpected behavior when it comes to opening and closing files and projects, and whatever else it likes to complain about. I ran your code and minus the #include for <stdlib.h> everything ran perfectly.
The only solutions I've seen before was to restart your IDE and see if that remedies your issue, maybe start a new project and copy paste your code into that main.cpp.
MSVS is very picky and is horrible with it's error codes. I suggest using caution when opening and closing projects and leaving the IDE running for a long period of time.