Question

In: Computer Science

To read in a number from a file as an integer is very simple. (Assuming our...

To read in a number from a file as an integer is very simple.

(Assuming our file is defined as myInput; int mynum; myInput >> mynum;

The file name should be myNum.txt

A sample run of the program: The min is: 40 The max is: 90 The total is: 180

The average is: 60 The number of records read in was: 3 This run used 90 40 50 as its test input.

A couple of things to keep in mind, you must handle two other conditions. The file does not exist or cannot be opened. The file opens but does not contain data. Make sure you have error messages for these conditions. The other thing that you have to handle is that the numbers might include negative numbers or very large numbers so using 0 as a starting point for max or some high number for min may not work. For testing create your own input. Think about what we said for easy testing then try with more numbers. Look at the work we did in class for hints on file handling. I suggest you work through your logic (i.e. pseudocode/algorithm) before coding.

Solutions

Expert Solution

Solution:

Code:

#include<fstream> // include statement for file operations.

#include<iostream> // include statement for I/O operations.

using namespace std;

int main() {

int cnt=0; // counter variable

ifstream file("myNum.txt"); // opens the file.

int min=INT16_MAX; // min variable assigned to max value.

int max=INT16_MIN; // max variable assigned to min value.

int val; // variable to store values after reading from file.

int total=0; // total variable initialised to 0.

while (file >> val){

cnt++; // increments counter variable.

total+=val; // adds the value read from the file.

if(min>val)

min=val; // updates min variable.

if(max<val)

max=val; // updates max variable.

}

cout<<"The min is: "<<min<<endl;

cout<<"The max is: "<<max<<endl;

cout<<"The total is: "<<total<<endl;

cout<<"The average is: "<<total/cnt<<endl;

//close the file

file.close();

}

Code screenshot:

Code output screenshot:


Related Solutions

//Java Language Read an integer number from the user. If the number is not positive, change...
//Java Language Read an integer number from the user. If the number is not positive, change its sign.    1   Count the digits of the number 2   Count the odd digits of the number 3   Calculate the sum of the digit values of the number
so i want to read in a file. just to keep it simple, the text file...
so i want to read in a file. just to keep it simple, the text file has student name and the grade, separated by a space. let's say there are 192 student's name and I want to read them in and made them into a Student /object class, which has a constructor, student(string name, int grade). individually creating them seems pain in the ass. reading in should work for any number of names. So how do I do this in...
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a...
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a file. Each pair will then be multiplied together with the results being accumulated (added together) forming a sum-of-products operation. Submit your report and code here.
File has a format Name and number, the number represents power. The name and the (integer)...
File has a format Name and number, the number represents power. The name and the (integer) power are separated by some amount of space. Importantly, any line that begins with a hash '#' are comments and they need to be ingored. Write a program that reads from that file, and prints out only the name of the hero with the strongest power. That name should be capitalized (not uppercase, but capitalized, as in 'Galadriel') Here is the heroes.txt # DC...
simple Java project// please explain every statement with reasoning. Thank you Read from a file that...
simple Java project// please explain every statement with reasoning. Thank you Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line...
write a c# program that: The file is using a very simple form of compression in...
write a c# program that: The file is using a very simple form of compression in which there are no spaces, each word is separated by camel casing. For Example, the string "TheCatWillRun" is parsed as "The Cat Will Run". *Now for the statistics* Prints to the console the following statistics about the content in the file retrieved above. - How many of each letter are in the file - How many letters are capitalized in the file - The...
In this lab, you open a file and read input from that file in a prewritten...
In this lab, you open a file and read input from that file in a prewritten C++ program. The program should read and print the names of flowers and whether they are grown in shade or sun. The data is stored in the input file named flowers.dat. Instructions Ensure the source code file named Flowers.cpp is open in the code editor. Declare the variables you will need. Write the C++ statements that will open the input file flowers.dat for reading....
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
Write a C++ program to read a collective of integer numbers. I f the number is...
Write a C++ program to read a collective of integer numbers. I f the number is greater than zero and less than 15 then terminate the loop and find factorial of the number
Create a c file to read from an existing file one character at a time and...
Create a c file to read from an existing file one character at a time and print that character to the console Create a c file to read a character from the standard input and write it to a file. please help me
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT