In: Computer Science
in C++ .....For this assignment you will calculate the average of a series of integers. You will use a while loop to repeatedly ask the user to enter a value until the user enters the sentinel value. Program results will be output to an external text file. Average of Ints Write a program that calculates and prints the average of several integers. Assume that last value read is the sentinel 9999. A typical input sequence might be: 10 8 11 7 9 9999 indicating that the average of all the values preceding 9999 is to be calculated. You will need define/declare variables to hold the following: • The value the user enters • The total (an accumulator to tally the total) • The average • The count (a counter to keep track of how many values the user has entered) • A constant value – 9999 (a sentinel that will serve as the value that stops the loop) Most, if not all variables will need to be initialized to a starting value for your program to produce true results. Define ALL variables at the top of your program. In addition, you will also need to define/declare an “output” filestream so you can send output to an external text file. You perform traditional I/O via the console but also “echo” output ONLY to the external file. Your external file will serve as a record of all your program runs................
This program asks for a series of input integers to be given and the average of those integers will be calculted and written as output to the output file.
Explanation of logic of the program:
Since we have to output the average to an output file, we're including fstream header file which allows us to create file objects for opening, writing and reading data. For this problem, an ofstream object is created to open a file in write mode as we are not reading anything from file. Make sure to open the file in append mode so as to save all the averages in the file for each time the program runs. Due to append mode, the file serves as a record of all our executions.
#define SENTINEL 9999 is used as a stopping condition to the input. If sentinel needs to be any other integer it can be changed in this area, so wherever SENTINEL is used, it is using the data defined.
The user can input as many numbers till the sentinel is entered. If the sentinel is entered, it breaks from thee loop. For each character entered it checks if it matches to sentinel, if not it is added to the sum variable and count is incremented to keep track of the number of variables entered.
Later, the average is calculated (sum/numbers) and is written to output file. Since the file is opened in append mode, it can save the averages of subsequent runs as well.
#include<iostream>
#include<fstream>
#define SENTINEL 9999 //sentinel can be changed here if there's a need to use another one
using namespace std;
int main(){
int val,sum=0,count=0;
float avg;
ofstream fout;
fout.open("outputfile.txt", ios::app); //opens file in write and append mode
while (1)
{
cout<<"Enter numbers:(enter sentinel to terminate):";
cin>>val;
if(val==SENTINEL){
break; //if sentinel is entered, it breaks from the loop
}
else{
sum+=val; //calculates sum
count++; //increments count to keep track of numbers entered
}
}
avg=(float)sum/(float)count; //calculates average
cout<<"Writing avg to file";
fout<<avg<<endl; //writes average to file
fout.close(); //closing file object
return 0;
}
Hope this solution helps :)