Write a program to read a set of integers from an input data file, and locate the largest and smallest values within the data file. Print out the largest and smallest values, together with the lines on which they were found. Assume that you do not know the number of values in the file before the file is read.

Short Answer

Expert verified
To write a program that reads a set of integers from an input data file, locates the largest and smallest values, and prints out the values along with the lines on which they were found, here is a Python solution: 1. Open the data file using `file = open('data.txt')`. 2. Initialize variables: `min_value`, `max_value`, `min_lines`, `max_lines`, and `line_number`. 3. Read the file line by line using `for line in file:` and convert each line to an integer. 4. Compare each number with `min_value` and `max_value`. Update the values and their respective line numbers accordingly. 5. Print the smallest and largest values, and their respective line numbers. Here's the complete code: ```python file = open('data.txt') min_value = None max_value = None min_lines = [] max_lines = [] line_number = 0 for line in file: number = int(line) line_number += 1 if min_value is None or number < min_value: min_value = number min_lines = [line_number] elif number == min_value: min_lines.append(line_number) if max_value is None or number > max_value: max_value = number max_lines = [line_number] elif number == max_value: max_lines.append(line_number) print(f'The smallest number is {min_value} found on lines {min_lines}') print(f'The largest number is {max_value} found on lines {max_lines}') ```

Step by step solution

01

Required Libraries

First, import necessary library. In Python, the standard `open()` function allows for file reading, and it is a part of Python's standard library. So the code is: ```python # No import statement needed ```
02

Open the File

Open the file and assign it to a variable. Let's assume the file is called 'data.txt'. ```python file = open('data.txt') ```
03

Initializing Variables

Initialize variables to store the minimum and maximum values, as well as their corresponding line numbers. ```python min_value = None max_value = None min_lines = [] max_lines = [] line_number = 0 ```
04

Read each line in the File

Read each line in the file using a for loop, and increment the line number at each iteration. Convert each line to an integer. ```python for line in file: number = int(line) line_number += 1 ```
05

Compare the Values

Compare each number with `min_value` and `max_value`. If smaller or larger, update `min_value` and `max_value` respectively, reset associated line_numbers and add current line number. If equal, simply add current line number. ```python if min_value is None or number < min_value: min_value = number min_lines = [line_number] elif number == min_value: min_lines.append(line_number) if max_value is None or number > max_value: max_value = number max_lines = [line_number] elif number == max_value: max_lines.append(line_number) ```
06

Print the Result

Lastly, print the smallest and largest values, along with their respective line numbers. ```python print(f'The smallest number is {min_value} found on lines {min_lines}') print(f'The largest number is {max_value} found on lines {max_lines}') ``` This program should now successfully read all the integers from the data file, find the smallest and largest values, and print these out along with the corresponding line numbers.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Write a program that reads an arbitrary number of real values from a user- specified input data file, rounds the values to the nearest integer, and writes the integers out to a user-specified output file. Make sure that the input file exists, and if not, tell the user and ask for another input file. If the output file exists, ask the user whether or not to delete it. If not, prompt for a different output file name.

We wrote a MATLAB program that calculated the arithmetic mean (average), rms average, geometric mean, and harmonic mean for a set of numbers. Modify that program to read an arbitrary number of values from an input data file, and calculate the means of those numbers. To test the program, place the following values into an input data file and run the program on that file: \(1.0,2.0,5.0,4.0,3.0\), \(2.1,4.7,3.0\).

The acceleration due to the Earth's gravity at any height \(h\) above the surface of the Earth is given by the equation $$ g=-G \frac{M}{(R+h)^{2}} $$ where \(G\) is the gravitational constant \(\left(6.672 \times 10^{-11} \mathrm{~N} \mathrm{~m}^{2} / \mathrm{kg}^{2}\right), M\) is the mass of the Earth \(\left(5.98 \times 10^{24} \mathrm{~kg}\right), R\) is the mean radius of the Earth \((6371 \mathrm{~km})\), and \(h\) is the height above the Earth's surface. If \(M\) is measured in \(\mathrm{kg}\) and \(R\) and \(h\) in meters, then the resulting acceleration will be in units of meters per second squared. Write a program to calculate the acceleration due to the Earth's gravity in \(500-\mathrm{km}\) increments at heights from \(0 \mathrm{~km}\) to \(40,000 \mathrm{~km}\) above the surface of the Earth. Print out the results in a table of height versus acceleration with appropriate labels, including the units of the output values. Plot the data as well.

What is the difference between binary and formatted \(1 / O\) ? Which MATLAB functions perform each type of L/O?

Write a MATLAB program to generate a table of the base-10 logarithms between 1 and 10 in steps of \(0.1\). The table should start on a new page, and it should include a title describing the table and row and column headings. This table should be organized as shown below.

See all solutions

Recommended explanations on Psychology Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free