how to calculate median and mode with bubble or selection sorts

the question i was given for homework was:

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

I have the beginning but i don't know how to calculate median and mode with bubble or selection sorts

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;
}

The random number generation should be rand() % 51 + 50 if you want 50 to 100, inclusive.

To calculate the mean you can't just divide the integer sum by the integer 30. Since both are integers, the division will be performed as integer arithmetic which tosses out any remainder. You need to make one of them floating point so the division is performed in floating point mode. It's easiest to just write 30.0 instead of 30.

So fix those and add a loop to print out the array after you print the mean.

When posting code, remember to use code tags: http://www.cplusplus.com/forum/articles/16853/
Topic archived. No new replies allowed.