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...

  • 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...

  • NOTE: USE PYTHON CS160 Computer Science Lab 14 Working with lists, functions, and files Objective: Work...

    NOTE: USE PYTHON CS160 Computer Science Lab 14 Working with lists, functions, and files Objective: Work with lists Work with lists in functions Work with files Assignment: Part 1: Write a program to create a text file which contains a sequence of test scores. Ask for scores until the user enters an empty value. This will require you to have the scores until the user enters -1. After the scores have been entered, ask the user to enter a file...

  • Write a c++ program to implement bubble sort of the data point given in the file...

    Write a c++ program to implement bubble sort of the data point given in the file data_points.txt file found in the Files area under assignments. Submit the source code in canvas. Do not change the data file name in your program if you are using file operations to load the values and should read from the same directory where your program reside

  • C++ Manually create a file Numbers.txtand fill it with integers, one below the other. Write a...

    C++ Manually create a file Numbers.txtand fill it with integers, one below the other. Write a program that reads ANY sequence of integers (can be positive, 0 or negative) from this file. Using a looping construct, you will read numbers from the file till end of the file is reached. Calculate the largest and the smallest numbers among the data in the file. Your code must display both of those on the terminal screen along with total count of numbers...

  • Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load...

    Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load them into an array. Add up all the values and store the answer in a variable. Find the largest and smallest number in the array. Put each into its own variable. Print to the screen the sum, largest value, and smallest value. Copy the array to a second array in reverse order. Print out the second array. Read in 20 integer numbers, all in...

  • Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays...

    Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays for the wind speed and for the temperature. You will then use the stored data to calculate and output the average wind speed and the average temperature. Create a constant with a value of 30 and use that to declare and loop through your arrays and as a divisor to produce your averages. Here is the data file (below). 1. 14 25 2. 12...

  • 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...

  • 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...

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