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: