Amazon.com Widgets Misprint in Prog 6.10 (NOT Ex. 6.10 which doesn't exist)
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2013, 02:46:00 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 6
| | |-+  Misprint in Prog 6.10 (NOT Ex. 6.10 which doesn't exist)
Pages: [1]   Go Down
Print
Author Topic: Misprint in Prog 6.10 (NOT Ex. 6.10 which doesn't exist)  (Read 1166 times)
ljaygould
Newbie
*
Posts: 16






« on: January 13, 2012, 09:07:47 PM »

One of the ways I learn from this great book is to try to type the program examples in (and often play with them to see what happens, etc.)

When doing this one, I got the strangest result: numerous (but not a consistent number) of iterations of each prime, along with a couple of wrong ones.

The reason: Count the "}"s. There's one missing (which Xcode of course put in very nicely, but in the wrong place).

There needs to be a "}" somewhere before the last if  statement, if I'm right, so that the four "{"s are properly matched...comments?

Logged
ljaygould
Newbie
*
Posts: 16






« Reply #1 on: January 17, 2012, 08:37:38 AM »

I should point out that I'm using the Kindle edition of the book; this misprint (missing "}") may not occur in print editions...
Logged
Chris W. Rea
Newbie
*
Posts: 2



WWW




« Reply #2 on: February 10, 2012, 01:16:07 PM »

I can confirm the misprint you found is also in the print 4th edition, specifically First Printing, December 2011.  Judging from the indentation, it appears the inner-most for loop is missing the close brace.
Logged
elkie13
Newbie
*
Posts: 17






« Reply #3 on: February 15, 2012, 03:51:28 PM »

Yep looks like the second for loop only executes the first if statement, not both.  So there needs to be a close brace after the first if statement.
Logged
jimmac
Newbie
*
Posts: 13






« Reply #4 on: March 03, 2012, 10:14:46 PM »

Even after reading the above explanations, I struggled with this a bit. So I though I'd post how I believe Program 6.10 should appear. (As per the book's style, only required braces are shown.)

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

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        int p, d, isPrime;
       
        for (p=2; p<=50; ++p) {
            isPrime = 1;
           
            for (d=2; d<p; ++d) {
                if (p % d == 0)
                    isPrime = 0;
            }
            if (isPrime !=0)
                NSLog(@"%i ", p);
        }
    }
    return 0;
}
Logged
Bunchadna
Newbie
*
Posts: 27






« Reply #5 on: April 16, 2012, 03:50:51 PM »

jimmac, your program looks like that's where the closed brace needs to go.  Also note, that if you simply delete the open brace that follows the second for statement, the program still works.  See my program below.

Including the brace makes the program more readable, though.

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.