Arrays

I have a few questions on a school project I am doing any help would be greatly appreciated.

Question #1

In the below code I need to change the grade to a double data type, however every time I try it throws an error. Why? The reason I need to do this is because I need to be able to input the grades with decimal, as well as after the grades get sorted in the table i need to be able to display up to two decimal points.

Question 2:

I am a little lost on making a loop for the program to start over after the initial grades are calculated. I know I need to point it back to the beginning of the program however I cant get it to work.

Any help would be greatly appreciated. I am very new at this. Sorry I don't know how add number lines to it. If anyone could tell me how that would be awesome. I am using Visual Studio Express 2013.

#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 = 100;
int grades[SIZE];
int i;
int userSize;

cout << "How many grades do you wish to enter? Up to 100." << endl; // Asks user for input of grades up to 100
cout << endl;
cin >> userSize; // user input

while (userSize > SIZE) // checks to see if user inputted a valid number and allows them to re-enter if not.
{
cout << endl << "I can not calulate that many grades" << endl;
cout << endl << "Please re-enter the number of grades you would like to enter: ";
cout << endl << endl;
cin >> userSize;
}
cout << endl;

for (i = 0; i < userSize; i++) // asks user to enter all grades up to 100.
{
cout << "Please enter grade number " << i + 1 << " : "; //ask user for input
cin >> grades[i];

while (grades[i] < 0 || grades[i] >100) //make sure that user enters a vlue between 0 and 100
{
cout << "Invalid input. Please enter a percentage between 0 and 100 only." << endl;
cout << "\nPlease Reenter grade " << i + 1 << " : ";
cin >> grades[i];
}
}

bubbleSort(grades, userSize); //call function to sort the array
cout << "\nThe sorted grades are: " << endl;//output the sorted array with 5 grades per line.
for (i = 0; i < userSize; i++)
{
if (i % 5 == 0) //put a line break after displaying 5 grades
cout << "\n";
cout << grades[i] << '\t';
}

double mean = calcMean(grades, userSize); //call function to calculate mean
cout << fixed << "\n\nThe mean of all the grades is : " << setprecision(2) << mean << endl;

double median = calcMedian(grades, userSize); //call function to calculate median
cout << "\n\nThe median of all the grades is : " << setprecision(2) << median << endl;

}


double calcMean(int v[], int n) //function to calculate the mean of the array
{
double 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;
}


double calcMedian(int v[], int n) //function to calculate the median of the array
{
double median; //check if array size is odd
if (n % 2 == 1)
{
int middle = n / 2;
median = v[middle];
}
else if (n % 2 == 0) //else if array size is even
{

int middle = n / 2;
median = (v[middle] + v[middle - 1]) / 2.0; //there are two middle elements in array

}
return median;
}

void bubbleSort(int v[], int n) //function to sort the array

{
for (int i = 0; i<n; i++)
{
for (int j = 0; j < n - i - 1; j++) // sorts the values in the array so that they appear in ascending order
{
if (v[j] > v[j + 1]) // if out of order
{
int temp = v[j]; // then swap
v[j] = v[j + 1];
v[j + 1] = temp;
}
}
}
}
In your calcMedian function, the if-statement for an odd-sized array needs to be slightly altered.

Change it from

1
2
3
4
5
if (n % 2 == 1)
{
    int middle = n / 2;
    median = v[middle];
}


to

1
2
3
4
5
if (n % 2 == 1)
{
    int middle = (n+1) / 2;
    median = v[middle-1];
}


I don't know if that will solve the problem, but who knows.
Last edited on
Topic archived. No new replies allowed.