-3

Every day, a weather station receives 5 temperatures expressed in degree Fahrenheit. Write a program to accept temperature in Fahrenheit, convert it to Celsius and display the converted temperature on the screen. After 5 temperatures, the message ‘All temperatures processed’ should be displayed on the screen

My answer is below

#include <stdio.h>
main()
{
  int fah, cel;
  printf("\nEnter 5 temperatures in Fahrenheit: ");
  scanf("%d %d %d %d %d", &fah);
  do
  { 
    cel=(fah-32)*(5/9);
    printf("\n These temperatures converted to Celsius are: %d \n", cel);
  }
  while(fah);
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
gadoora
  • 1
  • 1
  • 1
  • WHERE DID I GO WRONG ? You didn't post precisely what your problem is ? – Brian Agnew Aug 20 '13 at 08:50
  • What exactly is your problem? (apart from the infinite loop) – Frank Schmitt Aug 20 '13 at 08:50
  • Well, that program, apart from being buggy, doesn't do what is asked for. You were asked to terminate after 5 temperatures, not after a zero input, and you were asked to print a status message. – Tom Tanner Aug 20 '13 at 10:00
  • This is already your third homework in a row, that you try to get solved on SO. This site is to answer concrete technical questions, not to review your homework. Please stop it. Voting to close. – Jens Gustedt Aug 20 '13 at 12:55

3 Answers3

3
scanf("%d %d %d %d %d", &fah);

You used 5 %d in the conversion specifier, but only one variable.

cel=(fah-32)*(5/9);

Here, you used integer division, change the type of the variables to double and 5/9 to 5.0/9

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Can i get the direct solution?? mine is totally messed up i know, but im confused and i tried maybe if u tell me ill be able to understand – gadoora Aug 20 '13 at 09:06
  • @gadoora - read the good answers and work at it. It's the only way to learn this stuff well, honest! Hint - break up your problem - get it to work with one value first, then think about extending it to five. – Martin James Aug 20 '13 at 09:50
  • @gadoora Martin's right, it's better that you tried it yourself step by step. – Yu Hao Aug 20 '13 at 13:00
3

Lets take this one by one -

  • scanf("%d %d %d %d %d", &fah); : You want to scan five values while you are using only one %d. What about the other four values ? You should use an array of 5 values.
  • cel=(fah-32)*(5/9); : You are dividing 5 by 9, both of which are integers. The result of dividing an integer by another, a/b, where a < b is 0. Hence cel will always be 0. You have to cast one of the integers to a float/double.
  • If you do use an array, you need to loop over all elements of the array to process them.

I am purposely not providing a direct solution to all these. I suggest you look up on these things and understand them, since they are very basic for programming in C.

Cygnus
  • 3,222
  • 9
  • 35
  • 65
1

You're working in integers --> 5/9 is converted to integer 0 thus the result will also equal to 0. You need to work with float/double types or change your calculation to minimize such rounding errors, e.g. something like this: cel=(5*(fah-32))/9; - here the multiplication is done first and division after that.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85