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



Email




on: January 17, 2012, 11:45:24 AM

Exercise 1 Chapter 5

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
    @autoreleasepool
    {
        int n, squared;
        squared = 0;
      
        NSLog(@"Table for Square Number");
        NSLog(@"n          n^2");
        NSLog(@"_____________");
        
        
        for(n=1;n<=10;++n)
        {
            squared=n*n;
            NSLog(@"%2i---------%i",n,squared);
        }
              
        
        return 0;
    }
    
}

Output window:

Table for Square Number
n       n^2
_____________
1---------1
2---------4
3---------9
4---------16
5---------25
6---------36
7---------49
8---------64
9---------81
10-------100


Exercise 2 Chapter 5;

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
    @autoreleasepool
    {
        int n, triangularNumber;
        
        triangularNumber=0;
        
        NSLog(@"Table for every fifth triangular#");
        NSLog(@"n.........triangular#");
        NSLog(@"------------------");
        
        for (n=5;n<=50;n=n+5)
        {
            triangularNumber=(n*n+n)/2;
            NSLog(@"%2i............%i",n,triangularNumber);
            
        }
        
        return 0;
    }
    
}

Output window:

Table for every fifth triangular#
n.........triangular#
------------------
 5............15
10............55
15............120
20............210
25............325
30............465
35............630
40............820
45............1035
50............1275

Exercise 3 Chapter 5

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
    @autoreleasepool
    {
        int n,factorial;
        
        factorial=1;
        
        NSLog(@"Table for Factorial Number");
        NSLog(@"n.........Factorial#");
        NSLog(@"------------------");
        
        for (n=1;n<=10;++n)
        {
            factorial*=n;
            NSLog(@"%2i............%i",n,factorial);
            
        }
        
        return 0;
    }
    
}

Output Window:

Table for Factorial Number
n.........Factorial#
 ------------------
1............1
2............2
3............6
4............24
5............120
6............720
7............5040
8............40320
9............362880
10..........3628800


Exercise 5 Chapter 5

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
   
    @autoreleasepool
    {
        int n,number,triangularNumber,counter;
       
       
        for(counter=1;counter<=5;++counter)
           {
            NSLog(@"The number is %i",counter);
            NSLog(@"Type in the number");
            scanf("%i", &number);
           
            triangularNumber= 0;
            for(n=1;n<=number;++n)
             
            triangularNumber+=n;
            NSLog(@"Traingular number %i is %i", number,  triangularNumber);
            }
       
        return 0;
    }
   
}

Output Window:

The number is 1
Type in the number
1
Traingular number 1 is 1
The number is 2
Type in the number
2
Traingular number 2 is 3
The number is 3
Type in the number
3
Traingular number 3 is 6
The number is 4
Type in the number
4
Traingular number 4 is 10
The number is 5
Type in the number
5
Traingular number 5 is 15

All the best.  Grin
Last Edit: January 17, 2012, 01:47:11 PM by Randy Logged
sl0o0m
Newbie
*
Posts: 1






Reply #1 on: February 05, 2012, 01:06:58 AM

thanks  for sharing,
I think you have mistake in Exercise 5.

from my interstand, the program should be like this:
( the code I added in red)


int main (int argc, const char * argv[])
{
    @autoreleasepool {
        int n, number, triangularNumber, counter, numberofCalculate;
       
        NSLog(@"how many calculate you want?");
        scanf("%i", &numberofCalculate);

       
        for(counter=1; counter <= numberofCalculate; ++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);
           
        }

    }
    return 0;
}
Logged
alexsom
Jr. Member
**
Posts: 63



Email




Reply #2 on: February 05, 2012, 10:03:00 AM

Hey...you definitely right. I was just looking at mine and noticed that it doesn't really make sense with the requirement of exercise 5. It's more like a botched job of program 5.5. Yep...thank you, alex.
Logged
gbrandon
Newbie
*
Posts: 5






Reply #3 on: February 14, 2012, 12:26:27 PM

I have what is probably a silly question but will ask it anyway.

I have been able to get Exercise 1 to run and return the correct values but the output lists each output as it steps through it as opposed to in the nice table that I've tried to set up.  Obviously I am missing something very simple but can't figure out what it is.

Thanks for the help.
Logged
Waiting
Global Moderator
Full Member
*****
Posts: 113






Reply #4 on: February 14, 2012, 12:32:10 PM

We can't really help you unless you post the code you have written.
Logged
johnnok
Newbie
*
Posts: 3






Reply #5 on: February 14, 2012, 04:59:31 PM

yea I did it like this:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
   
    @autoreleasepool
    {
        int n,triangularNumber,teller;
       
        NSLog(@"Give the number of repetitions");
        scanf("%i", &teller);
        triangularNumber=0;
        n=0;
        do
        {
            ++n;
            triangularNumber+=n;
            NSLog(@"Traingular number %i is %i", n,  triangularNumber);
        }
        while (n<teller);
    }
    return 0;
   
}
Logged
gbrandon
Newbie
*
Posts: 5






Reply #6 on: February 14, 2012, 08:38:45 PM

Waiting, here is my code.  Thank you in advance for any assistance:


//  Program to generate a table of squares

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        int n, squared;
        squared = 0;
       
        NSLog (@"TABLE OF SQUARES");
        NSLog (@" n   n^2");
        NSLog (@"--   ---");
       
        for ( n = 1; n <= 10; ++n )  {
            squared = n*n;
            NSLog (@" %3i   %i", n, squared);
        }
    }
    return 0;
}
Logged
Waiting
Global Moderator
Full Member
*****
Posts: 113






Reply #7 on: February 15, 2012, 04:21:50 AM

This should do what you are after.

Code: (Objective-C)
//  Program to generate a table of squares

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    
    @autoreleasepool {
        int n, squared;
        squared = 0;
        
        NSLog (@"TABLE OF SQUARES");
        NSLog (@"  n       n^2");
        NSLog (@"________________");
        
        for ( n = 1; n <= 10; ++n )  {
            squared = n*n;
            NSLog (@" %2i.........%i", n, squared);
        }
    }
    return 0;
}
Logged
gbrandon
Newbie
*
Posts: 5






Reply #8 on: February 15, 2012, 08:50:20 AM

It was actually just a matter of formatting my output window.  Thanks for the help.
Logged
ahrydberg
Newbie
*
Posts: 9






Reply #9 on: February 26, 2012, 09:07:13 AM

How do you " format the output window"?
Logged
hanwei
Newbie
*
Posts: 2


Email




Reply #10 on: January 02, 2013, 08:26:21 PM

thanks all
in Exercise 5 i did it like this.

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

    @autoreleasepool {
        int counter,number,result;
        scanf("%i %i",&counter,&number);
        for (int i = 1; i <= counter; i++) {
            result = 0;
            for (int j = 0; j <= number; j++) {
                result += j;
                NSLog(@"triangular number %i is %i",number,result);
            }
        }
    }
    return 0;
}
so you can input like: 2 10
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.