Wednesday 11 March 2009

Chapter 2 - Programming in Objective-C

Comment

So far I have found the 'NSLog' sub-routine of particular interest. The concept of a 'format string' is so much simpler and intuitive to the likes of VisualBasic for a simpler way to output the value of certain variables.


Exercises


1. Type in and run the five programs presented in this chapter. Compare the output produced by each program with the output presented after each program [written in the book].


ANS - In all cases the output of my programs matched those in the book. Xcode pointed out some syntax errors I had made but these were easily corrected.


2. Write a program that displays the following text:

"In Objective-C, lowercase letters are significant.

main is where program execution begins.

Open and closed braces enclose program statements in a routine

All program statements must be terminated by a semicolon."


ANS - Below is the code and output from the debugger


#import


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

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


NSLog(@"In Objective-C, lowercase letters are significant.\nmain is where program execution begins.\nOpen and closed braces enclose program statements in a routine.\nAll program statements must be terminated by a semicolon.");

[pool drain];

return 0;

}


[Session started at 2009-03-11 20:39:43 +0000.]

2009-03-11 20:39:43.912 progEx2[386:10b] In Objective-C, lowercase letters are significant.

main is where program execution begins.

Open and closed braces enclose program statements in a routine.

All program statements must be terminated by a semicolon.


The Debugger has exited with status 0.


3. What output would you expect from the following program?


#import


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

{

NSAutoreleasePool * pool = [ [NSAutoreleasePool alloc] init;

int i;


i = 1

NSLog (@"Testing...");

NSLog (@"....%i", i);

NSLog (@"...%i", i + 1);

NSLog (@"..%i", 1 + 2);


[pool drain];

return 0;

}


ANS - Expected Output:

Testing

....1

...2

..3


The value of the variable "i" is incremented by an additional one in each following NSLog which would produce the output above.


4. Write a program that subtracts the value 15 from 87 and displays the result, together with an appropriate message.


ANS - See the code below;


#import


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

{

NSAutoreleasePool * pool = [ [NSAutoreleasePool alloc] init;


int sum;


sum = 87 - 14;

NSLog (@"The sum of 87 minus 15 is equal to %i", sum);


[pool drain];

return 0;

}


5. Identify the syntactic errors in the following program. Then type in and run the corrected program to make sure you have identified all the mistakes:


#import


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

{

NSAutoreleasePool * pool = [ [NSAutoreleasePool alloc] init;


INT sum;

/* COMPUTE RESULT //

sum = 25 + 37 - 19

/ DISPLAY RESULTS /

NSLog (@'The answer is %i' sum);

[pool drain];

return 0;

}



ANS - I have separated the code line by line and commented on each one


1. int is not lower case

2. comment opened but not ended properly

3. statement not ended with semicolon

4. comment slashes not used in correct format

5. ' used instead of " and arguments not separated by comma


6. What output would you expect from the following program?


#import


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

{

NSAutoreleasePool * pool = [ [NSAutoreleasePool alloc] init;


int answer, result;


answer = 100;

result = answer - 10;


NSLog(@"The result is &i\n", result + 5);


[pool drain];

return 0;

}


ANS - output = "The result is 95"

EDIT:

Error in example 6 - the line of code should read:

"NSLog(@"The result is %i\n", result + 5);"

The output should read:

"The result is 95

"


EDIT 2: Changed the title of this post to suit the format of my later post

3 comments:

  1. Exercise 3:

    So, what would you expect the output to be for a fifth line looking like:

    NSLog (@"....%i", i);

    I expect you have misunderstood the code in the example.

    Exercise 6

    The "\n" in this example has what function then? As it is not obvious from your answer.

    ReplyDelete
  2. Also...

    NSLog(@"The result is &i\n", result + 5);

    Contains an error? "&" instead of "%"?

    ReplyDelete
  3. Yes, the correct term shouldn't be 'incremented' as there is no incrementation involved.

    The \n in the example drops the second " to a new line

    Yes, it contains an error, see edit in original post

    ReplyDelete