Question

In: Computer Science

in C++ .....For this assignment you will calculate the average of a series of integers. You...

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................

Solutions

Expert Solution

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 :)


Related Solutions

For this assignment, write a program that will calculate the quiz average for a student in...
For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of quiz...
For this assignment, write a program that will calculate the quiz average for a student in...
For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of quiz...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of quiz...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of...
OverviewFor this assignment, write a program that will calculate the quiz average for a student in...
OverviewFor this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array.For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores).It is recommended that this program be written in two versions. The first version of the program will read a set of quiz scores from...
MUST BE DONE IN C (NOT C++) In this program we will calculate the average of...
MUST BE DONE IN C (NOT C++) In this program we will calculate the average of x students’ grades (grades will be stored in an array). To do so, please follow these guidelines: - Your program should ask the user for the number of students that are in the class. This number should help you declare your array. - Use the function seen in class to scan the grades of the array. In other words, we will populate the array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT