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

C++ Program. The attached text data file contains an unknown quantity of integer

ID: 3766695 • Letter: C

Question

C++ Program.

The attached text data file contains an unknown quantity of integers. Write a program that opens the file, counts how many values it contains, and creates a 1D array to store the values. Once the array is populated, calculate the minimum value, maximum value, average, and standard deviation. Also count how many values are greater than the average, and of those values, how many are even. Output all the results (including the count of how many values there are) to a separate text file.

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, int min, int max, int count) {

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

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

cout<<"Minimum is: "<<min<<" ";

cout<<"Maximum is: "<<max<<" ";

cout<<"Number of values: "<<count<<" ";

}

int findMin(int *arr, int &count) {

int min=arr[0];

for (int i=1; i<count; i++) {

if (min > arr[i]) {

min=arr[i];

}

}

return min;

}

int findMax(int *arr, int &count) {

int max=arr[0];

for (int i=1; i<count; i++) {

if (max < arr[i]) {

max=arr[i];

}

}

return max;

}

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, findMin(arr, count), findMax(arr, count), count);

  

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