Amazon.com Widgets exercise 5.1~5.8 except 5.7
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2013, 06:42: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
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 5
| | | |-+  exercise 5.1~5.8 except 5.7
Pages: [1]   Go Down
Print
Author Topic: exercise 5.1~5.8 except 5.7  (Read 433 times)
NaCl1119
Newbie
*
Posts: 5


Email




« on: March 30, 2011, 12:22:28 AM »

///////////////exercise 1//////////////////

#import <Foundation/Foundation.h>

int main(int argc, char *argc[])
{
   NSAutoreleasePool *pool[[NSAutoreleasePool alloc] init];
   
   int n=1;
   NSLog(@" n   n^2\n==============");
   for(;n>=1&&n<=10; n++)
      NSLog(@"%2i   %i", n, n*n);

   [pool drain];
   return 0;
}

/////////////exercise 2//////////////
#import <Foundation/Foundation.h>

int main(int argc, char *argc[])
{
   NSAutoreleasePool *pool[[NSAutoreleasePool alloc] init];

//   int triangularNumber=0;   

   for(int n=5, m=1;n<=50;n*m, m++)
   {
      int triangularNumber=0;   //Must clear triangularNumber each loop
      for(l=1;l<=n;l++)
      {
         triangularNumber = l*(l+1)/2;
      }
      NSlog(@"The triangularNumber value is %i" triangularNumber);
   }
   [pool drain];
   return 0;
}

///////////////////////exercise 3////////////////

#import <Foundation/Foundation.h>

int main(int argc, char *argc[])
{
   NSAutoreleasePool *pool[[NSAutoreleasePool alloc] init];
   int factorial=1;
   for(; factorial<=10; factorial++)
   {
      int result=1; //To memorize each number, m
      for(; factorial>=1; factorial--)
      {
         result*=factorial;
      }
      NSLog(@"%2i! = %i", factorial, result);
   }
      
   [pool drain];
   return 0;
}



//////////////////exercise 5/////////////////////

#import <Foundation/Foundation.h>

int main(int argc, char *argc[])
{
   NSAutoreleasePool *pool[[NSAutoreleasePool alloc] init];
   
   int n, number, trianglarNumber, numcount, counter;
   
   NSLog(@"How many check the number do you want?");
   scanf("%i", numcount);   

   for( counter =1; counter <=numcount; ++counter)
   {
      NSLog(@"#%i What triangular number do you want?", counter);
      scanf("%i", &number);
      
      triangularNumber = 0;

      for(n=1; n<=number; ++n)
         triangularNumber +=n;

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

   [pool drain];
   return 0;
}

//////////////////////////exercise 6
//5.2
#import <Foundation/Foundation.h>

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

   [pool drain];
   return 0;
}


//5.3
#import <Foundation/Foundation.h>

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

   [pool drain];
   return 0;
}

//5.4
#import <Foundation/Foundation.h>

int main(int argc, char *argc[])
{   
   NSAutoreleasePool *pool[[NSAutoreleasePool alloc] init];
   int n, triangularNumber;
   
   NSLog("TABLE OF TRIANGULAR NUMBERS");
   NSLog("n Sum from 1 to n");
   NSLog("-- ----------");


   triangularNumber = 0;
   
   /*
   for(n=1; n<=10; ++n)
   {
      triangularNumber +=n;
      NSLog(@"%i      %i", n, triangularNumber);
   }*/

   while(n<=10)
   {
      triangularNumber +=n;
      NSLog(@"%i      %i", n, triangularNumber);
      ++n;
   }
      
   [pool drain];
   return 0;
}




////////////////////////exercise 8

#import <Foundation/Foundation.h>

int main(int argc, char *argc[])
{
   NSAutoreleasePool *pool[[NSAutoreleasePool alloc] init];
   int number, sum=0;

   NSLog (@"Enter your number. ");
   
   while(number!=0)
   {
      sum+=number%10;
      number/=10;   
   }
   NSLog(@"The sum is %i", sum);

   [pool drain];
   return 0;
}




please give me a favor

I don't have a chance to use Xcode in Mac
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.