Question #40751

Created by Esguerra John Richard
Write a C++ application that asks the user to enter 10 numbers. The program then stores those numbers in an Array. The program should display the Mean , Median, and Mode.
Mean is the average of the 10 numbers.
Median is the average of the 5th and the 6th numbers. (But you have to arrange the numbers in ascending order first using Bubble Sort before calculating the Median.)
Mode is the most frequent number among the 10 numbers.

Expert's answer

//Answer on Question#40751 - Progamming - C++

#include <iostream>

using namespace std;

int main() {

cout << "Input 10 numbers: ";

double nums[10];

double sum;

for (int i = 0; i < 10; i++) {

cin >> nums[i];

sum += nums[i];

}

cout << "The mean is " << sum / 10.0 << endl;

for (int k = 0; k < 10; k++) {

for (int i = 1; i < 10; i++) {

if (nums[i-1] > nums[i]) {

double tmp = nums[i-1];

nums[i-1] = nums[i];

nums[i] = tmp;

}

}

}

cout << "The median is " << (nums[4] + nums[5]) / 2 << endl;

double mode = nums[0];

int mode_cnt = 1;

for (int i = 0; i < 10; i++) {

int curr_cnt = 0;

for (int j = 0; j < 10; j++) {

if (nums[i] == nums[j]) ++curr_cnt;

}

if (curr_cnt > mode_cnt) {

mode = nums[i];

mode_cnt = curr_cnt;

}

}

cout << "The mode is " << mode << endl;

cin.ignore().get();

}

#include <iostream>

using namespace std;

int main() {

cout << "Input 10 numbers: ";

double nums[10];

double sum;

for (int i = 0; i < 10; i++) {

cin >> nums[i];

sum += nums[i];

}

cout << "The mean is " << sum / 10.0 << endl;

for (int k = 0; k < 10; k++) {

for (int i = 1; i < 10; i++) {

if (nums[i-1] > nums[i]) {

double tmp = nums[i-1];

nums[i-1] = nums[i];

nums[i] = tmp;

}

}

}

cout << "The median is " << (nums[4] + nums[5]) / 2 << endl;

double mode = nums[0];

int mode_cnt = 1;

for (int i = 0; i < 10; i++) {

int curr_cnt = 0;

for (int j = 0; j < 10; j++) {

if (nums[i] == nums[j]) ++curr_cnt;

}

if (curr_cnt > mode_cnt) {

mode = nums[i];

mode_cnt = curr_cnt;

}

}

cout << "The mode is " << mode << endl;

cin.ignore().get();

}

## Comments

## Leave a comment