glsaRebatn/cs109-Practice-Exam-02.pdf?dl-0 cs109-Practice-Exam-02.pdf 4) There e
ID: 3723314 • Letter: G
Question
glsaRebatn/cs109-Practice-Exam-02.pdf?dl-0 cs109-Practice-Exam-02.pdf 4) There exists an input file containing real numbers, one contains at most 1000 real numbers, but typically less. The task is to write a C++ program that inputs this data into a 1D array and then outputs the average (as a real number). Your job is to write the main() function by per line. The file name is "dataset.txt", and the file calling the provided InputData() and ComputeSum() functions. Write only the main() function, assume the other functions have already been written for yousee the comment inside each function for the tasks they perform. Your main() function is responsible for all other operations #include #include #include #include using namespace std; int InputData(string filename, double data[ ) // opens filename, inputs data into array, returns # of values input: double ComputeSum(double data[], int N) /I returns sum of first N values in the array: int main()Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<stdlib.h>
#define MAX 1000
using namespace std;
// Read in the floating numbers from file and store it in numbers array passed as parameter
int InputData(string fileName, double data[])
{
// Counter for number of floating numbers in the file
int counter = 0;
// File stream object declared
ifstream fRead;
// Open the file given in fileName for reading
fRead.open(fileName.c_str());
// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!fRead.is_open())
{
// Displays error message
cout<<" Error: Unable to open the file!"<<fileName;
// Exit the program
exit(0);
}// End of if condition
// Loops till not end of the file
// eof() function returns true if the stream's eofbit error state flag is set (which signals that the End-of-File has been reached
// by the last input operation). Otherwise returns false.
while(!fRead.eof())
{
// Read the contents of file and stores it in the numbers array counter index position
fRead>>data[counter];
// Increase the counter value by one
counter++;
}// End of while condition
// Displays the numbers read from file
cout<<" Numbers in file ";
// Loops till numbers of numbers in file
for(int x = 0; x < counter; x++)
// Displays 10 numbers per line
if(x % 10 == 0)
cout<<" "<<data[x]<<", ";
else
cout<<data[x]<<", ";
// Close the file
fRead.close();
// Returns number of numbers in the file
return counter;
}// End of file
// Function to calculate and return sum of N numbers
double ComputeSum(double data[], int N)
{
double sum = 0.0;
// Loops till length of the array N
for(int x = 0; x < N; x++)
// Calculates sum
sum += data[x];
// Returns sum
return sum;
}// End of file
// main function definition
int main()
{
// Creates an array of size MAX
double data[MAX];
// Calls the function to read numbers from file dataset.txt and stores it in array data
// Stores the return value length in len
int len = InputData("dataset.txt", data);
// Calls the function to calculate sum and display the return sum value
cout<<" Sum of "<<len<<" numbers: "<<ComputeSum(data, len);
}// End of file
Sample Output:
Numbers in file
12.2, 10.1, 45.1, 1.2, 5.4, 2.2, 1.4, 40.3, 1.22, 50.41,
11.5, 11.14, 5.1, 81.4, 5.4, 10.7, 14.4, 40.6, 1.24, 5.44,
42.2, 80.4, 55.7, 1.24, 76.4, 1.9,
Sum of 26 numbers: 614.29
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.