Amazon.com Widgets Exercise 5.2 - This took me ages...
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 07:30:56 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 5
| | | |-+  Exercise 5.2 - This took me ages...
Pages: [1]   Go Down
Print
Author Topic: Exercise 5.2 - This took me ages...  (Read 693 times)
johnyorke
Newbie
*
Posts: 18


Email




« on: December 29, 2011, 02:19:21 PM »

I tried so hard to complicate this and in the end it took me close to 3 hours to figure out. I was trying to start the factorial from 0 each time and add 1 until it reached the number variable. This became very complicated. It only became apparent quite late on that actually the result for the previous factorial was cumulative and therefore could be used again.
Code: (Objective-C)
// Chapter 5 - Program Looping - Exercise 3
// Program that generates a table of the first 10 factorials

int main (int argc, char *argv [])
{
int number, factorial;

NSLog (@"Number       Factorial");
NSLog (@"------       ---------");
   
for (number = 1, factorial = 1; number <= 10; number++) {
        factorial = factorial * number;
        NSLog(@"%2i           %i",number,factorial);
}
return 0;
}
Output:
Quote
2011-12-29 21:09:35.479 exercises[2211:707] Number       Factorial
2011-12-29 21:09:35.481 exercises[2211:707] ------       ---------
2011-12-29 21:09:35.482 exercises[2211:707]  1           1
2011-12-29 21:09:35.483 exercises[2211:707]  2           2
2011-12-29 21:09:35.483 exercises[2211:707]  3           6
2011-12-29 21:09:35.484 exercises[2211:707]  4           24
2011-12-29 21:09:35.484 exercises[2211:707]  5           120
2011-12-29 21:09:35.485 exercises[2211:707]  6           720
2011-12-29 21:09:35.486 exercises[2211:707]  7           5040
2011-12-29 21:09:35.486 exercises[2211:707]  8           40320
2011-12-29 21:09:35.487 exercises[2211:707]  9           362880
2011-12-29 21:09:35.487 exercises[2211:707] 10           3628800
Logged
SMcD
Newbie
*
Posts: 1






« Reply #1 on: December 29, 2011, 03:24:14 PM »

Very neat.

For what it's worth, I did it the way you originally tried and here it is...

        int factorial, counter;
       
        NSLog(@"Number Factorial");
       
        for (n=1; n<=10; ++n)
        {
            for (factorial=1, counter=1; counter<=n; ++counter)
                factorial *= counter;
           
            NSLog(@"%6i %9i", n, factorial);
        }
Logged
johnyorke
Newbie
*
Posts: 18


Email




« Reply #2 on: January 01, 2012, 11:25:03 AM »

Very nice. Still trying to get my brain back into mathematics having left school quite some time ago now...
Logged
xian
Newbie
*
Posts: 2






« Reply #3 on: March 27, 2012, 09:52:41 PM »

To clarify, this is Exercise 5.3 not 5.2, correct?
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.