Question

PLEASE HELP C++

Program #2 This problem is a modification of Problem 24 on page 302 of the Gaddis text with two small additions. (A scan of this problem can be found on the last page of the assignment.) In this assignment, your program will read in a list of random numbers from an input file, random. txt (found on eLearning in the Homework Input Files folder), and calculate the following statistics on those numbers A. B. C. D. E. F. The number of numbers in the file The sum of the numbers in the file The average of the numbers in the file The largest number in the file The smallest number in the file The second largest number in the file CS1336-Programming Fundamentals Page: 4 G. The second smallest number in the file Your program should then print out a report, similar to the following (note that these are demonstration numbers only; they are not accurate for the homework data file) Number of numbers Sum of numbers: Average of numbers: Largest number: Smallest number: Second largest number: Second smallest number: 235 5346 22.8 515 512 ogramming Notes: 1. Note that the number of numbers, the sum of the numbers, the largest and smallest numbers are integers, whereas the average should be a floating point data type printed to one decimal place 2. All of the numbers in the file are between 0 and 1000. Therefore, the minimum value can be found by using the following technique a. Create an int variable minValue and initialize it to some number larger than any number in the file, say 2000 Then include the following statement inside your file processing loop b. if (nextValue < minValue) minValue-nextValue;

some of the content in random.txt

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

#include <iostream>

#include <fstream>

#include <sstream>

#include <iomanip>

using namespace std;

int main()

{

ifstream in("random.txt"); //Opening file

string line; //Hold line by line string from file

int minValue = 2000, second_minValue = 2000, maxValue = -1, second_maxValue = -1;

int nextValue, totalNum = 0, sum_of_elems = 0;

if(in.is_open()) //To check file open or not

{

while(getline(in, line)) //read line by line from file

{

stringstream toNum(line); //to convert string to integer declaring stringstream

toNum >> nextValue; //converting string to int

sum_of_elems += nextValue; //sum of value one by one

if(nextValue < minValue) //to find smallest value

{

second_minValue = minValue; //hold second smallest value;

minValue = nextValue;

}

else

{

if(nextValue < second_minValue) //to find second smallest value

second_minValue = nextValue;

}

if(nextValue > maxValue) //to find largest value

{

second_maxValue = maxValue; //hold second largest value;

maxValue = nextValue;

}

else

{

if(nextValue > second_maxValue) //to find second largest value

second_maxValue = nextValue;

}

totalNum++; //total numbers count

}

//To handle if one value present in file

if(second_minValue > 1000)

second_minValue = minValue;

if(second_maxValue < 0)

second_maxValue = maxValue;

cout << "\n\t\tNumber of Numbers : " << totalNum <<endl;

cout << "\t\tSum of numbers : " << sum_of_elems << endl;

//Average converted to float and precision set to 1 decimal place

cout << fixed << setprecision(1) << "\t\tAverage of numbers : " << ((float)sum_of_elems / (float)totalNum) << endl;

cout << "\t\tLargest number : " << maxValue << endl;

cout << "\t\tSmallest numbers : " << minValue << endl;

cout << "\t\tSecond largest number : " << second_maxValue << endl;

cout << "\t\tSecond smallest number : " << second_minValue << endl;

}

else

{ //If unable to open file display error message

cout << "failed to open file" << endl;

}

return 0;

}

======================================

random.txt

random.txt 1 42 2 468 3 335 4 501 5 170 6 725 7 479 8 359 9 963 10 465 11 706 12 146 13 282 14 828 15 962 16 492 17 996 18 94

OUTPUT :

Number of Numbers : 46 Sum of numbers 26291 Average of numbers 571.5 Largest number: 996 Smallest numbers 36 Second largest n

Add a comment
Know the answer?
Add Answer to:
PLEASE HELP C++ some of the content in random.txt Program #2 This problem is a modification...
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
  • :Pls help for this programing problem in c++ (standard IO without file read/write),better with some comment...

    :Pls help for this programing problem in c++ (standard IO without file read/write),better with some comment about why coding that way,thanks Problem A: Counting Numbers Problem Description Write a program that reads numbers from the keyboard into an integer array. You may assume that there will be 50 or fewer entries in the array. Your program allows any number of numbers to be entered, up to 50 numbers. The output is to be a two-column list. The first column is...

  • Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...

    Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART B: (Statistics Program) – (50%) Please read this lab exercise thoroughly, before attempting to write the program. Write a program that reads the pairs of group number and student count from the text file (user must enter name of file) and: Displays the number of groups in the file (5 %) Displays the number of even and odd student...

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

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

  • please put the comment each line, make sure i will have output too. write a program,...

    please put the comment each line, make sure i will have output too. write a program, Summarize (Summarize.java), containing the main() method, that first writes 10,000 random positive double type numbers, to the full accuracy of the number (15/16 decimal places), to a text file named DataValues.txt, one number per line. The program must then close that file, and reopen it for reading. Read back the values from the file and write the following information to a file named Summary.txt:...

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

  • Could anyone please help with this Python code assignment? In this programming assignment you are to...

    Could anyone please help with this Python code assignment? In this programming assignment you are to create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The count of how many numbers are in the file. The average of the numbers. The average is the sum of the numbers divided by how many there are. The maximum value. The...

  • Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel –...

    Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel – sentinel is used to indicate the end of input and must not to be considered as the valid data for the computation). Perform the following operations for the integers entered by the user. Display the sum of the numbers less than 100 Display the smallest of the positive integers Display the largest of the negative integers Display how many integers are between 100 and...

  • I am in C++ programming, I need help with this assignments. The answer in this assignments...

    I am in C++ programming, I need help with this assignments. The answer in this assignments when I look for it it doesn't work in my visual studios. Can you please help me? I hardly have C in the class, if I fail I will fail the whole class and repeat this class again.   The file named Random.txt contains a long list of random numbers. Download the file to your system, then write a program that opens the file, reads...

  • Write a C++ program that does the following: Asks the user to enter 3 integers, Obtains...

    Write a C++ program that does the following: Asks the user to enter 3 integers, Obtains the numbers from the user, Prints the largest number and then the smallest of the numbers, If the numbers are equal, prints the message: "These numbers are equal." Prints the sum, average, and product of the 3 numbers.

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