Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am attempting to complete number 4. This is for a c++ course. This is the code

ID: 3807859 • Letter: I

Question

I am attempting to complete number 4. This is for a c++ course. This is the code I have so far. The program is suppose to be able to get the file data.txt and use the values in this file in the program. I created a data.txt file but everytime I run the program it says there is a segmentation error. I am not sure on the exact way to create the data.txt file. The program then calculates the standard deviation of all the elements in the file data.txt . Below the code I have is the data.txt file that I created. Do you see where my error is? Please help.

#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;
while(!myfile.eof())
{
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;
}

This is the data.txt file i created. I just created a new file called data.txt and pasted this into it. I dont know if this is the reason there is an error? The program takes in the data.txt file then states segmentation error. I am lost, please help.

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 20

Explanation / Answer

I tried running this code and running fine.

// link for online code https://goo.gl/t8n7iT

sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter filename: data.txt
Standard deviation is: 27.9194

#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; // this need initialisation else it can cause array overflow
while(!myfile.eof() && i < 100) // as array is declared of size 100 hence only 100 entries should we read. If you need flexibility of number of entries use vector.
{
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;
}