Amazon.com Widgets Mathematical trick
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 09:51:12 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
| |-+  Shared Code Library
| | |-+  Mathematical trick
Pages: [1]   Go Down
Print
Author Topic: Mathematical trick  (Read 2263 times)
Ivan P
Newbie
*
Posts: 3


Email




« on: February 03, 2011, 02:49:11 PM »

Hello everyone  Smiley I am Ivan from the Netherlands and just bought Programming in Objective C 2.0

A year ago I made a program on my TI 84 (in basic). Now I'm at chapter six of the book and I have learnt enough to write the little program in objective C, using a custom class.

The program just executes a mathematical trick that always produces 1089. You can enter any integer between 1 and 999, negative too.

This is what the trick does: It inverts the given number. Next the small number is substracted from de large number and the result is inverted too. Finally the first result is added to the second result which gives 1089 as result.

I hope this little program can help someone, since it uses the basic features of objective-C.


Code:

#import <Foundation/Foundation.h>

@interface LittleTrick : NSObject //New class LittleTrick, interface section
{
   int input, inverted1, result1, inverted2, result2; //a couple of variables
   
}

-(void) setInput: (int) number; //sets the value of 'input'
-(void) executeTrick; //does the major part of the 'trick'

@end



@implementation LittleTrick //Implementation section of LittleTrick


-(void) setInput:(int)number
{
   input = number;
}

//The execute trick method

-(void) executeTrick
{
   
   inverted1 = 100 * (input % 10) + 10 * ((input / 10) % 10) + (input / 100) % 10; //Inverts input      
   
   if (input == inverted1)
      printf("Don't use symmetrical 3 digit numbers!!! \n\n"); //Trick doesn't work with symmetrical numbers
                                                //since it produces 0
   
   else {
      
      if (input > inverted1)
         result1 = input - inverted1;
         
      else  result1 = inverted1 - input; //always largest - smallest
      
      inverted2 = 100 * (result1 % 10) + 10 * ((result1 / 10) % 10) + (result1 / 100) % 10; //inverts result1
            
      result2 = result1 + inverted2; //calculates result of the trick (result2)
      
      
      if(input > inverted1) //output must be in the right order too :-)
      printf("%i - %i = %i \n%i + %i = %i \n\n", input, inverted1, result1, result1, inverted2, result2);
      
      else
      printf("%i - %i = %i \n%i + %i = %i \n\n", inverted1, input, result1, result1, inverted2, result2);
   }
}

@end


//main program section

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   float number1 = 1; //has to be not equal to 0, to let while loop start
   int number2;
   double fractpart;
   double intpart;
   
   while (number1 != 0) {
   
   printf("Choose an integer between -999 and -1 or between 1 and 999. Enter 0 to stop.\n");   
   scanf("%f", &number1); //gets input
   
   fractpart = modf(number1, &intpart); //takes the fraction part (intpart isn't used)
   
   if (number1 == 0) {
      printf("Program ended. \n"); //while loop stops when user enters 0
      break;
   }
   
   else if (number1 > 999 || number1 < -999 || fractpart > 0)
      //displays warning if user entered wrong number
      //so, smaller than -999 or larger than 999 or a non-integer
      printf("Use ónly integers between -999 and -1 or 1 and 999 \n\n");
   
   else {
      
      //makes integer from number1 so that % can be used in executeTrick
      number2 = number1;
      
      LittleTrick * trick = [[LittleTrick alloc]init];
      
      [trick setInput:number2];
            
      [trick executeTrick];
      
   };
   }
      
   [pool drain];
    return 0;
}

Example output:

Choose an integer between -999 and -1 or between 1 and 999. Enter 0 to stop.
129
921 - 129 = 792
792 + 297 = 1089


Try it out and see what happens when you enter a number that violates the rules  Cheesy

If you have any questions or suggestions (e.g. for more efficient coding), post them here  Cool

Have fun!!  Grin
« Last Edit: February 12, 2011, 11:36:04 AM by Ivan P » 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.