Amazon.com Widgets ANSWERS TO CHAPTER 2 EXERCISES
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 16, 2014, 10:28:27 PM
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
| |-+ Exercises
| | |-+ Chapter 2
| | | |-+ ANSWERS TO CHAPTER 2 EXERCISES
Pages: [1] Go Down
Print
Author Topic: ANSWERS TO CHAPTER 2 EXERCISES (Read 5800 times)
iPhone Dev
Newbie
*
Posts: 4

Code on the Brain


Email




on: April 09, 2012, 05:03:59 PM

1. Answer: Use X-Code to type in the following programs: Page 7 Program 2.1; Page 22 Program 2.2; Page 23 Program 2.3; Page 23 Program 2.4; Page Program 2.5.  Build/Run and see the output in the debug window at the bottom.  Change your view to display if no debug window is showing up.

2. Answer: Not really a question.

3. Answer: NSLog will display "Testing......1...2...3" in the debug window.

4. Answer:
//
//  main.m
//  prog11
//
//  Created by BlueBirdApps.com on 4/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
       
        int a, b, answer;
       
        a = 15;
        b = 87;
       
        NSLog(@"The difference between %i and %i is %i", a, b, (b-a));

    }
    return 0;
}

5. Answer:
>  INT should be lowercase. 
>  "/* COMPUTE RESULT//" comment should be "//COMPUTE RESULT" or "/*COMPUTE RESULT */"
>"/DISPLAY RESULTS/" comment should be "//DISPLAY RESULT" or "/*DISPLAY RESULT*/"

6. Answer:  95  (100 - 10 +5)
Last Edit: April 23, 2012, 12:03:18 AM by iPhone Dev Logged
RitzClark
Newbie
*
Posts: 1


Email




Reply #1 on: May 12, 2012, 01:05:41 AM

This has really helped me a lot. I needed the answer for my current project. This forum is very informative.
Logged

gene438
Newbie
*
Posts: 2


Email




Reply #2 on: May 20, 2012, 03:10:06 PM

Sorry, but isn't the answer:


2012-05-20 17:08:22.052 prog1[821:403] Testing...
2012-05-20 17:08:22.053 prog1[821:403] ....1
2012-05-20 17:08:22.054 prog1[821:403] ...2
2012-05-20 17:08:22.054 prog1[821:403] ...3


should not each line be on a separate line?

This is for the answer for exercise 3

Not to be picky but trying to understand the /n and the use of nslog numerous times, etc. can be confusing
Logged
gene438
Newbie
*
Posts: 2


Email




Reply #3 on: May 20, 2012, 03:43:00 PM

For those anal for the exact answer to number 6:


2012-05-20 17:42:32.596 prog1[1235:403] The result is 95
Logged
JanryC
Newbie
*
Posts: 9


cabasaganjanry@ymail.com




Reply #4 on: July 01, 2012, 08:43:01 PM

1. Answer: Use X-Code to type in the following programs: Page 7 Program 2.1; Page 22 Program 2.2; Page 23 Program 2.3; Page 23 Program 2.4; Page Program 2.5.  Build/Run and see the output in the debug window at the bottom.  Change your view to display if no debug window is showing up.

2. Answer: Not really a question.

3. Answer: NSLog will display "Testing......1...2...3" in the debug window.

4. Answer:
//
//  main.m
//  prog11
//
//  Created by BlueBirdApps.com on 4/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
       
        int a, b, answer;
       
        a = 15;
        b = 87;
       
        NSLog(@"The difference between %i and %i is %i", a, b, (b-a));

    }
    return 0;
}

5. Answer:
>  INT should be lowercase. 
>  "/* COMPUTE RESULT//" comment should be "//COMPUTE RESULT" or "/*COMPUTE RESULT */"
>"/DISPLAY RESULTS/" comment should be "//DISPLAY RESULT" or "/*DISPLAY RESULT*/"

6. Answer:  95  (100 - 10 +5)


Thanks for starting this out, but I found something that needs attention,

NO.3:

my output is:

2012-07-02 11:26:41.223 prog1[8011:403] Testing...
2012-07-02 11:26:41.224 prog1[8011:403] ....1
2012-07-02 11:26:41.225 prog1[8011:403] ...2
2012-07-02 11:26:41.225 prog1[8011:403] ..3

and on the no.4, in mathematics, it isn't grammatically correct:
                  NSLog(@"The difference between %i and %i is %i", a, b, (b-a));
Output:      "The difference between 15 and 87 is 72
If interpreted & performed in elementary mathematics, the result is -72.

Back to the instruction:
4. Write a program that subtracts the value 15 from 87 and displays the result, together with an "appropriate message".
Thus, the  "appropriate message" must be:
"The difference between 87 and 15 is 72"

Code: NSLog(@"The difference between %i and %i is %i", b, a, (b-a));

Logged
jonr
Full Member
***
Posts: 120






Reply #5 on: February 02, 2013, 11:21:35 AM

For exercise 5 answers I've seen so far, there seems to be items missing on the list.  Here are the code mistakes I see:

-- INT should be lowercase
-- comment notation is wrong: should be either // @ the beginning or   /* <comment> */
-- there is a missing semicolon at the end of the statement where the variable sum is being initialized
-- another incorrect comment notation example...same answer as above for the other comment mistake.
-- NSLog text argument needs double quotes instead of the single quotes that are there.  There also is a missing comma between the two arguments    (the text string and the variable (sum).
Logged
Lisa Marie
Newbie
*
Posts: 4






Reply #6 on: July 31, 2013, 12:54:51 PM

I know this is an old thread, but I'm just making my way through this book and maybe this will help people just starting out like me.

The complete answer to #5 is this:
1. There shouldn't be a semicolon after 'int main (int argc, const char *argv[])'
2. Before @autoreleasepool, there is a parentheses which should be a curly brace.
3. INT should be lowercase.
4. Comment line starting with /* should start with //.
5. Should be a semicolon after 'sum = 25 + 37 - 19'.
6. The "Display Results" comment should start and end with //.
7. Inside NSLog routine, replace ' with " and put a comma between arguments.
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.