Please help with C programming! How can I convert this code to a function? I nee
ID: 666869 • Letter: P
Question
Please help with C programming! How can I convert this code to a function? I need to have a function prototype above main, a function call inside of main, and then have the function definition below main. I achieved the same result without using a function which was to create a program that converts temperatures between Fahrenheit and Celsius and prints the resulting value to the screen. However, that apparently didn't suffice and I really don't know what to do. Any assistance would be greatly appreciated!
#include
int main() //Main program
{
int fahr,cel,selection; //Declaration of variables
printf("Enter a 1 to convert a Fahrenheit number to Celsius, or enter a 2 to convert a Celsius number to Fahrenheit. "); //User prompt
scanf("%d",&selection); //Read selection
//Displays the Celsius equivalent of the Fahrenheit temperature entered
if(selection==1)
{
printf("Enter the number to be converted ");
scanf("%d",&fahr);
cel=(5.0/9.0 * (fahr-32));
printf("%d degrees Fahrenheit is %d degrees Celsius ",fahr,cel);
}
//Displays the Fahrenheit equivalent of the Celsius temperature entered
if(selection==2)
{
printf("Enter the number to be converted ");
scanf("%d",&cel);
fahr=(9.0/5.0 * cel+32);
printf("%d degrees Celsius is %d degrees Fahrenheit ",cel,fahr);
}
} //End main
Explanation / Answer
I have created two functions for the above program, one for conversion from celsius to fahrenheit and the other from fahrenheit to celsius. I have specified function declaration, function call, and function definition in the comments. In addition, I have modified the values of temperature from int to float in order to get the correct values.
Modified program:
#include<stdio.h>
void Fah_to_Cel(); //function declaration
void Cel_to_Fah(); //function declaration
float fahr,cel; //global declaration of the required variables so that they are available to all functions in the program
int main() //Main program
{
int selection; //Declaration of variables
printf(" 1. Convert temperature from Fahrenheit to Celsius ");
printf(" 2. Convert temperature from Celsius to Fahrenheit ");
printf(" Enter your choice "); //User prompt
scanf("%d",&selection); //Read selection
//Displays the Celsius equivalent of the Fahrenheit temperature entered
if(selection==1)
{
Fah_to_Cel(); //Function call
}
//Displays the Fahrenheit equivalent of the Celsius temperature entered
else if(selection==2)
{
Cel_to_Fah(); //Function call
}
else
{
printf("Invalid option ");
}
} //End main
void Fah_to_Cel() //Function definition
{
printf("Enter the number to be converted ");
scanf("%f",&fahr);
cel=(5.0/9.0) * (fahr-32);
printf("%f degrees Fahrenheit is %f degrees Celsius ",fahr,cel);
}
void Cel_to_Fah() //Function definition
{
printf("Enter the number to be converted ");
scanf("%f",&cel);
fahr=(9.0/5.0)* (cel+32);
printf("%f degrees Celsius is %f degrees Fahrenheit ",cel,fahr);
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.