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
Convert the following hexadecimal numbers to decimal numbers. (Show your work) a. 0xE3 b. 0x4A c. 0xF9
MAVERICK [17]

Answer:

a. 227 , b. 74 , c. 249

Explanation:

a. 0x E3 = 227

Hexadecimal Digit E corresponds to decimal number 14.

So decimal representation = 14 * 16 + 3 = 224 + 3 = 227

b. 0x 4A = 74

Hexadecimal Digit A corresponds to decimal number 10.

So decimal representation = 4 * 16 + 10 = 74

c. 0x F9 =

Hexadecimal Digit F corresponds to decimal number 15.

So decimal representation = 15 * 16 + 9 = 240 + 9 = 249

3 0
3 years ago
What is something that can be done to help stop teen teen crashes
8090 [49]

Teaching teens about texting and driving considering 26 percent of all motor vehicle crashes are related to the use of cell phones. Each day, 11 teens die in crashes caused by texting and driving. 2.35 million people in the US are injured or disabled by car crashes every year. More than 330,000 of these crashes that cause severe injury are caused by texting and driving.

Hope this helped good luck!!!


8 0
3 years ago
Why do computers use binary code?
Anna71 [15]

Answer:

To make sense of complicated data, your computer has to encode it in binary. Binary is a base 2 number system. Base 2 means there are only two digits—1 and 0—which correspond to the on and off states your computer can understand

4 0
3 years ago
Read 2 more answers
Firefox, Chrome, Opera, and Safari are examples of
MrRissso [65]
D. Browsers

You're on one right now! :)

3 0
3 years ago
(1) Prompt the user to enter two words and a number, storing each into separate variables. Then, output those three values on a
Sergeu [11.5K]

Answer:

import java.util.Scanner;

public class num3 {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.println("Enter Favorite color");

       String word1 = in.next();

       System.out.println("Enter Pet's name");

       String word2 = in.next();

       System.out.println("Enter a number");

       int num = in.nextInt();

       //Point One

       System.out.println("You entered: "+word1+" "+word2+

               " "+num);

       //Point Two

       String passwordOne = word1+"_"+word2;

       String passwordTwo = num+word1+num;

       System.out.println("First password: "+passwordOne);

       System.out.println("Second password: "+passwordTwo);

       //Point Three

       int len_passwrdOne = passwordOne.length();

       int len_passwrdTwo = passwordTwo.length();

       System.out.println("Number of characters in "+passwordOne+" :" +

               " "+len_passwrdOne);

       System.out.println("Number of characters in "+passwordTwo+" :" +

               " "+len_passwrdTwo);

   }

}

Explanation:

  • This question is solved using java programming language
  • The scanner class is used to receive the three variables (Two string and one integer)
  • Once the values have been received and stored in the respective variables (word1, word2 and num), Use different string concatenation to get the desired output as required by the question.
  • String concatenation in Java is acheived with the plus (+) operator.
3 0
3 years ago
Other questions:
  • Buildings must be wired to comply with the latest National Electrical Code to ensure that, with adequate maintenance, the instal
    11·1 answer
  • Why isn't my rank move from ambitious to virsto i met all the requirements
    8·2 answers
  • Which should you use to find a saved file?
    15·2 answers
  • Nvm, Its B.
    10·1 answer
  • The technology behind the Internet and E-mail dates back as far as 1969. What two software innovations helped the Internet becom
    14·1 answer
  • How to make changes to a file on the USB drive
    6·2 answers
  • Using either a UNIX or a Linux system, write a C program that forks a child process that ultimately becomes a zombie process. Th
    8·1 answer
  • Please write a Java program to read movies.txt file. (It is in Modules under Chapter Code) Create your own movies.ser file from
    14·1 answer
  • Define artificial intelligence?​
    15·2 answers
  • Phoebe is a Counselor who is trying to schedule a meeting with one of her patients. Which best describes an inappropriate locati
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!