//********************************************************
//
// C Midterm - Question 7
//
// Name: Maya Mahin
//
// Class: C Programming, Spring 2025
//
// Date: March 23, 2025
//
// Description: This program prints charts showing the
// Fahrenheit equivalents of all Celsius temperatures from
// 0 to 100 degrees, and the Celsius equivalents of all
// Fahrenheit temperatures from 32 to 212 degrees
//
//********************************************************
#include <stdio.h>
/*define function prototypes*/
float toCelsius(float fahrenheitTemp);
float toFahrenheit(float celsiusTemp);
void printTableHeader(int tableNum);
void printTable(int TableNum, float temperature1, float temperature2);
int main(void) {
printTableHeader(1); /*print the header for the first table (i.e., the table that shows
celsius values converted to fahrenheit values)*/
/*Loop through values 0-100 and use them as
input values for the function that receives
a temperature value in celsius and returns
the converted value in fahrenheit*/
for (int i=0; i<101; i++) {
float fahrenheitRetVal=toFahrenheit(i); /*call the toFahrenheit function with the value from the loop*/
printTable(1,i,fahrenheitRetVal); /*print the results from the function in the table*/
}
printTableHeader(2); /*print the header for the second table (i.e., the table that shows
fahrenheit values converted to celsius values)*/
/*Loop through values 32-212 and use them as
input values for the function that receives
a temperature value in fahrenheit and returns
the converted value in celsius*/
for (int i=32; i<213; i++) {
float celsiusRetVal=toCelsius(i); /*call the toCelsius function with the value from the loop*/
printTable(2,i,celsiusRetVal); /*print the results from the function in the table*/
}
return 0;
}
//**************************************************************
// Function: toCelsius
//
// Purpose: Receives a temperature in fahrenheit, converts it
// to Celsius and returns the converted value
//
// Parameters:
//
// fahrenheitTemp - input temperature in fahrenheit
//
// Returns: celsiusTemp - result of converting input temperature to celsius
//
//**************************************************************
float toCelsius(float fahrenheitTemp){
float celsiusTemp=(fahrenheitTemp - 32) * 5/9; /*using the formula to convert fahrenheit to celsius*/
return celsiusTemp; /*returning the celsius value*/
}
//**************************************************************
// Function: toFahrenheit
//
// Purpose: Receives a temperature in celsius, converts it
// to Fahrenheit and returns the converted value
//
// Parameters:
//
// celsiusTemp - input temperature in celsius
//
// Returns: fahrenheitTemp - result of converting input temperature to fahrenheit
//
//**************************************************************
float toFahrenheit(float celsiusTemp){
float fahrenheitTemp=(celsiusTemp * 9/5) + 32; /*applying formula to convert celsius to fahrenheit*/
return fahrenheitTemp; /*returning fahrenheit value*/
}
//**************************************************************
// Function: printTableHeader
//
// Purpose: Prints the initial table header information.
//
// Parameters: tableNum - indicates which table header to be provider
// (a value of 1 indicates the first table header,
// a value of 2 indicates the second table header)
//
// Returns: void
//
//**************************************************************
void printTableHeader (int tableNum)
{
if (tableNum==1){
// print the table header for the celsius to fahrenheit conversion table
printf("\nCelsius Fahrenheit\n"); printf("------------------------\n"); }
else if (tableNum==2){
// print the table header for the fahrenheit to celsius conversion table
printf("\nFahrenheit Celsius\n"); printf("------------------------\n"); }
} // printTableHeader
//*************************************************************
// Function: printTable
//
// Purpose: Prints out all the temperature information
// in a nice and orderly table format.
//
// Parameters: tableNum - table number that printing output for
// temperature1 - the initial temperature value used to produce the converted temperature value
// temperature2 - the converted temperature value
//
//*************************************************************
void printTable (int tableNum, float temperature1, float temperature2)
{
// print the temperature
if (tableNum==1){
temperature1, temperature2); /*format the temperature values one way in the first table*/
}
else if (tableNum==2){
temperature1, temperature2); /*format the temperature values in a slightly different way in the second table
this is just to make sure everything aligns and the formatting is appropriate
for the type of value being displayed*/
}
}