Amazon.com Widgets Problem with Ex 5
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 21, 2014, 01:07:48 PM
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
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 5
| | | |-+ Problem with Ex 5
Pages: [1] Go Down
Print
Author Topic: Problem with Ex 5 (Read 778 times)
estschudi
Newbie
*
Posts: 15


Email




on: December 18, 2011, 06:58:33 AM

Chapter 5 exercise 5 asks to alter program 5.5 so that the user can enter the amount of triangular numbers to be
calculated and displayed.  I think I have that part right, but now when the program gets to the loop to calculate the
triangular number,  it's calculating  the number + 1 instead.  So I enter integer 4 and it calculates and displays integer 5.
I have looked over it over and over and can't see why.  Any pointers appreciated.  Thanks.
Code: (Objective-C)
#import <Foundation/Foundation.h>

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

{

    @autoreleasepool {
        int n, number, triangularNumber, counter, numberToCalculate;
       
        NSLog(@"How many triangular numbers would you like to calculate?");
        scanf("%i", &numberToCalculate);
       
        for (counter = 1; counter <= numberToCalculate; ++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.", n, triangularNumber);
           
        }
       
    }
    return 0;
}



2011-12-18 05:56:25.363 Ch5Ex5[3748:707] How many triangular numbers would you like to calculate?
 3
2011-12-18 05:56:31.133 Ch5Ex5[3748:707] What triangular number do you want?
18
2011-12-18 05:56:34.700 Ch5Ex5[3748:707] Triangular number 19 is 171.
2011-12-18 05:56:34.701 Ch5Ex5[3748:707] What triangular number do you want?
4
2011-12-18 05:56:37.660 Ch5Ex5[3748:707] Triangular number 5 is 10.
2011-12-18 05:56:37.662 Ch5Ex5[3748:707] What triangular number do you want?
7
2011-12-18 05:56:42.195 Ch5Ex5[3748:707] Triangular number 8 is 28.
Logged
mitchb
Full Member
***
Posts: 144






Reply #1 on: December 18, 2011, 06:22:00 PM

In the for loop you have:  n <= number; the for loop will not stop until n is greater than the entered number. Replace the n in your NSLog statement with number.

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

Mitch
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
estschudi
Newbie
*
Posts: 15


Email




Reply #2 on: December 18, 2011, 06:50:32 PM

Thank you---I missed that.

Eric
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.