Here is what I have. I need to figure out where to put either a sentinel or a do
ID: 3652045 • Letter: H
Question
Here is what I have. I need to figure out where to put either a sentinel or a do..while loop to stop asking for entries. I have a counter, but I cannot figure out where to put the 'while' statement to end the array.#include <iostream>
#include <iomanip>
using namespace std;
void bubbleSort (int calcs[],int n);
double calcMean (int calcs[],int n);
double calcMedian (int calcs[],int n);
int main ()
{
const int max =100;
int grades [max];
int i, count;
cout << "You may enter up to 100 grades. " << endl;
cout << "How many grades would you like to enter?";
cin >> count;
do
{
for (i = 0; i < max; i++)
{
cout << "Please Enter grade " << i+1 << " : ";
cin >> grades [i];
if(grades[i] < 0 || grades[i] >100)
{
cout << "Wrong input. Please enter a value between 0 and 100 only." << endl;
cout << " Please Reenter grade " << i+1 << " : ";
cin >> grades[i];
}
}
}
while (i <=count);
double mean = calcMean (grades,max); //call function to calculate mean
cout << fixed << " The average of all the grades is : " << setprecision(2) << mean << endl;
bubbleSort(grades, max); // call function to sort the array
cout << " The sorted grades are : " << endl;
for (i = 0; i < max; i++)
{
if(i%5==0)
cout << " ";
cout << grades[i] << ' ';
}
double median = calcMedian (grades, max);
cout << " The median of all the grades is : " << setprecision(2) << median << endl;
system("PAUSE");
return 0;
}
double calcMean (int calcs[],int n) // to calculate the mean
{
int total =0;
for(int i =0 ; i<n; i++)
total = total + calcs[i];
double average = total/n;
return average;
}
double calcMedian (int calcs[],int n) // to calculate the median
{
double median;
if(n%2==0)
{ int middle = n/2;
median = calcs[middle];
}
else
{
int middle1 = n/2;
int middle2 = (n/2)-1;
median = (calcs[middle1] + calcs[middle2])/2;
}
return median;
}
void bubbleSort (int calcs[],int n) //function to sort the array
{
for (int i =0 ; i<n; i++)
{
for (int j = 0; j < n-i-1; j++)
{
if (calcs[j] > calcs[j+1])
{
int temp = calcs[j];
calcs[j] = calcs[j + 1];
calcs[j + 1] = temp;
}
}
}
}