Thread: Unable to calculate float values

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    Unable to calculate float values

    Hi.

    I'm very new to the C programming language. I am using "The C Programming Language (2nd Edition)" by Kernighan and Ritchie with the Apple Xcode IDE. I am trying to complete Exercise 1-4 found on page 13 modified with float values instead of int values. My code is found first below and resulting output is found below that.

    Code:
    #include <stdio.h>
    
    main()
    {
    	float fahr, celsius;
    	int lower, upper, step;
    	
    	lower = 0;
    	upper = 300;
    	step = 20;
    	
    	celsius = lower;
    	while (celsius <= upper) {
    		fahr = ((celsius * 9.0) / 5.0) + 32.0;
    		printf("%6.2f\t%6.2f \n", celsius, fahr);
    		celsius = celsius + step;
    	}
    
    return 0;
    
    }
    Code:
    [Session started at 2005-12-08 17:14:28 -0500.]
      0.00	 32.00 
     20.00	 68.00 
     40.00	104.00 
     60.00	140.00 
     80.00	176.00 
    100.00	212.00 
    120.00	248.00 
    140.00	284.00 
    160.00	320.00 
    180.00	356.00 
    200.00	392.00 
    220.00	428.00 
    240.00	464.00 
    260.00	500.00 
    280.00	536.00 
    300.00	572.00 
    
    KR has exited with status 0.
    As you can see, the output values are float but are rounded to int values. My Build Results console gives me one warning - "warning: return type defaults to 'int'." Can someone help me locate the source of my problem.

    Thanks a lot in advance!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Because the C to F equation yields whole numbers when counting by 20.

    Change step to something like 8 to see some numbers on the left side of the decimal.

    ...and your warning is coming from main()

    Code:
    int main()
    Last edited by SlyMaelstrom; 12-08-2005 at 06:09 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    8
    Thank you sir!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  2. Reflective Factory Design
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 12-16-2005, 06:50 PM
  3. Pointer
    By silicon in forum C Programming
    Replies: 2
    Last Post: 03-25-2004, 02:34 PM
  4. Einstine calculator Mark 2
    By Mach_ie in forum C Programming
    Replies: 1
    Last Post: 08-31-2003, 01:54 PM
  5. Einstine calculator
    By Mach_ie in forum C Programming
    Replies: 2
    Last Post: 08-30-2003, 09:37 AM