Question

In: Computer Science

Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to...

Write a program in C++

Write three new functions, mean() and standard_deviation(). Modify your code to call these functions instead of the inline code in your main().

In addition, add error checking for the user input. If the user inputs an incorrect value prompt the user to re-enter the number.

The flow of the code should look something like:

/**
 Calculate the mean of a vector of floating point numbers
 **/
float mean(vector& values)

/**
 Calculate the standard deviation from a vector of floating point numbers
 **/
float standard_deviation(vector& values)

/**
 Main
 **/
int main()
{
    /**
     Prompt the user and get the number of values to enter
     **/
    
    /**
     Get the values from the user
     **/
    
    /**
     Display the mean and standard deviation
     **/
    
    return 0;
}

Solutions

Expert Solution

taking the condition only floating point number are using in the vector so the code is as follows

// including required libraries
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
// Calculate the mean of a vector of floating point numbers
void mean(vector<float> vect)
{
   float sum,mean;
   int n=vect.size();
for(int i=0;i<n;i++)
{
       sum = sum+vect[i];
   }
   mean=sum/n;
   cout<< "\nmean of the given vector is: "<< mean<<"\n";
}
// Calculate the standard deviation from a vector of floating point numbers
void standard_deviation(vector<float> vect)
{
   float sum = 0.0, mean, variance = 0.0,var, sd;
      
       int n=vect.size();
       for(int i=0;i<n;i++)
       {
               sum = sum+vect[i];
       }
       mean=sum/n;
       for(int i=0;i<n;i++)
       {
           variance = variance+pow(vect[i]-mean,2)   ;
       }
       var=variance/n;
       sd=sqrt(var);
       cout<< "\nStandard deviation of the given vector is: "<< sd<<"\n";
}
int main()
{
   //intializing variables
   int n,i;
   float a;
   float b=4.5656;
   //intializing vector
vector<float> vect;
//taking length of the vector
cout<< "Enter length of the vector: ";
cin>>n;
// taking values into the vector
cout<< "Enter values in the vector\n";
for(i=0;i<n;i++)
{
       cout<< "vect["<<i<<"]= ";
           cin>>a;
               if(floor(a)-a==0)// checking the given number is float or not
               {
                   cout<< "please enter valid float number : ";   //error condition if not float
                   cin>>a;
                   vect.push_back(a);
               }
               else{
           vect.push_back(a);
               }
   }   
   //calling function mean
   mean(vect);
   //calling function standard deviation
   standard_deviation(vect);
return 0;
}

OUTPUT

also uploading images of code in case of any error in written code please go through the code in images

any queries comment please:)


Related Solutions

C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A function to get principal, rate, and term. A function to calculate monthly payment. Test your program by getting the data from the user by using the first function and calculate monthly payment by calling the other function. Add version control and write proper comments with a few blank lines & indentations in order to improve your programming style.
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Code in C Write a program whose inputs are three integers, and whose outputs are the...
Code in C Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. Ex: If the input is: 7 15 3 the output is: largest: 15 smallest: 3 Your program must define and call the following two functions. The LargestNumber function should return the largest number of the three input values. The SmallestNumber function should return the smallest number of the three input values. int...
write a C++ program to CREATE A CLASS EMPLOYEE WITH YOUR CHOICE OF ATTRIBUTES AND FUNCTIONS...
write a C++ program to CREATE A CLASS EMPLOYEE WITH YOUR CHOICE OF ATTRIBUTES AND FUNCTIONS COVERING THE FOLLOWING POINTS: 1) COUNTING NUMBER OF OBJECTS CREATED ( NO OBJECT ARRAY TO BE USED) USING ROLE OF STATIC MEMBER 2) SHOWING THE VALID INVALID STATEMENTS IN CASE OF STATIC MEMBER WITH NON STATIC MEMBER FUNCTION, NON STATIC MEMBERS OF CLASS WITH STATIC MEMBER FUNCTION, BOTH STATIC. SHOW THE ERRORS WHERE STATEMENTS ARE INVALID. 3) CALL OF STATIC MEMBER FUNCTION, DECLARATION OF...
C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT