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
A storyboard is an example of an implementation tool.<br><br> A.<br> True<br><br> B.<br> False
Phantasy [73]
A True A storyboard is hardware installed directly onto the motherboard or using a <span>Peripheral device</span>
4 0
3 years ago
Read 2 more answers
A programmer is writing a program that is intended to be able to process large amounts of data. Which of the following considera
Dmitry [639]

Answer:

B. How many programming statements the program contains

Explanation:

If a program is intended to be able to process large amounts of data, then the ability of the program to process larger data sets is heavily affected by

  • execution time
  • size of memory the program requires
  • storage space the program requires as it runs.

The length of the statements the program contains does not much affect program performance.

8 0
3 years ago
Which of these YA (young adult movies is not base on a book is it A wrinkle in time, or Midnight sun,or Love,simon.
ExtremeBDS [4]
I think it’s midnight sun
4 0
3 years ago
What is the meaning of HDMI?
tiny-mole [99]

Hey there!

The correct answer is that it stands for high definition video devices.



hope this helped and have a great day (:

3 0
4 years ago
Read 2 more answers
Alright, so I have a wireless mouse that uses a dongle (it's not Bluetooth nor wired). But recently I realized that none of the
serg [7]

Answer:Mouse is not working because you haven't changed your battery for months.

Remember that, most of wireless mouses use AA 1.5V battery for it's power on. If you've let your computer and mouse set on, then the battery can only run around 5-6 months. If you turn off your mouse everytime you're not using then the battery life might get longer. So, all you have to do is buy new batteries and replace it. Follow steps of replacing battery in this video.

(Consider buying dry battery for using longer)

No.2: Loose battery in the tray

This is quite common as well. We understand that the mouse made materials can have its own changing. Sometimes, you might drop your mouse, then after that, the loosen battery just happening around. A loosen battery might get your wireless mouse not working well. It works then stop at the middle. Follow the easy steps to fix this state of mouse is not working.

No. 3: Bad Pairing.

Due to some reasons, bad pairing can happen and get your mouse stuck on laptop screen. Bad pairing might be from overworking or overloading tasks on your PC. Easy quick fix for bad pairing is disconnect your USB receiver for a while and re-plug in later on.

No.4: On/Off Switch or Power button on the mouse just drop or loosen in

Many people might have problems with their mouse on/off switch button. Don't worry, this can be fixed as well. You can open the mouse (just look under the battery, there is a single screw). Open the mouse from this screw, and disassembly it carefully. You can see the on/off switch is just around the corner. Now, you can, reattach the button and close the mouse correctly.

No. 5 : Buy a new mouse

I've suggested a way to search for a new wireless mouse with USB Unifying Receiver. For a wireless mouse that allows Unifiying receiver, then you don't worry when your current usb receiver get lost (because, you can always buy a new usb receiver).

Explanation:

6 0
3 years ago
Other questions:
  • ¿Cuál es el objetivo principal de los servicios?
    7·1 answer
  • What is a main cause of a virus on a computer
    6·1 answer
  • This program has some errors in it that are needed to be checked import java.io.*;
    13·1 answer
  • You have been given the job of creating a new order processing system for the Yummy Fruit CompanyTM. The system reads pricing in
    8·1 answer
  • Select the characteristics that describe the evolution of computers that predate the personal computer (select all correct answe
    12·1 answer
  • What do the buttons on the paste options menu do
    11·1 answer
  • ___________ is a technique that uses IT to develop an ongoing relationship with the purchasers or buyers of an organization's pr
    13·1 answer
  • Estoy empezando a usar una pagina llamada source filmmaker, no se que apreté y quiero el menú de nuevo (en donde se ponen los mo
    14·1 answer
  • Select the correct answer.
    14·1 answer
  • Compare and contrast the leadership and leadership and management style of apple and Samsung ​
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!