Question:
C programming. Easy problem.?
John
2012-11-20 20:16:24 UTC
So Im 90% done with this program. Here is my problem. When I run it, I get Farenheit goes from 1 to 100 but celcius only equals 0 all the way down. It seems like an easy fix. But I can't figure it out. Here's the program...


#include
#include

int main()
{
float F, C, t, i;
t=1;
printf("Farenheit Celcius\n");
printf("-----------------------\n");
i = 0;
while (i <= 100)
{
F = i;
C = (5/9)*(F - 32);

printf("%1.2f" "%15.2f", F, C);
printf("\n");
i++;
}
}
Five answers:
Rad7
2012-11-20 20:27:55 UTC
Use this:

C = 5*(F - 32)/9;
John H
2012-11-20 22:09:33 UTC
Your problem has to do with the rules of promotion of types, specifically when integers are promoted to a floating point type.



C = (5/9)*(F - 32);



In the above expression, you'd like 5/9 to result in the number 0.55555555..., but it actually results in 0. This is because in the expression (5/9), no promotion of the 5 or 9 occurs, so integer division is performed, and the result is 0. What you need to do is change the expression so a floating point calculation is performed. The easiest way to do this is change the 5 or 9 or both to a floating point type. Then, the other argument will be promoted to a floating point type, and the calculation will be performed as a floating point divide. Simplest solution:



C = (5./9)*(F - 32);



The simple addition of a decimal point causes the 5 to become a double, rather than an int, and that then causes the 9 to be promoted to a floating point double, and floating point division is performed.



Another thing you could do is rearrange the expression:



C = (5 * (F - 32)) / 9;



In this case, the F-32 results in a float, and that causes the 5 to get promoted as well, and then when you divide by 9 that is done as floating point as well. But you have a problem with (5/9) because no promotion to a floating point type was performed, and the result of the division was 0.
James
2012-11-20 21:08:36 UTC
You know how!!!!
2016-12-29 13:06:25 UTC
of direction, the different selection may be to prompt and allow only numeric entries, somewhat of allowing them to sort regardless of they want and then telling them that they typed it incorrect. #comprise #comprise #define MAX_STRLEN 256 int considerable(int argc, char *argv[]) { double n = 0; int chPos = 0; char in[MAX_STRLEN], tmpch; printf("nEnter a huge selection : "); for (int n = 0; n < MAX_STRLEN; n++) { tmpch = fgetc(stdin); swap(tmpch) { case 'n' : in[chPos++] = '0'; n = MAX_STRLEN; wreck; default: if (isdigit(tmpch)) in[chPos++] = tmpch; wreck; } } printf("npercents enteredn",in); return 0; } Oh, and if reminiscence serves me appropriate, an sscanf() on the enter might enable a huge selection like this "153985gobbledegookhere" (without the rates of direction) to pass by way of ok.
motyka
2016-12-07 10:38:03 UTC
of path, the different decision could be to on the spot and permit purely numeric entries, relatively of allowing them to form despite the fact that they want and then telling them that they typed it incorrect. #contain #contain #define MAX_STRLEN 256 int significant(int argc, char *argv[]) { double n = 0; int chPos = 0; char in[MAX_STRLEN], tmpch; printf("nEnter a selection : "); for (int n = 0; n < MAX_STRLEN; n++) { tmpch = fgetc(stdin); change(tmpch) { case 'n' : in[chPos++] = '0'; n = MAX_STRLEN; smash; default: if (isdigit(tmpch)) in[chPos++] = tmpch; smash; } } printf("np.c.s enteredn",in); return 0; } Oh, and if memory serves me properly suited, an sscanf() on the enter might enable a selection like this "153985gobbledegookhere" (without the charges of path) to bypass by using ok.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...