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

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.
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
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the following restrictions (SO WRITE TWO FILES THAT PLAY PYTHON THROUGH A SOCKET) Use a 5 x 5 grid (dimensions are subject to change, so use constants for NUM_ROWS and NUM_COLS) Use 'X' for player 1 and 'O' for player 2 (symbols and the number of players is subject to change, so use constants) Each player can make 1 move per turn before having to...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT