Question

Problem (3) 15 points Write a C++ segment of code in main() that reads a file called numbers txt. You should cout the range of the numbers in the file, for example if the file contained 3.5 1.8 15 the range of numbers is 15 1.8 13.2

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

------------------------------------------------------------------- Program ------------------------------------------------------------------

#include <iomanip>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
//Locate numbers.txt
ifstream f1 ("numbers.txt", ios::in);

int num, largest = 0, smallest=9999999;

//Read file
cout<<"\n\nReading numbers.txt \n\n";
for( int i = 0; i < 20; i++)
{
   // Extract each int into num variable
       f1>> num;
      
       // If the number is greater than current largest assign num to largest  
       if (num > largest)
       {
           largest = num;
       }
       // If the number is smaller than current smallest assign num to smallest
       if (num < smallest)
       {
           smallest = num;
       }      
   }
  
cout << "Largest: " << largest;
cout << "\nSmallest: " << smallest;
  
   // Print range
   cout<< "\n\nRange : "<< (largest-smallest);
   //close File
f1.close();
return 0;
}

-------------------------------------------- numbers.txt ------------------------------------------------------------

6 13 16 17 11 5 18 9 10 20

------------------------------------------- Sample Output -------------------------------------------------------

Reading numbers.txt

Largest: 20
Smallest: 5

Range : 15
--------------------------------
Process exited after 0.1266 seconds with return value 0
Press any key to continue . . .

Reading numbers.txt Largest 20 Smallest: 5 ange 15 Process exited after 0.09612 seconds with return value E Press any key to

Add a comment
Know the answer?
Add Answer to:
Write a C++ segment of code in main () that reads a file called "numbers txt"....
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
  • 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...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Write a program in C++ that reads in integer numbers from a file called scores.txt until...

    Write a program in C++ that reads in integer numbers from a file called scores.txt until the sentinel value -999 is read. The program should then output the total and average of the numbers read and output each of the numbers incremented by the overall average to a file called or scoresout.txt along with the sentinel value of -999 at the end So if 10, 20, 30 and -999 are read in then the program would display a total of...

  • 1. Write a program called Numbers that a. prompts the user for a file name. b....

    1. Write a program called Numbers that a. prompts the user for a file name. b. reads that file assuming that its contents consist entirely of integers. c. prints the maximum, minimum, sum, count (number of integers in the file), and average of the numbers. For example, if the file numberinput.dat has the following content: 4 -2 18 15 31 27 Your program should produce the following output: csc% java Numbers Enter file name: numberinput.daft Maximum31 Minimum- -2 Sum -...

  • Write a C++ that read a list of numbers from a .txt file into array. The...

    Write a C++ that read a list of numbers from a .txt file into array. The first digit on the list will be array size, while the second number on the list will be our first number in our array list. Example: t1.txt file contain list off input number: 5, 3, 6, 10, 43, 23. notice the first number is 5 which will need to be use as an array size and the rest of the numbers are in the...

  • The following code reads in 4 comma separated values from a .txt file of the format:...

    The following code reads in 4 comma separated values from a .txt file of the format: Kayne,West,1112223333,0. getline(inputFile, firstName, ','); getline(inputFile, lastName, ','); getline(inputFile, phoneNumber, ','); getline(inputFile, phonetype, '\n'); How could I write a while loop that reads through a .txt file like above (without using eof!) but with an unspecified number of entries?

  • Write a program in C that reads 20 float numbers from a file. The values are...

    Write a program in C that reads 20 float numbers from a file. The values are stored in a vector (1-dimensional array). Find and display the smallest number, the second smallest number, and the third smallest number of the set. Use only these libraries: stdio.h, stdlib.h, math.h, and string.h. Below is an example of the input file (in1.txt) and the resulting output file (output.txt). In1.txt: 3.14       4.05       -3.73      4.13       1.32 -2.21      0.46       4.57       4.64       -3.42 4.57       -0.14      3.00       -3.58      -0.78...

  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • a. Provide me with your code file, output file and the text file. 1. Create a...

    a. Provide me with your code file, output file and the text file. 1. Create a file with a series of integers. Save it as numbers. txt. Write a program that reads all the numbers and calculates their sum . 2. Create a file having different integers than the first one. Save it as numbers1. txt . Write a program that reads all the number and calculates their average. Important: The two files that you are creating will contain different...

  • Please write a short code that opens a text file called "data.txt" and reads, writes, and...

    Please write a short code that opens a text file called "data.txt" and reads, writes, and keeps writing data to the file in vb.net.

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