Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program in statistics_given.cpp calculates the mean, median, mode and standa

ID: 3744174 • Letter: T

Question

The program in statistics_given.cpp calculates the mean, median, mode and standard deviation of a given set of integers.

As it is given, the set of integers is taken from standard in as a string, which is then parsed using stringstream to count the number of integers in the string and insert them into an array.

The program in statistics_given.cpp file works, but it is all crammed into the main function. As a consequence, a lot of code gets repeated, and the overall logic of the program is hidden by the mass of details.

Rewrite the program by grouping the code into functions.

Change the program so it takes the input from a file ‘input.txt’ instead of the terminal. In particular, your program should include the following functions:

1. A void function named readFile. The parameter should be a string passed by reference. This function reads a set of numbers from a text file as a single string.

2. A function named countInt that returns an integer. The parameter should be one string containing the set of numbers taken from the file and counts the integers in the string.

3. A function named fillArray that takes a string, an integer array, and the arrays size as parameters and returns nothing. This function fills the integer array with the integers from a string. Query: are arrays passed by reference automatically?

4. A function named bubbleSort() that returns nothing (void). The parameter should be an integer array and its size. The array is sorted by the function.

5. A function named calcMean()that returns a double. The parameters should be an integer array and the size of the array. Calculates the mean of the set of integers in the array.

6. A function named calcMedian()that returns a double. The parameters should be an integer array and the size of the array. Calculates the median of the set of integers in the array.

7. A function named calcMode()that returns an integer. The parameters should be an integer array, the size of the array, and a Boolean value. Calculates the mode of the set of integers in the array and sets the Boolean value to false if mode is not found. Hint: pass Boolean by reference.

8. A function named calcStdDev()that returns a double, The parameters should be an integer array, the size of the array, and a double. Calculates the standard deviation of the set of integers in the array. As you introduce each function, replace the code in main() by calls to your new functions as appropriate. In particular, note that some of these new functions may be called from within the bodies of some of the other functions.

Remember that this program was already working.

You should not alter the output of the program in any way. Hopefully, once you are done it will be obvious to you that your revised code is simpler and easier to understand than the original. In real life, code that is easier to understand, is easier to get working in the first place, so you should try to develop the code in small, function-based chunks from the very beginning. To test your code you can always run inputs through the original .cpp and your own to compare the outputs

Explanation / Answer

#include <iostream>
#include <sstream>/// istringstream
#include <cmath> /// pow, sqrt
using namespace std;
int calcMode(int intArray[], int counter, bool *modefound){
///calculate mode of the numbers
int temp;
temp=intArray[0];
int mocount=1, mode, i;
int maxi=1;
mode=temp;
*modefound=false;
for(i=1; i<counter; i++){
if(intArray[i]==temp){
mocount++;
if(mocount>maxi){
maxi=mocount;
mode=intArray[i];
*modefound=true;
}
else if(mocount==maxi){
*modefound=false;
}
}
else{
if(mocount>maxi){
maxi=mocount;
mode=intArray[i];
*modefound=true;
}
mocount=1;
temp=intArray[i];
}
}
return mode;
}
double calcStdDev(int intArray[], int counter, double stddev){
///calculate the standard deviation
double sum=0.0;
double avg=0.0;
int i;
for(i=0; i<counter; i++){
avg += intArray[i];
}
avg= avg/counter;
for(i=0; i<counter; i++){
sum+= pow(intArray[i]-avg,2);
}
stddev=sqrt(sum/(counter-1));
return stddev;
}
double calcMedian(int intArray[], int counter){
double median;
///calculate the median of the numbers
if(counter%2==0){
median= intArray[(counter/2)-1] + intArray[counter/2];
median=median/2;
}
else{
median= intArray[(counter/2)];
}
cout << "Mode = " << calcMode(intArray, counter, false) << endl;
cout << "Standard Deviation = " << calcStdDev(intArray, counter, 0) << endl;
return median;
}
double calcMean(int intArray[], int counter){
double mean = 0;
///calculate the mean of the numbers
for(int i=0; i<counter; i++){
mean += intArray[i];
}
mean= mean/counter;
cout << "Median = " << calcMedian(intArray, counter) << endl;
return mean;
}
void bubbleSort(int intArray[], int counter){
int i, j, temp;
for (i = 0; i < counter-1; i++)
for (j = 0; j < counter-i-1; j++)
if (intArray[j] > intArray[j+1]){
temp = intArray[j];
intArray[j] = intArray[j+1];
intArray[j+1] = temp;
}
cout << "Mean = " << calcMean(intArray, counter) << endl;
}
void fillArray (string input, int intArray[], int counter){
int i,j, temp;
std::istringstream ss(input);
int inum=0;
for( i=0; i<counter; i++){
ss>>inum;
intArray[i]=inum;
}
///sort the numbers in the array smallest to largest
for( i=1; i< counter; i++){
for( j=0; j< counter-i; j++){
if(intArray[j]>intArray[j+1]){
temp=intArray[j];
intArray[j]=intArray[j+1];
intArray[j+1]=temp;
}
}
}
bubbleSort(intArray, counter);
}
int countInt (string input){
//convert string into stringstream and count the number of integers
int inum=0, counter = 0;
std::istringstream ss(input);
while(ss>>inum){
counter++;
}
///clear the flags on the string stream and refill with the input string
ss.clear();
ss.str(input);
int intArray[counter];
fillArray (input, intArray, counter);
return counter;
}
void readFile(string *input){
///take a set of numbers as string input
std::cout<<"Enter a set of numbers separated by spaces for analysis:"<<std::endl;
getline(std::cin, *input);
int counter = countInt(*input);
}
int main()
{
string filename = "input.txt";
readFile(&filename);
return 0;
}

**Comment for any queries. Happy to help ...:)