1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
stellarik [79]
3 years ago
12

Write a program that will open the file random.txt and calculate and display the following: A. The number of numbers in the file

. B. The sum of all the numbers in the file. C. The average of all the numbers in the file. You must determine how many numbers are in the file by searching for the end of the list of numbers. You are not allowed to hard code the expected number of numbers into your code. The file random.txt is located in the Lab 4 Canvas Module. Here is the result you should get: Number of numbers: 200 Sum of the numbers: 105527 Average of the numbers: 527.635

Computers and Technology
1 answer:
Rama09 [41]3 years ago
5 0

Answer:

Here is the C++ program:

#include <iostream>  //to use input output functions

#include <fstream>  //to manipulate files

using namespace std;  //to identify objects like cin cout

int main(){  //start of main function

  ifstream file;   //creates an object of ifstream

   file.open("random.txt"); //open method to open random.txt file using object file of ifstream

       

    int numCount = 0;  //to store the number of all numbers in the file          

    double sum = 0;   //to store the sum of all numbers in the file

    double average = 0.0;   //to store the average of all numbers in the file

    int number ; //stores numbers in a file

                 

        if(!file){   //if file could not be opened

           cout<<"Error opening file!\n";    }   //displays this error message

       

       while(file>>number){   //reads each number from the file till the end of the file and stores into number variable

           numCount++; //adds 1 to the count of numCount each time a number is read from the file          

           sum += number;  }  //adds all the numbers and stores the result in sum variable

           average = sum/numCount;  //divides the computed sum of all numbers by the number of numbers in the file

     

       cout<<"The number of numbers in the file: "<<numCount<<endl;  //displays the number of numbers

       cout<<"The sum of all the numbers in the file: "<<sum<<endl;  //displays the sum of all numbers

       cout<<"The average of all the numbers in the file: "<< average<<endl;  //displays the average of all numbers

       file.close();     }  //closes the file    

   

Explanation:

Since the random.txt is not given to check the working of the above program, random.txt is created and some numbers are added to it:

35

48

21

56

74

93

88

109

150

16

while(file>>number) statement reads each number and stores it into number variable.

At first iteration:

35 is read and stored to number

numCount++;  becomes

numCount = numCount + 1

numCount = 1      

sum += number; this becomes:

sum = sum + number

sum = 0 + 35

sum = 35

At second iteration:

48 is read and stored to number

numCount++;  becomes

numCount = 1+ 1

numCount = 2    

sum += number; this becomes:

sum = sum + number

sum = 35 + 48

sum = 83

So at each iteration a number is read from file, the numCount increments to 1 at each iteration and the number is added to the sum.

At last iteration:

16 is read and stored to number

numCount++;  becomes

numCount = 9 + 1

numCount = 10    

sum += number; this becomes:

sum = sum + number

sum = 674 + 16

sum = 690

Now the loop breaks and the program moves to the statement:

       average = sum/numCount;  this becomes:

       average = 690/10;

       average = 69

So the entire output of the program is:

The number of numbers in the file: 10                                                                                                           The sum of all the numbers in the file: 690                                                                                                     The average of all the numbers in the file: 69

The screenshot of the program and its output is attached.

You might be interested in
How to drive more website traffic?
lidiya [134]

Hello there,

How to drive more website traffic?

Answer: Advertise

8 0
3 years ago
Amy would like to find all records that were delivered on August 8, 2008. Her database has a field that tracks when the items we
denpristay [2]
Conduct a query and set the criteria as = August 8, 2008. I'm not sure if this is the right answer, but that's what I think it is.
7 0
3 years ago
Read 2 more answers
The students of a college have to create their assignment reports using a word processing program. Some of the questions in thei
asambeis [7]
The insert table function of a word processing program will be the most useful for comparison.
7 0
3 years ago
Read 2 more answers
Having knowledge of the main parts of a computer: CPU, ALU, INPUT, OUTPUT ,and MEMORY Can you explain in simple for dummies what
Morgarella [4.7K]

Answer:

Explanation:

Great question, it is important to ask these questions in order to get rid of any doubts you may be having.

I will explain this as simply as possible in separate parts.

CPU: the CPU is the brain of the computer where all the information is received, processed, and sent from.

ALU: are the digital circuits inside the CPU used to perform all the arithmetic and logic operations

INPUT: are the commands given to the computer by the user.

OUTPUT: is the information displayed on the screen by the computer.

Memory: is the piece of hardware used to save all the information that is currently being used by the computer.

I hope this answered your question. If you have any more questions feel free to ask away at Brainly.

5 0
2 years ago
What does , , and in HTML means?
zloy xaker [14]

< b > makes the text bold.

<u>< u > makes the text underlined.</u>

<em>< i > makes the text italicized.</em>

(In your question, it didn't show these code because the codes were affected already).

(Mark this as the brainliest if you think this helps!)

8 0
3 years ago
Other questions:
  • NEED HELP ASAP!
    13·1 answer
  • Which of the following information is okay to share on social networking site?
    11·2 answers
  • Write thanks to the IT teacher at the end of grade 5
    7·1 answer
  • A sentinel value ________ and signals that there are no more values to be entered:____
    14·1 answer
  • What type of ransomware was developed to block the user from accessing the computer and encrypts all the files on the user's dev
    5·2 answers
  • How do I write my name in binary code?
    5·1 answer
  • Whats an MAR? lolz better not take my points #bonkers
    12·2 answers
  • Which is the OS that can be obtained for free or at a much lower price than other major operating systems?
    15·2 answers
  • Helppppp
    13·1 answer
  • Blockchain is often associated with Bitcoin and the financial services industry. However, it is applicable to almost every indus
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!