Rewrite the program by grouping the code into functions. Change the program so i
ID: 3744419 • Letter: R
Question
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.
#include <iostream> ///cin, cout
#include <sstream>/// istringstream
#include <cmath> /// pow, sqrt
int main()
{
std::string input;
int counter=0;
int inum=0;
double mean=0.0;
double mode=0.0;
double median=0.0;
double stddev=0.0;
int i,j,temp;
///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);
///convert string into stringstream and count the number of integers
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);
///create array of size counter(the number of integers) and fill it from the string stream
int intArray[counter];
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;
}
}
}
///calculate the mean of the numbers
for(i=0; i<counter; i++) {
mean += intArray[i];
}
mean= mean/counter;
///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)];
}
///calculate mode of the numbers
temp=intArray[0];
int mocount=1;
int maxi=1;
mode=temp;
bool 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];
}
}
///calculate the standard deviation
double sum=0.0;
double avg=0.0;
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));
///print the results
std::cout<<"Mean: "<<mean<<std::endl;
std::cout<<"Median: "<<median<<std::endl;
if(modefound) {
std::cout<<"Mode: "<<mode<<std::endl;
}
else {
std::cout<<"Mode: A single mode may not exist"<<std::endl;
}
std::cout<<"Standard Deviation: "<<stddev<<std::endl;
return 0;
}
Explanation / Answer
/***********************
ADDED FUNCTIONS
Do give a feedback. It's helpful to us.
*************************/
#include <iostream> ///cin, cout
#include <sstream> /// istringstream
#include <cmath> /// pow, sqrt
#include <string>
#include <fstream>
void readFile(std::string &s) {
std::ifstream f("input.txt");
getline(f, s);
}
void fillArray(std::string s, int *arr, int size) {
int n;
std::istringstream ss(s);
for (int i = 0; i < size; i++) {
ss >> n;
arr[i] = n;
}
}
void bubbleSort(int *arr, int size) {
///sort the numbers in the array smallest to largest
int temp;
for (int i = 1; i < size; i++) {
for (int j = 0; j < size - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
double calcMean(int *intArray, int counter) {
///calculate the mean of the numbers
int mean = 0;
for (int i = 0; i < counter; i++) {
mean += intArray[i];
}
return mean / (double) counter;
}
double calcMedian(int *intArray, int counter) {
///calculate the mean of the numbers
double median = 0;
///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)];
}
return median;
}
double calcMode(int *intArray, int counter, bool &modefound) {
///calculate mode of the numbers
double temp = intArray[0];
int mocount = 1, i;
int maxi = 1;
double mode = temp;
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) {
///calculate the standard deviation
double sum = 0.0;
double avg = 0.0;
double stddev = 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;
}
int main() {
std::string input;
int counter = 0;
int inum = 0;
double mean = 0.0;
double mode = 0.0;
double median = 0.0;
double stddev = 0.0;
int i, j, temp;
bool modefound = false;
///take a set of numbers as string input
readFile(input);
///convert string into stringstream and count the number of integers
std::istringstream ss(input);
while (ss >> inum) {
counter++;
}
///create array of size counter(the number of integers) and fill it from the string stream
int intArray[counter];
fillArray(input, intArray, counter);
bubbleSort(intArray, counter);
mean = calcMean(intArray, counter);
median = calcMedian(intArray, counter);
mode = calcMode(intArray, counter, modefound);
stddev = calcStddev(intArray, counter);
///print the results
std::cout << "Mean: " << mean << std::endl;
std::cout << "Median: " << median << std::endl;
if (modefound) {
std::cout << "Mode: " << mode << std::endl;
} else {
std::cout << "Mode: A single mode may not exist" << std::endl;
}
std::cout << "Standard Deviation: " << stddev << std::endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.