Amazon.com Widgets Chapter 2: Exercices program
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 07:13:45 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
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 2
| | | |-+  Chapter 2: Exercices program
Pages: [1] 2   Go Down
Print
Author Topic: Chapter 2: Exercices program  (Read 11275 times)
jerome
Newbie
*
Posts: 48






« on: February 14, 2009, 03:45:18 AM »

CU Jerome
Logged
mdeh
Full Member
***
Posts: 166






« Reply #1 on: February 14, 2009, 05:43:07 AM »

Jerome,
Just post your code in plain, with the appropriate tags. I think people are a little reluctant to download zipped files from unknown sources...although I am sure yours would be fine.
Logged
jerome
Newbie
*
Posts: 48






« Reply #2 on: February 14, 2009, 06:34:08 AM »

Hi mdeh,

My feeling is that it's awfully painfull to have the source code in the post as it makes it a bit messy (where is the code and where are the comment). Of course nobody is obliged to trust me and download the file.

How much people just give their password to a program installer and don't know what's happen in it? I can live with the idea that the previouly mentionned people feel safer when retyping by hand the examples.

CU Jerome
Logged
mdeh
Full Member
***
Posts: 166






« Reply #3 on: February 14, 2009, 06:48:13 AM »

Hi mdeh,

My feeling is that it's awfully painfull to have the source code in the post as it makes it a bit messy


Code: (Objective-C)
(where is the code
and
Quote
where are the comment)  // or /*......*/
.

It's quite easy once you get the hang of it. But, as you say, everyone is different.
Logged
Kari
Newbie
*
Posts: 3






« Reply #4 on: February 14, 2009, 09:04:21 AM »

Personally I prefer to read the code within the body of a message.  I am not inclined to download code in a zipped file from anyone other than the author of the book.  Exercise 1 is as follows:

Program 2.1--
Code: (Objective-C)
//First program example

#import <Foundation/Foundation.h>

int main (int argc, constr char * argv[])

{
     NSAutoreleasePool * pool = [[NSAutorelease Pool alloc] init];
     NSLog (@"Programming is fun!";
     
     [pool drain];
     return 0;
}


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

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

{
     NSAutoreleasePool * pool [[NSAutoreleasePool alloc] init];

     NSLog (@"Programming is fun!");
     NSLog (@"Programming in Objective-C is even more fun!");

     [pool drain];
     return 0;
}

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

int main (int argc, const char * argv[])
{
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     
     NSLog (@"Testing... \n..1\n...2\n....3");
     [pool drain];
     return 0;


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

int main (int argc, const char * argv[])
{
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

     int sum;
     
     sum = 50 + 25;
     NSLog (@"The sum of 50 and 25 is %i", sum);
     [pool drain];
     
     return 0;
}

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

int main (int argc, const char * argv[])
{
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     int value1, value2, sum;
     
     value 1 = 50;
     value 2 = 25;
     sum = value1 + value2;

     NSLog (@"The sum of %i and %i is %1", value1, value2, sum);
     
     [pool drain];
     return 0;
}

The exercise asks that we compare the output produced by each program.  Maybe later I will edit this post to include the output.
Logged
Kari
Newbie
*
Posts: 3






« Reply #5 on: February 14, 2009, 09:14:36 AM »

Chapter 2, Exercise 2 (page 25):

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

int main (int argc, const char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSLog (@"In Objective-C, lowercase letters are significant.\n main is where program execution
    begins.\n Open and closed braces enclose program statements in a routine.\n
    All program statements must be terminated by a semicolon.");
    [pool drain];
    return 0;
}

Logged
jkiley
Newbie
*
Posts: 43



Email




« Reply #6 on: April 09, 2009, 07:08:26 PM »

Chapter 2, Exercise 4 (p. 26)

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

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // Subtracts 15 from 87 and displays the output.

int sum;

sum = 87 - 15;

    NSLog(@"The difference between 87 and 15 is %i.", sum);
    [pool drain];
    return 0;
}
Logged
jkiley
Newbie
*
Posts: 43



Email




« Reply #7 on: April 09, 2009, 07:10:19 PM »

Chapter 2, Exercise 5 (p. 26)

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

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // Corrects Chapter 2 Exercise 5 as instructed.

int sum;

// Compute result

sum = 25 + 37 - 19;

// Display results.

    NSLog(@"The answer is %i", sum);
    [pool drain];
    return 0;
}
Logged
Intyre3
Guest




« Reply #8 on: April 13, 2009, 04:58:27 PM »

Programming in Objective-C 2.0

Chapter 2. Exercises

 See prog1, Program 2.3, Program 2.4, and Program 2.5 at Desktop/Obective-C Projects.
 See C2E2 at Desktop/Objective-C Projects.
 Testing...
     ....1
     ...2
     ..3
See C2E4 at Desktop/Objective-C Projects
See C2E5 at Desktop/Objective-C Projects
    INT should be int
    sum = 25 + 37 - 19   should be   sum = 25 + 37 - 19;
    NSLog(@’ The answer is %i’ sum);   should be   NSLog(@”The answer is %i” , sum);
95

NOTE: Exercises 1, 2, 4 and 5 are the typed programs on my computer.  They are not included in these answers.
Logged
mylifeis24
Newbie
*
Posts: 12


Email




« Reply #9 on: May 06, 2009, 01:58:57 PM »

Hello,

I am just starting the book and so far I am doing fine, but when I type my code in for exercise 2.5, I always get an error that says...

Quote
error: syntax error before 'value2'

Below is the whole code with the error, but it is the same as the one in Program 2.5 on pg. 24.

Quote
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   int value1, value2, sum;
   value1 = 50
   value2 = 25
//This is where the error appears
   sum = value1 + value2;
   NSLog(@"The sum of %i and %i is %i", value1, value2, sum);
   
   [pool drain];
   return 0;
}

Thank you for any help you may provide.
Logged
EnzoBro
Newbie
*
Posts: 10






« Reply #10 on: May 06, 2009, 04:28:53 PM »

@ mylifeis24

you forgot to put the semicolon at the end of your statements

value1 = 50;
value2 = 25;
Logged
mylifeis24
Newbie
*
Posts: 12


Email




« Reply #11 on: May 06, 2009, 04:32:53 PM »

@ mylifeis24

you forgot to put the semicolon at the end of your statements

value1 = 50;
value2 = 25;


Wow.... I spent a good 10 minutes looking over that short piece of code and missed that! Even for someone new to this that is a little frightening. Thanks a lot.
Logged
EnzoBro
Newbie
*
Posts: 10






« Reply #12 on: May 06, 2009, 05:45:25 PM »

You are not alone, I spent 20 minutes in the Book and Xcode and another 20 minutes in these forums looking for an error in Program 3.2.
Only to find out i typed:
myfunction
instead of
myFunction

I am new like you and we will get through this. One error at a time.

Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #13 on: May 06, 2009, 08:14:46 PM »

This is a valuable part of the learning process, which I why I discourage new programmers from just copying and pasting the program examples into Xcode.

Cheers,

Steve
Logged
mylifeis24
Newbie
*
Posts: 12


Email




« Reply #14 on: May 07, 2009, 03:21:59 AM »

This is a valuable part of the learning process, which I why I discourage new programmers from just copying and pasting the program examples into Xcode.

Cheers,

Steve

I try and read the description before I enter the code, so that as I enter it, I can understand what it is I am typing!

Its a great book, great tool for beginners and this site is most certainly extremely helpful to anyone with questions or an idea.
Logged
Pages: [1] 2   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.