Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with rearranging easy c++ code #include ///cin, cout #include /// istringstream #include /// pow, sqrt int main() { std::string input; int counter=0; int inum=0;

Help with rearranging easy c++ code #include  ///cin, cout #include /// istringstream #include  /// 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:"<>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>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; imaxi){ 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 

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

Is the person willing to deal with the consequences?

Answered: 1 week ago

Question

Was there an effort to involve the appropriate people?

Answered: 1 week ago