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

Teacher\'s Instructions In each project, you should open the files using any pat

ID: 3887420 • Letter: T

Question

Teacher's Instructions

In each project, you should open the files using any paths to folders but remember that on a Windows system, you must use the '' character when specifying directories. If you copy the input file to the same directory as your project executible, you can simply open statements using the following:

cin >> filename; // where the user enters "hw4pr1input.txt"

inputFileStream.open(filename);

That means the input and output files need to be in the same folder as your compiled and linked executable code (your EXE file). That will vary by compiler, but it is not difficult to place your files in the correct folder. I don't want to see paths to folders on your computer, those paths will not work on mine when I go to grade your homework. If I have to modify your open statements or if I have to answer prompts for file names I will count off.

This shows the results for the input data file provided by the Homework Assignment. We simply need to read the values from the file, adding them up and keeping track of how many values, then divide to get the average of the values. I want to see that average value and the count of how many values are in the file. Your program should work with any number of data values in the file, including none at all (review page 353 in our text).

The word, average, is a bit ambiguous. We will use the arithmetic mean, i.e. sum/count, for the average since this is what the author intends. Other possibilities are median, or middle value (a number for which as many data are larger as are smaller) and the mode (the most frequent, or the value representing the interval containing the most items.)

Question from book....

1.Write a C++ program that takes its input from a file of numbers of type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of type double separated by blanks and/or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. (hw4pr01input.txt is file name)

Explanation / Answer

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

int n;

ifstream Input;   

Input.open("c: umbers.txt"); //opeingn the fike

if(Input.fail( )) //condition if file open fails

{

cout << "error opening input file"<<endl;

return 1;

}

  

  

int c=0;

while(!Input.eof()) //reading the numbers

{

Input>>n;

sum=sum+n; //calculating the sum

++c;

}

double avg=0.0;

avg=sum/c; //calculating the average

cout<<"The average is: "<<avg; //displaying the result.

Input.close();

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