Sunday 19 April 2009

Chapter 5 - Program Looping

Comment

As you have probably noticed, my faithful readers, there have been no updates on the Mondays of the 5th and 12th of April. This is due to delegating my time to other 'activities' during the Easter vacation period. I hope this hasn't been an inconvenience to you all and you find my effects dedicated to this particular chapter satisfactory. Thanks again for reading.


This chapter on loops was fairly straightforward to understand since looping is a concept native in nearly every programming language (the small number that I know of anyway ;) ). The specific Objective-C syntax was not too difficult to comprehend and I feel I have executed it quite well in the following examples.


Examples

1. Write a program to generate and display a table of n and n^2, for integer values of n ranging from 1 through 10. Be sure to print the appropriate column headings.


ANS - Copy and Paste of code and console below


#import


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

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

int n, n_squared;

NSLog (@"Table of numbers and their squared equivelents");

NSLog (@" n n^2");

NSLog (@"--- -----");

n_squared = 0;

for ( n = 1; n <= 10; ++n ) {

n_squared = n * n;

NSLog (@" %2i %i", n, n_squared);

}

[pool drain];

return 0;

}


[Session started at 2009-04-13 12:11:32 +0100.]

2009-04-13 12:11:32.971 Ex1[348:10b] Table of numbers and their squared equivelents

2009-04-13 12:11:32.973 Ex1[348:10b] n n^2

2009-04-13 12:11:32.974 Ex1[348:10b] --- -----

2009-04-13 12:11:32.974 Ex1[348:10b] 1 1

2009-04-13 12:11:32.974 Ex1[348:10b] 2 4

2009-04-13 12:11:32.975 Ex1[348:10b] 3 9

2009-04-13 12:11:32.975 Ex1[348:10b] 4 16

2009-04-13 12:11:32.975 Ex1[348:10b] 5 25

2009-04-13 12:11:32.976 Ex1[348:10b] 6 36

2009-04-13 12:11:32.976 Ex1[348:10b] 7 49

2009-04-13 12:11:32.976 Ex1[348:10b] 8 64

2009-04-13 12:11:32.977 Ex1[348:10b] 9 81

2009-04-13 12:11:32.977 Ex1[348:10b] 10 100


The Debugger has exited with status 0.


2. A triangular number can also be generated for any integer value of n by this formula: triangularNumber = n (n + 1) / 2


For example, the 10th triangular number, 55, can be calculated by substituting 10 as the value for n into the previous formula. Write a program that generates a table of triangular numbers using the previous formula. Have the program generate every fifth triangular number between 5 and 50 (that is, 5, 10, 15, ..., 50).


ANS - Copy and Paste of code and console below


#import


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

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

int n, triangularNumber;

for (n = 5; n <= 50; n = n + 5) {

triangularNumber = n * (n + 1) / 2;

NSLog (@"TN%i = %i", n, triangularNumber);

}

[pool drain];

return 0;

}


[Session started at 2009-04-13 12:19:42 +0100.]

2009-04-13 12:19:42.610 Ex2[394:10b] TN5 = 15

2009-04-13 12:19:42.613 Ex2[394:10b] TN10 = 55

2009-04-13 12:19:42.613 Ex2[394:10b] TN15 = 120

2009-04-13 12:19:42.614 Ex2[394:10b] TN20 = 210

2009-04-13 12:19:42.614 Ex2[394:10b] TN25 = 325

2009-04-13 12:19:42.615 Ex2[394:10b] TN30 = 465

2009-04-13 12:19:42.616 Ex2[394:10b] TN35 = 630

2009-04-13 12:19:42.616 Ex2[394:10b] TN40 = 820

2009-04-13 12:19:42.617 Ex2[394:10b] TN45 = 1035

2009-04-13 12:19:42.617 Ex2[394:10b] TN50 = 1275


The Debugger has exited with status 0.


3. The factorial of an integer n, written n!, is the product of the consecutive integers 1 through n. For example, 5 factorial is calculated as follows:

5! = 5 x 4 x 3 x 2 x 1 = 120

Write a program to generate and print a table of the first 10 factorials.


ANS - This one was tricky and I devised this solution, but it doesn't appear to work and I'm unsure as to why.


#import


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

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

int n, n_facto, temp;

n_facto = 0;

temp = 0;

NSLog (@"TABLE OF FACTORIALS");

NSLog (@"n n!");

NSLog (@"-- --");

for ( n = 1; n <= 10; ++n ) {

for ( temp = n; temp == 0; --temp )

n_facto *= temp; //isolated problem to here

NSLog(@"%2i %i", n, n_facto);

}


[pool drain];

return 0;

}


[Session started at 2009-04-19 23:20:07 +0100.]

2009-04-19 23:20:07.794 Ex3[1386:10b] TABLE OF FACTORIALS

2009-04-19 23:20:07.796 Ex3[1386:10b] n n!

2009-04-19 23:20:07.797 Ex3[1386:10b] -- --

2009-04-19 23:20:07.797 Ex3[1386:10b] 1 0

2009-04-19 23:20:07.798 Ex3[1386:10b] 2 0

2009-04-19 23:20:07.798 Ex3[1386:10b] 3 0

2009-04-19 23:20:07.799 Ex3[1386:10b] 4 0

2009-04-19 23:20:07.799 Ex3[1386:10b] 5 0

2009-04-19 23:20:07.800 Ex3[1386:10b] 6 0

2009-04-19 23:20:07.800 Ex3[1386:10b] 7 0

2009-04-19 23:20:07.801 Ex3[1386:10b] 8 0

2009-04-19 23:20:07.801 Ex3[1386:10b] 9 0

2009-04-19 23:20:07.801 Ex3[1386:10b] 10 0


The Debugger has exited with status 0.


4. A minus sign placed in front of a filed width specification causes the field to be displayed left-justified. Substitute the following NSLog statement for the corresponding statement in Program 5.2, run the program, and compare the outputs produced by both programs:

NSLog (@"%-2i %i", n, triangularNumber);


ANS - A copy of the modified Program 5.2 and the console outputs from the original Program 5.2 and the modified Program 5.2.


// Program to calculate the 200th triangular number

//Introduction of the for statement


#import


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

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


int n, triangularNumber;

triangularNumber = 0;

for ( n = 1; n <= 200; n = n + 1 )

triangularNumber += n;

// Original statement - NSLog (@"The 200th triangular number is %i", triangularNumber);

NSLog (@"%-2i %i", n, triangularNumber);

[pool drain];

return 0;

}


-Original Program 5.2

[Session started at 2009-04-19 18:11:42 +0100.]

2009-04-19 18:11:42.830 Ex4[296:10b] The 200th triangular number is 20100


The Debugger has exited with status 0.


-Modified Program 5.2

[Session started at 2009-04-19 18:12:49 +0100.]

2009-04-19 18:12:49.947 Ex4[312:10b] 201 20100


The Debugger has exited with status 0.


-Comparison

I don't see how the outputs differ except that the second output also produces the value of n at the end of the loop which is supposed to be left-justified.


5. Program 5.5 allows the user to type in only five different numbers. Modify that program so that the user can type in the number of triangular numbers to be calculated.


ANS - Copy of modified Program 5.5 and its console output. Comparisons identified between original and modified program.


#import


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

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


int n, number, calcNumber, triangularNumber, counter;

//These two lines added by me

NSLog (@"How many triangular numbers do you want to be calculated?");

scanf ("%i", &calcNumber);

for ( counter = 1; counter <= calcNumber; ++counter) { //loop condition set to new variable

NSLog (@"What triangular number do you want?");

scanf ("%i", &number);

triangularNumber = 0;

for (n = 1; n <= number; ++n );

triangularNumber += n;

NSLog (@"Triangular number %i is %i", number, triangularNumber);

}

[pool drain];

return 0;

}


[Session started at 2009-04-19 21:41:31 +0100.]

2009-04-19 21:41:31.908 Ex5[277:10b] How many triangular numbers do you want to be calculated?

1

2009-04-19 21:41:34.402 Ex5[277:10b] What triangular number do you want?

5

2009-04-19 21:41:35.761 Ex5[277:10b] Triangular number 5 is 6


The Debugger has exited with status 0.



6. Rewrite Program 5.2 through 5.5, replacing all uses of the for statement with equivalent while statements. Run each program to verify that both versions are identical.


ANS - Copy of all uses of the for statement within each program paired with its equivalent while statement. All substitutions have worked unless specified individually.


//Program 5.2

for ( n = 1; n <= 200; n = n + 1 )

triangularNumber += n;

n = 1;

while (n <= 200) {

triangularNumber += n;

n = n + 1;

}


//Program 5.3

for ( n = 1; n <= 10; ++n ) {

triangularNumber += n;

NSLog (@" %i %i", n, triangularNumber);

}

n = 1;

while ( n <= 10 ) {

triangularNumber += n;

NSLog (@" %i %i", n, triangularNumber);

++n;

}



//Program 5.4

for (n = 1; n <= number; ++n )

triangularNumber += n;

n = 1;

while ( n <= number ) {

triangularNumber += n;

++n;

}


//Program 5.5 (It's a long one)

for ( counter = 1; counter <= 5; ++counter) {

NSLog (@"What triangular number do you want?");

scanf ("%i", &number);

triangularNumber = 0;

for (n = 1; n <= number; ++n );

triangularNumber += n;

NSLog (@"Triangular number %i is %i", number, triangularNumber);

}

counter = 1;

while ( counter <= 5 ) {

NSLog (@"What triangular number do you want?");

scanf ("%i", &number);

triangularNumber = 0;

n = 1;

while ( n <= number ) {

triangularNumber += n;

++n;

}

NSLog (@"Triangular number %i is %i", number, triangularNumber);

++counter;

}


7. What would happen if you typed a negative number into Program 5.8? Try it and see.


ANS - Copy of Program 5.8 and its console output when input with a positive number and then a negative number.


#import


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

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


int number, rightDigit;

NSLog (@"Enter your number");

scanf ("%i", &number);

while ( number != 0 ) {

rightDigit = number % 10;

NSLog (@"%i", rightDigit);

number /= 10;

}


[pool drain];

return 0;

}


[Session started at 2009-04-19 22:23:48 +0100.]

2009-04-19 22:23:48.585 Ex7[738:10b] Enter your number

786

2009-04-19 22:23:52.128 Ex7[738:10b] 6

2009-04-19 22:23:52.129 Ex7[738:10b] 8

2009-04-19 22:23:52.130 Ex7[738:10b] 7


The Debugger has exited with status 0.

[Session started at 2009-04-19 22:24:02 +0100.]

2009-04-19 22:24:02.439 Ex7[739:10b] Enter your number

-786

2009-04-19 22:24:06.440 Ex7[739:10b] -6

2009-04-19 22:24:06.441 Ex7[739:10b] -8

2009-04-19 22:24:06.441 Ex7[739:10b] -7


The Debugger has exited with status 0.


8. Write a program that calculates the sum of the digits of an integer. For example, the sum of the digits of the number 2155 is 2+ 1 + 5 + 5, or 13. The program should accept any arbitrary integer the user types.


ANS - Copy of Program and its console output. ( @d. - I was unable to get this one to work either and I'm unsure as to why. I suspect it has something to do with assignment operators within loops. I will bring both examples in on Monday for you to have a look.)


#import


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

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


int number, total;

NSLog (@"Enter your number");

scanf ("%i", &number);

while ( number != 0 ) {

total += number % 10; //isolated the problem to here

number /= 10;

}

NSLog (@"Result: %i", total);

[pool drain];

return 0;

}


The Debugger has exited with status 0.

[Session started at 2009-04-19 22:45:00 +0100.]

2009-04-19 22:45:00.363 Ex8[993:10b] Enter your number

55

2009-04-19 22:45:11.647 Ex8[993:10b] Result: 4106 (- Not the correct result.)


The Debugger has exited with status 0.

4 comments:

  1. (3) Factorials

    The line "n_facto *= temp;" is equivalent to "n_facto = n_facto * temp;", and since n_facto is intialised to 0, it will always remain 0.

    (5) The formula for triangular numbers should be "n (n + 1) / 2" as described in excercise 5.2 I think.

    (7) Did you know what would happen in this exercise in advance, or did you just run the program to check? Also, do you think this is the correct behaviour?

    (8) Start by declaring total to be a value at the start, helps to avoid unexpected results.

    ReplyDelete
  2. (5) Watch for misplaced semi-colon, and try to ensure you are consistent with your indentation and use of braces.

    ReplyDelete
  3. (3) Still can't get this to work, still attempting, so will get back to you

    (5) Task 5 asks to modify the original program where they use a different loop orientated method to calculate the triangular numbers. But your right, the wording is slightly confusing.

    (7) I expected the output to consist of a negative sign preceding the final reversed digit. I should have included a statement to indicate what I expected compared to the actual output.

    (8) This is sound advice and I will make sure to do so in future

    (5) misplaced semi-colon noted. I was only producing the code as it was structured in the book which doesn't include braces for nested loops but in hindsight this seems also a good practice to adopt.

    ReplyDelete
  4. (3) Just checked again and it seems to be working. It was also working when I posted my previous post but did not realise.

    -Everything now A.O.K

    ReplyDelete