Help Finding Median and Mode

the question given by my teacher is
Write a program that uses an array called marks of length 30. Initialize its values to random numbers between 50 and 100.
-Find the mean for the marks generated
- Print the content of the array.
- Sort the array using Bubble Sort or Selection Sort
- Find the Median value for the marks
- Find the mode of the marks generated.
- Print the array one more time

This is the start i have but i dont know how to do the median and mode with a bubble sort

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main() {
int mark[30];
int temp;
int largest = 0;
float mean;
int sum = 0;
srand(time (0));


for (int index = 0; index <= 29; index++) {
mark[index]= rand() % 50 + 50;

sum = sum + mark[index];
}
mean = sum/30;
cout << "The mean of 50 random numbers is " << mean<< endl;




return 0;
}
Well, here is a nice way to print the array.

1
2
3
4
5
6
7
	//print array
	for (int i = 0; i < 30; i++) {
		cout << mark[i] << " ";
		if ((i+1) % 10 == 0) {
			cout << endl;
		}
	}
acaspiroy wrote:
i dont know how to do the median and mode with a bubble sort

You don't have to do them at the same time. Sort and then think about how you'd find median / mode.

Bubble Sort algorithm? The last part of wikipedia's "optimizing bubble sort" section:
wiki wrote:

To accomplish this in pseudocode we write the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
procedure bubbleSort( A : list of sortable items )
    n = length(A)
    repeat
        newn = 0
        for i = 1 to n-1 inclusive do
            if A[i-1] > A[i] then
                swap(A[i-1], A[i])
                newn = i
            end if
        end for
        n = newn
    until n = 0
end procedure

Last edited on
Here is a little more...

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

void sort(int mark[]) {
	int temp;
	for (int i = 0; i < 30; i++) {
		for (int j = 0; j < 30; j++) {
			if(mark[j] >= mark[i]) {
				temp = mark[i];
				mark[i] = mark[j];
				mark[j] = temp;
			}
		}
	}
}

void printArray(int mark[]) {
	for (int i = 0; i < 30; i++) {
		cout << mark[i] << " ";
		if ((i + 1) % 10 == 0) {
			cout << endl;
		}
	}
	cout << endl;
}

int main() {
	int mark[30];
	int temp;
	int largest = 0;
	float mean;
	int sum = 0;
	srand(time(0));


	for (int index = 0; index <= 29; index++) {
		mark[index] = rand() % 50 + 50;

		sum = sum + mark[index];
	}
	mean = sum / 30;
	cout << "The mean of 50 random numbers is " << mean << endl;

	cout << endl;
	cout << "The 50 random numbers." << endl;

	printArray(mark);
	sort(mark);

	cout << endl;
	cout << "The 50 random numbers in order." << endl;
	printArray(mark);
	
	cout << endl;
	cout << "The median number is ";
	cout << (mark[14] + mark[15]) / 2 << endl;



	system("pause");
	return 0;
}
Topic archived. No new replies allowed.