Amazon.com Widgets Exercise 6 Chapter 5
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 01, 2014, 06:51:39 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
| | | |-+ Exercise 6 Chapter 5
Pages: [1] Go Down
Print
Author Topic: Exercise 6 Chapter 5 (Read 988 times)
alexsom
Jr. Member
**
Posts: 63



Email




on: January 17, 2012, 04:02:23 PM

Exercise 6 Chapter 5

Please let me know if you find anything wrong.

5.2 with While statement:

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
       
        int n, triangularNumber;
       
        triangularNumber =0;
       
        n=1;   
        while(n<=200)
        {
           
          triangularNumber+=n;
          n++;
        }
          NSLog(@"The 200th triangular number is %i",triangularNumber);
       
       
        return 0;
    }
   
   
}

Output Window:

Triangular[10237:707] The 200th triangular number is 20100

5.3 with while statement:

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
       
        int n, triangularNumber;
        triangularNumber=0;
       
       
       
        NSLog (@"TABLE OF TRIANGULAR NUMBERS");
        NSLog (@" n Sum from 1 to n");
        NSLog (@"-- --------");
       
       
        n=1;
        while(n<=10)
           
        {     
            triangularNumber +=n;       
            NSLog(@"%2i        %i",n, triangularNumber);
            ++n;   
        }
          return 0;
    }
   
}
   
Output Window:

TABLE OF TRIANGULAR NUMBERS
n Sum from 1 to n
----------
1        1
2        3
3        6
4        10
5        15
6        21
7        28
8        36
9        45
10      55

5.4 with While statement:

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
   
    @autoreleasepool
    {
        int n,number,triangularNumber;
       
       
       
        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);
       
       
        return 0;
       }
   
}

Output Window:
What triangular number do you want?
50
Triangular number 50 is 1275


5.5 for While statement:

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
   
    @autoreleasepool
    {
        int n,number,triangularNumber,counter;
       
       
           counter=1;
           while (counter<=5)
        {
            ++counter;
            NSLog(@"What triangular number do you want?");
            scanf("%i", &number);
           
            triangularNumber= 0;
            n=1;
            while (n<=number)
            {
                triangularNumber+=n;
                ++n;
            }
               
               
            NSLog(@"Triangular number for %i is %i.", number,  triangularNumber);
        }
        return 0;
       
    }
   
}

Output window:

What triangular number do you want?
12
Triangular number for 12 is 78.
What triangular number do you want?
25
Triangular number for 25 is 325.
What triangular number do you want?
50
Triangular number for 50 is 1275.
What triangular number do you want?
75
Triangular number for 75 is 2850.
What triangular number do you want?
83
Triangular number for 83 is 3486.

Best to you.  Wink
Logged
clouded
Full Member
***
Posts: 123






Reply #1 on: May 10, 2012, 01:35:55 PM

Here's what I have w/o the outputs (too many):

Code: (Objective-C)
// Chapter 5 Exercise 6
// Programs 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.

/* Program 5.2
// Program to calculate the 200th triangular number
// Introduction of the for statement

#import <Foundation/Foundation.h>

int main (int argc, char * argv[]) {
    @autoreleasepool {
        int n = 1, triangularNumber;
            
        triangularNumber = 0;
        while (n <= 200) { //for ( n = 1; n <= 200; n = n + 1 )
            triangularNumber += n;
            ++n;
        }
        NSLog (@"The 200th triangular number is %i", triangularNumber);
    }
    return 0;
}*/

/* Program 5.3
// Program to generate a table of triangular numbers

#import <Foundation/Foundation.h>

int main (int argc, char * argv[]) {
    
    @autoreleasepool {
        int n = 1, triangularNumber;
        
        NSLog (@"TABLE OF TRIANGULAR NUMBERS");
        NSLog (@" n Sum from 1 to n");
        NSLog (@"-- ---------------");
        
        triangularNumber = 0;
        while (n <= 10) { //for ( n = 1; n <= 10; ++n ) {
            triangularNumber += n;
            NSLog (@" %i          %i", n, triangularNumber);
            ++n;
        }
    }
    return 0;
}*/

/* Program 5.4

#import <Foundation/Foundation.h>

int main (int argc, char * argv[]) {
    
    @autoreleasepool {
        int n = 1, number, triangularNumber;
        
        NSLog (@"What triangular number do you want?");
        scanf ("%i", &number);
        
        triangularNumber = 0;
        while (n <= number) { //for ( n = 1; n <= number; ++n )
            triangularNumber += n;
            ++n;
        }
        NSLog (@"Triangular number %i is %i\n", number, triangularNumber);
    }
    return 0;
}*/

/* Program 5.5

#import <Foundation/Foundation.h>

int main (int argc, char * argv[])
{
    @autoreleasepool {
        int n, number, triangularNumber, counter = 1;
        while (counter <= 5) { //for ( counter = 1; counter <= 5; ++counter ) {
            NSLog (@"What triangular number do you want?");
            scanf ("%i", &number);
            triangularNumber = 0;
            n = 1;
            while (n <= number) { // for ( n = 1; n <= number; ++n )
                triangularNumber += n;
                ++n;
            }
            NSLog (@"Triangular number %i is %i", number, triangularNumber);
            ++counter;
        }
    }
    return 0;
}*/
Last Edit: May 11, 2012, 05:28:37 PM by clouded Logged
stuartjj
Newbie
*
Posts: 11






Reply #2 on: June 03, 2012, 10:21:29 AM

I have something different than you guys for 5.2.  I'm wondering if my answer, though also correct, is, from a stylistic prospective, less desirable, i.e., is my version more confusing for another programmer or myself to figure out later?  If it is, why?

Thanks!


Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, char * argv[])
{
    @autoreleasepool
    {
        int n, triangularNumber;
       
        triangularNumber = 0;
        n=0;
       
        while (n<200)
        {
            n=n+1;
            triangularNumber+=n;
           
        }
        NSLog (@"The 200th triangular number is %i", triangularNumber);
    }
   
    return 0;
}
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






Reply #3 on: July 23, 2012, 08:35:28 AM

here's mine:

Program 5.2

Code: (Objective-C)
int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        int n = 1, triangularNumber = 0;
       
        while (n <= 200) {
            triangularNumber += n;
            ++n;
        }
        NSLog(@"The 200th triangular number is %i", triangularNumber);
       
    }   
    return 0;
}

output:

2012-07-23 16:00:35.951 prog5[3478:403] The 200th triangular number is 20100

Program 5.3

Code: (Objective-C)
int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        int n = 1, triangularNumber = 0;
       
        NSLog(@"TABLE OF TRIANGULAR NUMBERS");
        NSLog(@" n Sum from 1 to n");
        NSLog(@"-- ---------------");
       
        while (n <= 10) {
            triangularNumber += n;
            NSLog(@" %i           %i", n, triangularNumber);
            ++n;
        }
       
    }   
    return 0;
}

output:


2012-07-23 16:10:08.963 prog5[3509:403] TABLE OF TRIANGULAR NUMBERS
2012-07-23 16:10:08.965 prog5[3509:403]  n Sum from 1 to n
2012-07-23 16:10:08.966 prog5[3509:403] -- ---------------
2012-07-23 16:10:08.966 prog5[3509:403]  1           1
2012-07-23 16:10:08.967 prog5[3509:403]  2           3
2012-07-23 16:10:08.967 prog5[3509:403]  3           6
2012-07-23 16:10:08.968 prog5[3509:403]  4           10
2012-07-23 16:10:08.968 prog5[3509:403]  5           15
2012-07-23 16:10:08.969 prog5[3509:403]  6           21
2012-07-23 16:10:08.969 prog5[3509:403]  7           28
2012-07-23 16:10:08.969 prog5[3509:403]  8           36
2012-07-23 16:10:08.970 prog5[3509:403]  9           45
2012-07-23 16:10:08.970 prog5[3509:403]  10           55

Program 5.4

Code: (Objective-C)
int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        int n = 1, triangularNumber = 0, number;
       
        NSLog(@"What triangular number do you want?");
        scanf("%i", &number);
       
        while (n <= number) {
            triangularNumber += n;
            ++n;
        }
        NSLog(@"Triangular number %i is %i\n", number, triangularNumber);
       
    }   
    return 0;
}

output:

2012-07-23 16:19:29.114 prog5[3523:403] What triangular number do you want?
100
2012-07-23 16:19:31.097 prog5[3523:403] Triangular number 100 is 5050

Program 5.5

Code: (Objective-C)
int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        int n = 1, triangularNumber = 0, counter = 1, number;
       
        while (counter <= 5) {
            NSLog(@"What triangular number do you want?");
            scanf("%i", &number);
            ++counter;
           
            while (n <= number) {
                triangularNumber += n;
                ++n;
            }
            NSLog(@"Triangular number %i is %i\n", number, triangularNumber);
        }
    }   
    return 0;
}

output:

2012-07-23 16:26:19.626 prog5[3533:403] What triangular number do you want?
1
2012-07-23 16:26:23.667 prog5[3533:403] Triangular number 1 is 1
2012-07-23 16:26:23.668 prog5[3533:403] What triangular number do you want?
2
2012-07-23 16:26:27.307 prog5[3533:403] Triangular number 2 is 3
2012-07-23 16:26:27.308 prog5[3533:403] What triangular number do you want?
3
2012-07-23 16:26:30.307 prog5[3533:403] Triangular number 3 is 6
2012-07-23 16:26:30.308 prog5[3533:403] What triangular number do you want?
4
2012-07-23 16:26:34.772 prog5[3533:403] Triangular number 4 is 10
2012-07-23 16:26:34.773 prog5[3533:403] What triangular number do you want?
5
2012-07-23 16:26:36.180 prog5[3533:403] Triangular number 5 is 15
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.