106k views
2 votes
This program involves working with a set of discrete data (grades) and determining their average (arithmetic mean), median, and mode. These operations are explained below.1. The average of the grades is the sum of all grades divided by the number of the grades.2. The median is the value located in middle of all grades. The grades should be ordered first. If the set contains an even number of values, the median is the average of the two middle values.3. The mode is the value that occurs most often or with the great frequency.4. The standard deviation shows how much variation exists from the average. It is calculated using the following equation: Where x1, x2, ..., xN represent the grades and ยต represents the mean (average).Write a C++ program that handles any number of grades and performs the data analysis operations outlined above. Consider the following requirements.a. Read the input data from user. The program must not assume a fixed length of data; it should determine the length dynamically.b. Display the result on a standard display monitor.c. Use functions for each specific operations; for instance,double getAverage(int *grades, int size)double getMedian(int *grades, int size)int getMode(int *grades, int size)double getSd(int *grades, int size)d. If the grades have no mode, the getMode function should return -1. If the grades have more than one mode, the function should return anyone of them.e. Do not accept negative numbers for input.

1 Answer

3 votes

Answer:

#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

// function declarations

double getAverage(int* grades, int size);

double getMedian(int* grades, int size);

int getMode(int* grades, int size);

double getSd(int* grades, int size);

int main()

{

// Declaring variables

int size, val;

// setting the precision to two decimal places

std::cout << std::setprecision(2) << std::fixed;

// Getting the input entered by the user

cout << "How many grades you wnat to enter :";

cin >> size;

// Creating array dynamically

int* grades = new int[size];

/* Getting the inputs entered by the user

* and populate those values into array

*/

for (int i = 0; i < size;)

{

while (true)

{

cout << "Enter the grade#" << i + 1 << ":";

cin >> val;

if (val < 0)

{

cout << "** Invalid.Must be greater than zero. **" << endl;

continue;

}

else

{

grades[i] = val;

i++;

break;

}

}

}

// calling the functions

double avg = getAverage(grades, size);

double median = getMedian(grades, size);

int mode = getMode(grades, size);

double sd = getSd(grades, size);

cout << "Average :" << avg << endl;

cout << "Median :" << median << endl;

cout << "Mode :" << mode << endl;

cout << "Standard Deviation :" << sd << endl;

return 0;

}

// This function calculates the average of grades

double getAverage(int* grades, int size)

{

double sum = 0.0;

// calculating the sum of res[] array elements

for (int i = 0; i < size; i++)

{

// calculating the sum

sum += grades[i];

}

// calculating the average

double avg = sum / size;

return avg;

}

// This function calculates the median of grades

double getMedian(int* grades, int size)

{

// This Logic will Sort the Array of elements in Ascending order

int temp;

int middle;

double median;

for (int i = 0; i < size; i++)

{

for (int j = i + 1; j < size; j++)

{

if (grades[i] > grades[j])

{

temp = grades[i];

grades[i] = grades[j];

grades[j] = temp;

}

}

}

if (size % 2 == 0)

{

middle = size / 2;

median = (float)(grades[middle - 1] + grades[middle]) / 2.0;

}

else

{

middle = (size + 1) / 2;

median = grades[middle];

}

return median;

}

// This function calculates the mode of grades

int getMode(int* grades, int size)

{

int counter1 = 0, counter2, modevalue;

for (int i = 0; i < size; i++)

{

counter2 = 0;

for (int j = i; j < size; j++)

{

if (*(grades + i) == *(grades + j))

{

counter2++;

}

if (counter2 > counter1)

{

counter1 = counter2;

modevalue = *(grades + i);

}

}

}

if (counter1 > 1)

return modevalue;

else

return -1;

}

// This function calculates the standard deviation of grades

double getSd(int* grades, int size)

{

int sum_of_squares = 0, standard_deviation;

double variance;

double avg = getAverage(grades, size);

/* This loop Calculating the sum of

* square of eeach element in the array

*/

for (int i = 0; i < size; i++)

{

/* Calculating the sum of square of

* each element in the array

*/

sum_of_squares += pow((grades[i] - avg), 2);

}

// calculating the variance of an array

variance = ((double)sum_of_squares / (size - 1));

// calculating the standard deviation of an array

standard_deviation = sqrt(variance);

return standard_deviation;

}

Step-by-step explanation:

User Tomas F
by
4.0k points