Question

In: Computer Science

How do I create this program? Using C++ language! Write a program that reads data from...

How do I create this program? Using C++ language!

Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL INES OF THE PROGRAM MUST BE COMMENTED.

Solutions

Expert Solution

readvalues.txt (save this file under D Drive.Then the path of the file pointing to it is D:\\readvalues.txt)

98
93
72
86
99
61
89
77
91
73
79
71
86
74
19

______________________

#include <iostream>
#include <fstream>
#include <iomanip> // std::setprecision
#include <cmath>
using namespace std;

//Declaring global variables
double mean,standard_deviation;

//Function declarations
void calAvg(int grades[],int count);
void standardDeviation(int grades[],int count);

int main()
{
//defines an input stream for the data file  
ifstream dataIn;
  
//Declaring variables
int nos,count=0;
  
//Opening the input file
dataIn.open("D:\\readvalues.txt");
  
while(dataIn>>nos)
{
   count++;
}
  
//Closing the data input stream
dataIn.close();
  
//Create an integer array which is of size based on count value
int grades[count];
  
//Opening the input file
dataIn.open("D:\\readvalues.txt");
  
/* Getting the values from the txt file and
* populate those values into grades[] array
*/
for(int i=0;i<count;i++)
{
   dataIn>>nos;
   grades[i]=nos;
}
dataIn.close();
  
//Calling the function by passing the grades[] array as argument
calAvg(grades,count);
standardDeviation(grades,count);
  
//Setting the precision to Two decimal places
std::cout << std::setprecision(2) << std::fixed;

//displaying the mean and standard deviation
cout<<"Mean is "<<mean<<endl;
cout<<"Standard Deviation is "<<standard_deviation<<endl;
     
return 0;
}
//Function implementation which calculates the average value of grades[] array
void calAvg(int grades[],int count)
{
   //Declaring local variables
   double sum=0.0;
  
   //calculating the sum of grades[] array elements
   for(int i=0;i<count;i++)
   {
       //calculating the sum
       sum+=grades[i];
   }
  
   //calculating the average
   mean=sum/count;
  
}
//This function which claculates the standard deviation
void standardDeviation(int grades[],int count)
{
   int sum=0;
   double variance;
   //This loop Calculating the sum of square of eeach element in the deviation[] array
   for(int i=0;i<count;i++)
   {
   //Calculating the sum of square of eeach element in the deviation[] array  
   sum+=pow(grades[i]-mean,2);
   }
   //calculating the standard deviation of grades[] array
   variance=(double)sum/(count-1);
   standard_deviation=sqrt(variance);
}
  

___________________

output:

____________Thank You


Related Solutions

Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the...
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the first quarter of y consists of the value of the first element of x. The second quarter of y consists of the value of the second element of x. The third quarter of y consists of the value of the third element of x. The fourth quarter of y consists of the value of the fourth element of x?
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT