Grade arrays

I have to make a code that finds the test means and medians of the test scores that are put in. I have also added code to sort the test grades. The user
doesn't know ahead of time how many students will exactly take the test but can assume that it's between 1 and 100, that's the part I'm having trouble with. Here's my code below, but it only goes up to ten students, I don't know how to code it so that the user can control how many grades can be put in..


#include <iostream>
#include <iomanip>
using namespace std;
void bubbleSort(int v[], int n);
double calcMean(int v[], int n);
double calcMedian(int v[], int n);

int main()
{
const int 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;
}
}
}
}
Last edited on
I'm a beginner so would I go about this by like

cout << "How many students are there (1 - 100)? "; //Variable starts at 0
cin >> i;

When I went about this way if I typed in there are 3 students, it would only ask for 2, is there a way I could fix that? And would this be the right approach?
Topic archived. No new replies allowed.