Thread: Code Question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Central Coast, California
    Posts
    9

    Code Question

    Hello, I'm beginning C programming and am having a little problem with this program of mine. The program is to take entered temperatures, which it does correctly, average them, also does correctly, then covert them to Celsius and then average those...which it does not to correctly...yet I cannot grasp the issue with my code. Any thoughts?
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	double fah1, fah2, fah3, fah4, fah5;
    	double cel1, cel2, cel3, cel4, cel5;
    	printf ("Please enter the first temperature in degrees fahrenheit -> ");
    	scanf ("%lf", &fah1);
    	printf ("Please enter the second temperature in degrees fahrenheit -> ");
    	scanf ("%lf", &fah2);
    	printf ("Please enter the third temperature in degrees fahrenheit -> ");
    	scanf ("%lf", &fah3);
    	printf ("Please enter the fourth temperature in degrees fahrenheit -> ");
    	scanf ("%lf", &fah4);
    	printf ("Please enter the fifth temperature in degrees fahrenheit -> ");
    	scanf ("%lf", &fah5);
    
    	printf ("The five temperatures (in degrees fahrenheit) that you entered are -> %.1lf %.1lf %.1lf %.1lf %.1lf\n\n",  fah1, fah2, fah3, fah4, fah5);
    
    	printf ("The average of the five temperatures (in degrees fahrenheit) is -> %.lf\n\n", (fah1 + fah2 + fah3 + fah4 + fah5) / 5);
    	
    	printf ("The first temperature entered in degrees Celsius is -> %.1lf\n", cel1=(fah1 - 32) * (5 / 9));
    	printf ("The second temperature entered in degrees Celsius is -> %.1lf\n", cel2=(fah2 - 32) * (5 / 9));
    	printf ("The third temperature entered in degrees Celsius is -> %.1lf\n", cel3=(fah3 - 32) * (5 / 9));
    	printf ("The fourth temperature entered in degrees Celsius is -> %.1lf\n", cel4=(fah4 - 32) * (5 / 9));
    	printf ("The fifth temperature entered in degrees Celsius is -> %.1lf\n", cel5=(fah5 - 32) * (5 / 9));
    
    	printf ("The five temperatures in degrees Celsius are -> %.1lf %.1lf %.1lf %.1lf %.1lf\n\n",  cel1, cel2, cel3, cel4, cel5);
    
    	printf ("The average of the five temperatures in degrees Celsius is -> %.1lf\n\n", (cel1 + cel2 + cel3 + cel4 + cel5) / 5);
    	
    return 0;
    }
    The program runs fine with the exception of a myriad of 0's where there should not be.

    Thank you

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    5 / 9 does integer division, the result of which is zero; use 5.0 / 9.0 instead.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    5 / 9 is 0
    5.0 / 9.0 isn't

    Dagnabbit, that's twice Dave's beaten me today
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Location
    Central Coast, California
    Posts
    9
    Thank you so much! I have learned and I will keep that in mind in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi all i have a question about a code
    By iweapons in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2007, 11:05 AM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. My First If Code, Question? :P
    By dimirpaw in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2005, 08:49 PM
  4. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM
  5. question about my code
    By killerasp in forum C++ Programming
    Replies: 7
    Last Post: 02-18-2002, 08:05 PM