Amazon.com Widgets Chapter 4 Exercise 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 22, 2014, 01:09:26 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 4
| | |-+ Chapter 4 Exercise 2
Pages: [1] Go Down
Print
Author Topic: Chapter 4 Exercise 2 (Read 968 times)
jacob3au
Newbie
*
Posts: 2


Email




on: December 03, 2012, 04:56:46 PM

I'm new to programming and have several books on Objective-C. After searching around for weeks, someone recommended this one. I was finally able to understand what an instance of a class meant, etc. The explanations are perfect, but the exercises are torture. I thought I was doing great up until the Chapter 4 Exercises. The thing that confuses me is that the author doesn't guide you through starting the project.

Exercise 2 says

"Write a program that converts 27 degrees from degrees Fahrenheit (F) to degrees Celsius (C) using the following formula:

C = (F - 32) / 1.8

Note that you don't need to define a class to perform this calculation. Simply evaluating the expression will suffice."

I have no clue what to do.
This is what I came up with:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        {
            char C;
            float F;
            C = 'C';
            F = 27;
            NSLog(@"Converting 27 degrees Fahrenheit to degrees Celsius: %d", C = (F - 32) / 1.8 );
       
    }
    return 0;
}
}


For some reason it wouldn't let me use the %f format specifier because it said "format specifier says type double but contains char." Underlined in red was the formula C = (F-32) / 1.8 );
Logged
Tpdef
Newbie
*
Posts: 7






Reply #1 on: December 03, 2012, 05:31:24 PM

 char C;// first of all you dont need this,use NSLog insted

let's take a looke at your NSLog

 NSLog(@"Converting 27 degrees Fahrenheit to degrees Celsius: %d", C = (F - 32) / 1.8 );
C = (F - 32) / 1.8 )

the right side of the equation evaluate as a float,and you are assigning the result
of the expression to a char c variable ,does that make sense to you?

you did ok,besides the char thing,and change the %d to %f because your answer is a float type.
Logged
jacob3au
Newbie
*
Posts: 2


Email




Reply #2 on: December 03, 2012, 06:34:06 PM

I was thinking I didn't need the char C, so I tried int first but then realized that I wasn't supposed to assign an integer to C. Now I have it as float C; and that works just fine.

Thanks for the quick reply, I changed the code to this:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        {
            float C;
            float F;
            F = 27.0;
           
    NSLog(@"Converting 27 degrees Fahrenheit to degrees Celsius: %f", C = (F - 32) / 1.8 );
       
    }
    return 0;
}
}

and it gives me the right result.

If you don't mind, could you tell me how you would have written this code or how you would have executed this program?
Logged
Tpdef
Newbie
*
Posts: 7






Reply #3 on: December 04, 2012, 06:05:49 AM

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        NSLog(@"27 degrees Fahrenheit is %f degrees Celsius", (27 - 32) / 1.8 );       
    }
    return 0;
}
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.