Amazon.com Widgets Exercise 5.2
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 07:38:24 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
Pages: [1]   Go Down
Print
Author Topic: Exercise 5.2  (Read 608 times)
Alistair
Newbie
*
Posts: 18






« on: April 07, 2011, 06:07:35 PM »

How did I do?

Code: (Objective-C)


#import <Foundation/Foundation.h>

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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int n, triangularNumber, rem;
   
    triangularNumber = 0;
   
    NSLog (@"TABLE OF TRIANGULAR NUMBERS");
    NSLog (@"N  SUM FROM 1 TO N");
    NSLog (@"------------------");
   
    for ( n = 1; n <= 50; ++n)
    {
        rem = n % 5;
        while ( rem == 0)
        {
            triangularNumber = n * (n +1) / 2;
            NSLog (@"%2i       %i", n, triangularNumber);
            rem = 1;
        }
    }
    [pool drain];
    return 0;
}
Logged
tuhinbhatt
Newbie
*
Posts: 3


Email




« Reply #1 on: May 03, 2011, 08:46:30 AM »

This might Me Be Silly Question----

But in the solution

 
            triangularNumber = n * (n +1) / 2; 
            NSLog (@"%2i       %i", n, triangularNumber); 
            rem = 1; 

Why rem=1 statement has to be used (I know if we dont use it results in infinite loop)

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







« Reply #2 on: May 03, 2011, 11:38:26 AM »

You don't know as of Chapter 5 about "if" statements, but that's what should be used instead of the while.  Better yet, why not have the for loop count  by 5 instead of by 1?

Cheers,

Steve
Logged
badBandit
Newbie
*
Posts: 5






« Reply #3 on: May 13, 2011, 11:37:02 AM »

You don't know as of Chapter 5 about "if" statements, but that's what should be used instead of the while.  Better yet, why not have the for loop count  by 5 instead of by 1?

Cheers,

Steve

Hi Steve!

That's exactly what I was trying to do with this:
Code: (Objective-C)
for (n = 5; n <= 50; n + 5) {

But that gave me a warning and an infinite loop where the n + 5 wasn't being used. So my final code went like this:
Code: (Objective-C)
// Program to calculate a table of every 5th triangular numbers

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
   
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int n, triangularNumber;
   
    NSLog(@"TABLE OF TRIANGULAR NUMBERS");
    NSLog(@"      n n (n + 1) / 2");
    NSLog(@"     -- -------------");
   
    triangularNumber = 0;
   
    for (n = 5; n <= 50; n = (n + 5)) {
        triangularNumber = n * (n + 1) / 2;
        NSLog(@"     %2i          %i", n, triangularNumber);
       
    }
   
    [pool drain];
    return 0;
}

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.