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

Write a program in C++ that does the following: Read an array of numbers from a

ID: 3766606 • Letter: W

Question

Write a program in C++ that does the following:

Read an array of numbers from a text file (ask the user for the file name).

Calculate their average and standard deviation.

Then print the array followed by the average and standard deviation.

Your program should have the following functions:

findaverage() - To find the average

finddev() - To find the standard deviation and the following subroutines:

readarray() - To get the file name and read the numbers from the array

writeresults() - To display the results on the screen

Make sure to use good programming style (i.e., comments, indentation, etc.)

Explanation / Answer

#include<iostream>

#include<string.h>

#include<fstream>

#include<math.h>

using namespace std;

double findaverage(int arr[], int len) {

double sum=0;

for (int i=0; i<len; i++) {

sum+=arr[i];

}

return sum/len*1.0;

}

void readarray(ifstream &file, int *arr, int &count) {

if (file) {

while (!file.eof()) {

cin>>arr[count++];

}

}

}

double finddev(int arr[], int len, double avg) {

double temp=0;

for (int i=0; i<len; i++) {

temp+=(arr[i] - avg)*(arr[i] - avg);

}

temp = temp/len;

return sqrt(temp);

}

void writeresults(double avg, double dev) {

cout<<"Average is: "<<avg<<" ";

cout<<"Standard deviation is: "<<dev<<" ";

}

int main() {

int arr[100];

int count=0;

string fileName;

cout<<"Enter file name: ";

cin>>fileName;

  

ifstream file;

file.open(fileName);

  

readarray(file, arr, count);

double avg = findaverage(arr, count);

double dev = finddev(arr, count, avg);

writeresults(avg, dev);

  

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote