trouble with arrays, sorting, and calculating the median of grades entered.

Hi guys. I am relatively new to programming and am having trouble writing a code where the user can enter how many grades he wants and the program will sort them and give back the median of all the grades. I can't figure out why I can't input the number of grades I want, it keeps asking until I hit the max grades acceptable (in this case 20 is the max number of grades acceptable). I also can't seem how to find out how to put a grade with decimal points in. Please help and explain where I went wrong. Thanks in advance.

an example of how it should look is as follows:
"Please enter the number of grades: " 6 (user enters 6)
(user enters 6 different grades)
85.2
92.3
78.0
51.5
91.6
87.0
"The median grade is 86.1"


Here is what I got so far. I have looked around on this forum to help me stitch it together.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
void bubbleSort(int v[], int n);
double calcMedian(int v[], int n);

int main()
{
	
	const int size = 20;
	int array[size];
	int i;
	
	//enters number of grades
	cout << "Please enter the number of grades. . .  ";
	cin >> array[size];
	
	for (i = 0; i < size; i++)
		
		{
		//grades for array
			cout << " Please enter grade " << (i + 1) << " : ";
			cin >> array[i];
		}

	//call function to sort the array
	bubbleSort(array, size);
	cout << "\nThe sorted array is : " << endl;
	for (i = 0; i < size; i++)
	{
		
		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;

	return 0;
}


//function for bubble sort
void bubbleSort(int v[], int n)

{

	for (int i = 0; i<n; i++)
	{
		for (int j = 0; j < n - i - 1; j++)
		{			
			if (v[j] > v[j + 1])
			{
								int temp = v[j];
				v[j] = v[j + 1];
				v[j + 1] = temp;
			}
		}
	}
}

//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
	{

		int middle1 = n / 2;
		int middle2 = (n / 2) - 1;
		//find average of the two middle elements
		median = (v[middle1] + v[middle2]) / 2;
	}
	return median;
}
cin >> array[size];
that won't work; instead take a new int and then use that in your for loop at number 19;

needless to say you also have to change your function calls
I can't figure out why I can't input the number of grades I want, it keeps asking until I hit the max grades acceptable


Your for loop on line 19 tells me that it will loop 20 times. You declared the variable size as a constant (Take a look at line 11). One way to determine the number of grades is to declare a variable to determine the number of grades. For example:

1
2
3
4
5
6
7
8
9
10
11
12
int numOfGrades{ 0 };

//enters number of grades
cout << "Please enter the number of grades. . .  ";
cin >> numOfGrades;

for (int i = 0; i < numOfGrades; i++)
{
    //grades for array
    cout << " Please enter grade " << (i + 1) << " : ";
    cin >> arrayGrades[i];
}


That being said, something to think about is input validation. What if the user inputs more than 20 grades? What if the user input a negative number by accident (i.e. -4)?


I also can't seem how to find out how to put a grade with decimal points in.

Take a look at line 12, You need to declare the variable as a float or double and not an integer.

P.S: I would suggest to change the variable array[size] to something like arrayOfGrades[size]
Topic archived. No new replies allowed.