Question

Use C++How many values to process? 50 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Data file: Largest val

For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the console window (and output file) as pictured below. You will need to use a while loop to read all the values!

1. Write the algorithm – the list of steps needed to solve the problem. No C++ statements please! Use words like Get/Read, Sum, Average, Repeat, and Display. Think about what instructions you would need to read and process one value, then by putting this set of instructions in the loop you are able to process many values.

2. Write the C++ statements for the constant and variable declarations, choosing a name and data type for each one. Then write the C++ statements for the steps listed in step 1. Think about what C++ statement can be used for each step and carefully check the format and punctuation for each one.

3. Type the program – Create a new C++ project folder in Visual Studio using your last name and first initial (e.g., MortonL_Lab7). Add a source file to the project. Put your name, class time and today's date in comments at the very beginning of the code. Add inline comments and the code in the main function.

4. Debug program – Correct any errors found in your code.

5. Test program – What values are you expecting to see? Are you seeing these values? Verify by computing the expected value yourself… Excel may be useful to do this rather than entering all the values by hand into a calculator. In bonus part what should happen if the user enters a negative value or something larger than 100?

6. If you plan to go on and do the Bonus, wait and turn in your project after finishing that part. Otherwise, turn in your compressed solution electronically via Canvas. Run your program with input of 100 and screen capture the results which you should paste into the grade sheet. SAMPLE OUTPUT Data file: lab7_input.txt Largest value: xxx Smallest value: yyy Average of values: zzz.zz Press any key to continue… Optional Instructions Modify the code to ask the user for the number of values to read out of the data file and a new file name (use “myowninput.txt”). Create the myowninput.txt data file with at least 25 numerical values (one per Lab 7 DUE: Specified on Canvas CS 2010, Carlson line) and place it in the project folder where your .cpp file is stored. Make sure there are no blank lines at the end of the file. Read the given number of values out of the file. This number has to be less than or equal to 25 and greater than 5 so validate it. If any other value is requested by the user, the program should terminate with an error message and not process any values. Verify the computed results.

Output File

44

33

11

13

15

37

48

57

34

72

33

89

681

22

123

154

54

44

23

77

91

12

12

24

56

67

22

13

108

88

23

22

45

67

11

12

33

15

67

77

81

11

12

98

50

30

48

35

66

27

98

119

72

11

61

24

42

33

22

11

110

51

38

69

46

77

13

290

176

467

57

888

28

34

55

22

78

11

22

11

15

27

38

49

56

45

48

17

38

99

62

63

144

77

55

89

39

26

40

14

0 0
Add a comment Improve this question Transcribed image text
Answer #1

****This requires some effort so please drop a like if you are satisfied with the solution****

I have satisfied all the requirements of the solution and I'm providing the screenshots of output obtained exactly the same as above and code for your reference..

Algorithm:

Step 1: Start

Step 2: create an ifstream object and read the file from path

Step 3: declare string variables str to store each number while reading file , filename to store the filename

Step 4: declare integer variables max ,min, count, process and double variable avg to store the avg

Step 5: print "How many values to process"

Step 6: read process

Step 7: if file is not open then print " Error while opening file

Step 8: if file is bad or corrupt then print " Error while reading file"

Step 9: run while until getline(f,str)

Step 9.1: create stringstream object stoi(str)

Step 9.2 if process == count then break the loop

Step 9.3: increment count by 1

Step 9.4: convert str to int and store it in x

Step 9.5: if max<x then max=x

Step 9.6 if min>x then min=x

Step 9.7 calculate avg= avg+x

Step 10: calculate avg=avg/count

Step 11: print "Data File" filename

Step 12: print "Largest value: " max

Step 13: print "Smallest value: " min

Step 14: print "Average of values: "avg

Step 15: Stop

Code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
int main() {

//path to the file on my desktop
ifstream f("C://Users/THE__INFINITY/Desktop/input.txt");
   string str,filename="input.txt";
   int max=-9999,min=9999,count=0,process,x;
   double avg=0;
   cout<<"How many values to process ?";
   cin>>process;
   if (!f.is_open())
   perror("error while opening file");
else if (f.bad())
   perror("error while reading file");
   else{
       while(getline(f, str)) {
           stringstream stoi(str);
           if(process==count)
               break;
       count++;
       stoi >> x;
       if(max<x)
           max=x;
       if(min>x)
           min=x;
       avg+=x;
       }
       avg=avg/count;
       cout<<"\n-----------------------------------"<<endl;
       cout<<"Data file: "<<filename<<endl;
       cout<<"Largest value: "<<max<<endl;
       cout<<"Smallest value: "<<min<<endl;
       cout<<"Average of Values: "<<std::fixed<<std::setprecision(2)<<avg;
   }
   return 0;
}

Input file (input.txt):

inputbt input.txt - Notepad View Help File Edit Format 44 33 11 13 15 37 48 57 34 72 33 89 X

Output Screenshot:

F:AUntitled1.exe X How many values to process ?50 Data file: Largest value : Smallest value: Average of Values: input.txt 681

Code Screenshot:

#include <iostream> 1 #include <fstream> #include <sstream> #include <iomanip> using namespace std 6 int main() { 4 5 ifstreaavg avg/count; cout<<\n--- cout<Data file --- <<endl; 31 <<filename<<endl; <<max<<endl; <<min<<endl; <<std::fixed< < s

Add a comment
Know the answer?
Add Answer to:
Use C++ For this week’s lab you will write a program to read a data file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that reads the values from a data file til the end of...

    Write a C++ program that reads the values from a data file til the end of the data file is reached. It displays the values read, and computes and displays the largest, smallest, and the average of these values

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Introduction: In this lab, you will write a MIPS program to read in (up to) 50...

    Introduction: In this lab, you will write a MIPS program to read in (up to) 50 integer values from the user, store them in an array, print out the amay, one number per line, reverse the elements in the array and finally print out the elements in the just-reversed) array. Feel free to do this lab and all assembly programming labs) in Windows. You must use MARS Getting started: l. In MARS, create a new assembly file with the name...

  • please read directions first. this is supposed to be a hybrid programe add comments please JAVA...

    please read directions first. this is supposed to be a hybrid programe add comments please JAVA please add comments Programming Project 1 and Programming Project 2 "Hybrid" Your goal is to write a program that combines the functionality of both Programming Project 1andProgramming Project 2 into a single program. That is, your program should read a file ( Numbers.txt) of numbers of type double that contains some duplicates and is ordered smallest to largest. Your program should ignore the duplicate...

  • Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file...

    Exercise 6: Program exercise for 2D List Write a complete Python program including minimal comments (file name, your name, and problem description) that solves the following problem with the main function: Problem Specification: The following code reads values from the file object infile and stores them in the 2d list table2: (It assumes that each line contains values of elements in each row and the values are whitespace separated.) table2 = [] for line in infile: row=line.split() intRow = []...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, t...

    In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, then present a menu to the user, and at the end print a final report shown below. You may(should) use the structures you developed for the previous assignment to make it easier to complete this assignment, but it is not required. Required Menu Operations are: Read Students’ data from a file to update the list (refer to sample...

  • Topics: list, file input/output (Python) You will write a program that allows the user to read...

    Topics: list, file input/output (Python) You will write a program that allows the user to read grade data from a text file, view computed statistical values based on the data, and to save the computed statistics to a text file. You will use a list to store the data read in, and for computing the statistics. You must use functions. The data: The user has the option to load a data file. The data consists of integer values representing student...

  • (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...

    (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that you have been provided on Canvas. That file has a name and 3 grades on each line. You are to ask the user for the name of the file and how many grades there are per line. In this case, it is 3 but your program should work if the files was changed to have more or fewer grades per line. The name and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT