This project is for a computer science 1 course in c++. I need to complete numbe
ID: 3807187 • Letter: T
Question
This project is for a computer science 1 course in c++. I need to complete number 4. Need to create a program that calls a function to read in a text file. The file I need to use is called data.txt and it contains these numbers. How do you create this file too?
The program then needs to call a function that will calculate the standard deviation of all the numbers that were in the data.txt file. Finally, the program has to allow the user to enter the name of the file. Allow the user to make up to 3 mistakes when typing in the file before ending the program. Thanks!
e, course sylabus cs -1511 x HW7.pdf x C XM course: MATH 3298 x Spike Ball Gamecombo x M Textbooks Invitation to x & Textbooks Google Dri C Secure I https drive.google.com/ You may assume that the partially filled array contains only lowercase letters. Embed your function in a suitable test program. 4. The standard deviation of a list of numbers is a measure of how much the numbers deviate from the average. If the standard deviation is small, the numbers are clustered close to the average. If the standard deviation is large, the numbers are scattered far from the average. The standard deviation, S, of a list of N numbers x is defined as follows: where x is the average of the N numbers x1, x2, Define a function that takes a partially filled array of numbers as its arguments and returns the standard deviation of the numbers in the partially filled array. Since a partially filled array requires two arguments, the function will actually have two formal parameters: an array parameter and a formal parameter of type int that gives the number of array positions used. The numbers in the array will be of type double. Embed your function in a suitable test program. a Ask me any A 2:14 AM 3/29/2011 20Explanation / Answer
#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>
using namespace std;
double calculateMean(int arr[], int n)
{
double total = 0;
for(int i = 0; i < n; i++)
{
total += arr[i];
}
return total/n;
}
double calculateStandardDeviation(int arr[], int n)
{
double mean = calculateMean(arr, n);
double variance = 0;
for(int i = 0; i < n; i++)
{
variance += (arr[i] - mean)*(arr[i] - mean);
}
variance = variance/n;
double standardDeviation = sqrt(variance);
return standardDeviation;
}
int readFileInArray(int arr[], char filename[])
{
ifstream myfile(filename);
int i = 0;
while(!myfile.eof() && i < 100)
{
myfile >> arr[i];
i++;
}
return i;
}
int main()
{
int data[100];
char filename[100];
int i;
for(i = 0; i < 3; i++)
{
cout << "Enter filename: ";
cin >> filename;
if (strcmp(filename, "data.txt") != 0)
{
cout << "Enter file name correctly. Entered '" << filename<<"'"<< endl;
}
else
{
break;
}
}
if (strcmp(filename, "data.txt") != 0)
{
cout << "All attempts exhausted. Exiting" <<endl;
return 1;
}
int n = readFileInArray(data, filename);
cout << "Standard deviation is: " << calculateStandardDeviation(data, n) << endl;
return 0;
}
Sample run
Enter filename: data.txt
Standard deviation is: 27.9194
Please note there is a hard assumption that you want file name to be data.txt only and thats what you want user to enter as otherwise what mistake user is allowed to make three time is not clear.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.