Reputation: 6251
I'm working through 'C Primer Plus', was just going through the programming exercises and I've hit a brick wall on the last one on the fifth chapter (Operators, Expressions and Statements).
The exercise is:
Write a program that requests the user to enter a Fahrenheit temperature. The program should read the temperature as a type double number and pass it as an argument to a user-supplied function called Temperatures(). This function should calculate the Celsius equivalent and the Kelvin equivalent and display all three temperatures with a precision of two places to the right of the decimal. It should identify each value with the temperature scale it represents. Here is the formula for converting Fahrenheit to Celsius:
Celsius = 1.8 * Fahrenheit + 32.0
The Kelvin scale, commonly used in science, is a scale in which 0 represents absolute zero, the lower limit to possible temperatures. Here is the formula for converting Celsius to Kelvin:
Kelvin = Celsius + 273.16
The Temperatures() function should use const to create symbolic representations of the three constants that appear in the conversions. The main() function should use a loop to allow the user to enter temperatures repeatedly, stopping when a q or other nonnumeric value is entered.
My code is:
#include <stdio.h>
void Temperatures(double);
int main(void)
{
double farh;
printf("Enter a fahrenheit temperature: ");
scanf("%f", &farh);
printf("\n");
Temperatures(farh);
return 0;
}
void Temperatures(double f)
{
float c;
float k;
c = 1.8 * f + 32;
k = c + 273.16;
printf("Fahrenheit Celcius Kelvin\n");
printf("%.2f %.2f %.2f\n", f, c, k);
}
Where have I gone wrong? :o Just get nonsense.
Upvotes: 0
Views: 2642
Reputation: 1
An alternate version that goes beyond the exercise. Here, I want to have values returned by the functions. However, a function cannot return two values without getting into more advanced topics, pointers and such. So instead we'll separate the Celsius and Kelvin calcs into two separate functions, and return them to main().
I have tried to lay this example out very clearly, notice the use of i and j in this example, as it demonstrates how these variables are just in use in the functions themselves, not in the main body of the program. "fah" (for fahrenheit) is the variable being sent to the functions, and we tell the functions what variables to return. Notice how these functions work the same way algebra functions work, they are based on the same idea. f(x) = (y)(z). Only the (y)(z) part goes outside the body of the main program. In this analogy, the x in f(x) is the value that is "passed" to the function. And the "f" can be anything we want... so of course I chose the most obvious name, celcius and kelvin, and then they can be called over and over in the main body of the program. Think of functions as subroutines.
#include <stdio.h>
#include <stdlib.h>
double celcius (double i);
double kelvin (double j);
int main()
{
double fah = 0.0;
printf("Enter a Fahrenheit temperature (or 'q' to quit): ");
while (scanf("%lf", &fah) == 1)
{
printf("Fahrenheit: \t%8.2lf", fah);
printf("\n");
printf("Celcius: \t%8.2lf", celcius(fah)); // 1st function call
printf("\n");
printf("Kelvin: \t%8.2lf", kelvin(fah)); // 2nd function call
printf("\n");
printf("Enter another Fahrenheit temperature: ");
}
printf("goodbye");
return 0;
}
double celcius (double i) // the value fah is "passed" to i. weird, right?
{
const double FRAC = (5.0 / 9.0);
const double FREEZE = 32.0;
double cel; // notice I have to define cel HERE, not in main.
cel = FRAC * (i - FREEZE); // the only place we see i
return cel; // here we tell the function which value to send back
}
double kelvin (double j)
{
const double ADD = 273.16;
double kel; // again, I must define the variable IN the function.
kel = ADD + ((5.0 / 9.0) * (j - 32.0));
return kel;
}
Upvotes: -1
Reputation: 11
fully worked function
void exc8(void){
double Celtemp = 0;
double Fartemp = 0;
double Keltemp = 0;
printf("enter the temperature in Fahrenheit: ___\b\b\b");
while (scanf_s("%lf",&Fartemp)==1)
{
Celtemp = (1.8 * Fartemp) + 32.0;
Keltemp = Celtemp + 273.16;
printf("\nThe temp %4.3lf in Far is equal are %4.3lf in Celc and %4.3lf in Kelv\n", Fartemp, Celtemp, Keltemp);
printf("Enter another one to convert or non number char for exit:___\b\b\b");
}
}
Upvotes: 1
Reputation: 1
the formula for calculating fahrenheit to celcius is wrong.. instead is
(°F - 32) x 5/9 = °C
and aslo excercise is asking to convert f to kelvin so : Fahrenheit To Kelvin:
(5/9 * (Fahrenheit - 32) + 273 ).
anyway can someone help how to make this : The main() function should use a loop to allow the user to enter temperatures repeatedly, stopping when a q or other nonnumeric value is entered. thnks
Upvotes: 0
Reputation: 1366
You are using scanf to take input and cast it to a certain type, in this case to get a float
for the fahrenheit.
scanf("%f", &farh);
However, you are initialising farh
as a double...
...
int main(void)
{
double farh;
...
Change this to float farh;
and see if that helps.
Updated to answer question within the comment below
If you want to take in a double, instead of a float, then instead of the above, change the scanf line to be as follows:
scanf("%lf", &farh);
Upvotes: 2
Reputation: 213306
You ask for a float %f as input through scanf but store it in a double. float and double is often of different size, and if so, you'll get garbage when trying to store a float inside a double. Try to use %lf instead, or change "farh" to float.
Upvotes: 3