216k views
4 votes
With a list of given Fahrenheit temperatures, display a table of converted Celsius temperatures.

The program should read the temperature inputs from a file.
Two input files are provided for use. The first line of the file indicates the type of temperature data stored.
The program will ask user to enter a selection of 1) Fahrenheit to Celsius or 2) Celsius to Fahrenheit conversion. The program will validate user's input such that only valid selection will be allowed to proceed.
Carefully align both temperature columns as a table for output.
Format each temperature precision to only single decimal digit.

1 Answer

6 votes

Final answer:

To convert Fahrenheit to Celsius, use the formula C = (F - 32) * 5/9. To convert Celsius to Fahrenheit, use the formula F = (C * 9/5) + 32. The program should read temperature inputs from a file and present a table of converted Celsius temperatures.

Step-by-step explanation:

To convert Fahrenheit to Celsius, you can use the formula: C = (F - 32) * 5/9. To convert Celsius to Fahrenheit, you can use the formula: F = (C * 9/5) + 32. The program should read the temperature inputs from a file and present a table of converted Celsius temperatures. The program will ask the user to select either Fahrenheit to Celsius or Celsius to Fahrenheit conversion, and validate the user's input before proceeding.

1. Read the temperature data from a file: - Start by asking the user for the filename where the temperature data is stored. - Open the specified file in read mode. - Read the first line to determine the type of temperatures stored in the file ("F" for Fahrenheit or "C" for Celsius). - Continue reading the file line by line, storing each temperature value in a list for later processing.

2. Ask the user for the conversion direction: - Prompt the user to enter a selection of 1 for Fahrenheit to Celsius conversion or 2 for Celsius to Fahrenheit conversion.

- Validate the input to make sure the user enters either 1 or 2. If not, ask the user to enter the selection again.

3. Convert the temperatures: - If the user selects "1" and the temperatures in the file are in Fahrenheit, use the formula `C = (F - 32) * 5/9` to convert each temperature to Celsius. - If the user selects "2" and the temperatures in the file are in Celsius, use the formula `F = (C * 9/5) + 32` to convert each temperature to Fahrenheit. - If the selection does not match the data type indicated by the first line in the file, inform the user and exit, as no conversion is necessary.

4. Print the results in a table format: - Create a header line indicating "Fahrenheit" and "Celsius", aligning them appropriately. - Iterate over the original temperatures and their converted values, printing them in two aligned columns. - Format each temperature to only one decimal digit for both Fahrenheit and Celsius values.

User Johnnywho
by
7.6k points