Question

Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...

Fix the following program (C++).

#include <iostream>
#include <cmath>
#include <vector>
#include <limits>
using namespace std;


/*
 * Calculate the square of a number
 */
float square (float x)
{
        return x*x;
}

/*
 * Calculate the average from a list of floating point numbers
 */
float average(vector<float>& values)
{
        float sum = 0.0f;
        float average;

        for (float x : values)
                sum += x;
        average = sum / values.size();
        return average;
}

/**
 Calculate the standard deviation from a vector of floats
 1) Calculate the sum of the squares of the differences between dataSet[i]
    and the mean using the square function.
 2)Divide this result, diffSquared, by (values.size() - 1)
 3)Take the square root of the result
 **/
float stddeviation(vector<float>& values)
{
        float diffSquared = 0.0f;
        float stdDev;

        /*Do a sum of the squares of the differences over the range of
        vector values*/
        for (float x : values)
        {
                diffSquared += square(x - average(values));
        }
        diffSquared /= values.size() - 1;
        stdDev = sqrt(diffSquared);

        return stdDev;
}

/*
 * Main
 */
int main()
{
        int valueCount; //number of vector values


/////////////////////User input: get number of values and the actual values/////////////////////////////////////
        /*
         * Prompt the user and get the number of values to enter
         */

        cout << "Please enter the amount of values to be entered >";

        vector<float> dataSet(valueCount); // initialize the vector with the given data size
        //Get user input values. Error check for non-numbers.
        //Run the for loop for the size of the vector dataSet to avoid out-of-bounds errors
        for (vector<double>::size_type i = 0; i < valueCount; i++)
        {
                cout << "Please enter the values, one at a time > ";

                //Check for valid input, loop until valid
                while ( !(cin >> dataSet[i]) )
                {
                        cin.clear(); //clear input
                        cin.ignore(numeric_limits<streamsize>::max(), '\n');
                        cerr << "Input Error: non-numeric input. Please enter only numbers.";
                }
        }
///////////////////////////////End Value Input/////////////////////////////////////////////////////////////////

////////////////////////////////////////////////Computations///////////////////////////////////////////////
        /*
         * Calculate and display the average of the data using function average()
         */
        cout << "\nThe average of the entered values is " << average(dataSet) << ".";

        /*
         * Calculate and display the standard deviation of the data using stddeviation()
         */
        cout << "\nThe standard deviation of the data is " << stddeviation(dataSet) << ".\n";

        return(0);
}
0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • C++ Standard Deviation with arrays and vectors: #include <vector> #include <cmath> #include <iostream> using namespace std;...

    C++ Standard Deviation with arrays and vectors: #include <vector> #include <cmath> #include <iostream> using namespace std; void fillVector(vector <double> &); double average(const vector <double> &); int main() { return 0; } void fillVector(vector <double> &v) { double d; while (cin >> d) { v.push_back(d); } } double average(const vector <double> &v) { double sum = 0.; for (int i = 0; i < v.size(); i++) { sum += v.at(i); } return sum / v.size(); } standardDeviation() //stuck on this part...

  • #include <iostream> #include <math.h> using namespace std; float f1(int low,int up) { int i,total=0.0; for(i=low;i<=up;i++) {...

    #include <iostream> #include <math.h> using namespace std; float f1(int low,int up) { int i,total=0.0; for(i=low;i<=up;i++) { total=total+(i*i+i*1.5+4); } return total; } float f2(int low,int up) { int i,total=0.0; for(i=low;i<=up;i++) { total=total+(i*i+i*1.5-4); } return total; } int main() { int low,up; cout << "This program calculates the area under a curve between two points on the x axis\n"; cout<< "\nThe FIRST equation is: f(x)=x^2 +1.5x +4"; cout<< "\nThe SECOND equation is: f(x)=x^2 +1.5x -4"; cout << "\nEnter x's minimum value :...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0,...

    Flow chart of this program #include <iostream> #include <cmath> using namespace std; int mainO double sum=0.0, ave-ee, int locmx, locmn int n; cout <<"Enter the number of students: "<<endl; cin >> ni double listln]; double max-0; double min-e; for (int i-e ; i<n ;i++) cout<s enter the gpa: "<cendli cin>>listli]; while (listlile i listli1>4) cout<s error,Try again "<cendl; cin>listlil: sun+=list[i]; N1 if (listli]>max) for(int isin itt) max=list [i]; 10cmx=1+1 ; else if (list [i] min) min=list [i]; locmn-i+1; ave sum/n;...

  • // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); //...

    // PLACE YOUR NAME HERE #include <iostream> using namespace std; float findAverage (int [], int); // finds average of all //grades int findHighest (int [], int); // finds highest of all //grades int findFirstFail( int[]); int main() { int grades[100]; // the array holding 100 grades. int numberOfGrades; // the number of grades read. int pos; // index to the array. float avgOfGrades; // contains the average of the grades. int highestGrade; // contains the highest grade. int inderOfFail; //...

  • C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; }...

    C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; } bool checkinventoryprice(float price) {    return price > 0; } void endProgram() {    system("pause");    exit(0); } void errorID(string str) {    cout << "Invalid Id!!! " << str << " should be greater than 0" << endl; } void errorPrice(string str) {    cout << "Invalid Price!!!" << str << " should be greater than 0" << endl; } int inputId() {...

  • Edit this C++ code to show the output as well. #include<iostream> #include<cstring> using namespace std; class...

    Edit this C++ code to show the output as well. #include<iostream> #include<cstring> using namespace std; class Product { private:    // private variables    int id_number;    char pDescription[40];    int mId;    double productPrice;    double productMarkUp;    int qty; public:    // constructor    Product() {        id_number = 0;        strcpy_s(pDescription, "");        mId = 0;        productPrice = 0.0;        productMarkUp = 0.0;        qty = 0;    }   ...

  • Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std;...

    Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std; int read_function(int array[], int, int); int main() { int array[300], numba; read_function(array[300]); cout << "Enter a whole number between 2-20: " << endl; cin >> numba; read_function(numba); return 0; } int read_funtion (int arr[300], int num, int Values) { int sum=0; ifstream array_file; array_file.open("Lab8.dat"); for(int i=0; i < 300; i++) {   array_file >> arr[i];   cout << arr[i];   sum += i; } cout << sum;...

  • Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n)...

    Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n) {     vector <int> v1;     // Print the number of 2s that divide n     while (n%2 == 0)     {         printf("%d ", 2);         n = n/2;         v1.push_back(2);     }     // n must be odd at this point. So we can skip     // one element (Note i = i +2)     for (int i = 3; i <=...

  • Arrays C++ #include <iostream> using namespace std; int main() {       int   tests[6]; // array declaration...

    Arrays C++ #include <iostream> using namespace std; int main() {       int   tests[6]; // array declaration       int   sum = 0;       float avg;       //input test scores       cout << " Enter " << 6 << " test scores: " << endl;       for (int i = 0; i < 6; i++)       {             cout << "Enter Test " << i + 1 << ": ";             cin >> tests[i];       }       return 0; }    Type...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT