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]
4 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]4 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
Please help add header file
ki77a [65]
There is nothing to see
5 0
3 years ago
16:9 and 4:3 are numbers that refer to an image’s...
lozanna [386]
These refer to an aspect ratio of an image.
6 0
3 years ago
A computer network is best described as two or more computers that are
ANTONII [103]
A computer network is best described as two or more computers that are linked together. 
6 0
3 years ago
How many steps are there on Mail Merge Task Pane? List them.​
Pavlova-9 [17]

Answer:

There are six steps in the Mail Merge Task Pane.

  • Select the document type.
  • Start the document.
  • Select recipients.
  • Write your letter.
  • Preview your letters.
  • Complete the merge.

Explanation:

Mail Merge is a handy feature that incorporates data from both Microsoft Word and Microsoft Excel and allows you to  create multiple documents at once, such as letters, saving you the time and effort of retyping the same letter over and  over.  

4 0
3 years ago
Which of the following is recognition that data used by an organization should only be used for the purposes stated by the infor
uranmaximum [27]

Answer:

A. Accountability

Explanation:

Data accountability refers to a situation whereby an organization takes responsibility of what is being done with data and how compliant they are to the principles guiding the data. It refers to recognizing the fact that data is being used for the stated purposes by the owner of the data at the time it was collected.

Data confidentiality simply refers to protecting the data against unauthorized, illegal, unlawful usage or access.

Data privacy refers to an organization controlling what data has to be shared with an external factor.

Data availability ensures that data is readily available and accessible when needed.

The best option is accountability.

8 0
3 years ago
Other questions:
  • What is a software program for navigating the web and displaying websites and pages?
    5·1 answer
  • To configure a router / modem, what type of IP interface configuration should you apply to the computer you are using?
    8·1 answer
  • ________ is the process of translating a task into a series of commands that a computer will use to perform the task.
    5·1 answer
  • What is Processor to Memory Mismatch problem?
    15·1 answer
  • What is a directed graph?
    5·1 answer
  • What is the system that consists of nonproprietary hardware and software based on publicly known standards that allow third part
    13·1 answer
  • Can I change my username?
    6·1 answer
  • What are some ways to find out what skills you need to develop at work? Check all of the boxes that apply.
    15·2 answers
  • Fill in the table below of the Interactive Word Wall. You are provided with the
    13·1 answer
  • Samantha plans to self publish her own website. What app would she use to design the cover
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!