Amazon.com Widgets Chapter 6 exercise 6 **DIFF METHOD
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 05:28:57 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 6
| | | |-+  Chapter 6 exercise 6 **DIFF METHOD
Pages: [1]   Go Down
Print
Author Topic: Chapter 6 exercise 6 **DIFF METHOD  (Read 271 times)
hmsheena
Newbie
*
Posts: 1






« on: June 01, 2012, 08:42:39 AM »

Hey everyone,
Exercise 6 stumped me for a long time, as I was getting the digits to write out, but it would be in reverse. I saw that everyone did some type of complicated fix for that, but I figured out an alternative (and in my opinion) easier way to fix the program. Instead of dealing with the numbers after, I wrote a program that reversed the input number and then printed that. Check it out:

Code: (Objective-C)
//
//  main.m
//  Chap6E6
//
//  Created by Halim Sheena on 5/31/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        int number, mod, digits, revNumber, numdig, tranNum, expon, var, var2;
        
        
        NSLog (@"Enter in a number to have it displayed.");
        scanf ("%i", &number);

    
        // End of prompt. Now for the code stuff
        
        if ( number == 0 ) {    //Displays "Zero" if it is 0.
            
            NSLog (@"Zero");
        }
    
        // For any number that is NOT zero....
        
        else {
        
        // This is an attempt to reverse the numbers
        
        
            // begin digit count
            numdig = number;
        
            while ( numdig != 0 ) {
                digits += 1;
                numdig /= 10; }
            // End of digit count
        
        
        // begin reversal
        tranNum = number;
        
        
        while (digits != 0 ) {
            expon = pow (10, digits); // I used google to get this, its a C function for exponent, becomes expon = 10 ^(digits)
            var = tranNum % 10;
            var2 = var * expon;
            revNumber += var2;
            tranNum /= 10;
            digits -= 1; } //reverses numbers but adds 0 at end
        
        
        revNumber /= 10; //Fix: removes extra zero from end
        
    
        
        //end reversal
        
        
        
        // This code writes out the numbers
        if (revNumber<0) {
             NSLog (@"Negative");
            revNumber= -revNumber; }  
        
        do {
            mod = revNumber % 10;
            switch (mod) {
        case 1:
            NSLog (@"One");
            break;
        case 2:
            NSLog (@"Two");
            break;
        case 3:
            NSLog (@"Three");
            break;
        case 4:
            NSLog (@"Four");
            break;
        case 5:
            NSLog (@"Five");
                    break;
                case 6:
                    NSLog (@"Six");
                    break;
                case 7:
                    NSLog (@"Seven");
                    break;
                case 8:
                    NSLog (@"Eight");
                    break;
                case 9:
                    NSLog (@"Nine");
                    break;
                case 0:
                    NSLog (@"Zero");
                    break;
                default:
                    NSLog (@"Not a valid digit");
                    break;
                    
            
            } revNumber /= 10;
        } while (revNumber != 0);
        
      
        // The following is a fix, due to the reverse digit function if you had a number end with 0, the zero was forgotten  (because 0 * 10^(digits) will be zero, so it would not work well with the sequence). This sequence adds a 0 at the end for every 0 at the end of the original number.
        
       while ( number % 10 == 0 ) {
           NSLog (@"Zero");
           number /= 10;  }
        
            
            
            
            

        
        
        
        
        
        }
        
        
        
        
    }
    return 0;
}
« Last Edit: June 01, 2012, 01:25:30 PM by happyzhb » Logged
clusterflux
Newbie
*
Posts: 1


Email




« Reply #1 on: June 02, 2012, 04:05:57 PM »

I did basically the same, then came on here to see how other people managed it. I did originally start out using the pow() function to get the reversal working and then I found out there's a far simpler way to use arithmetic to reverse the number (see line 14):

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

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

int number, flip_number = 0, right_digit;

NSLog(@"Enter your number.");
scanf("%i", &number);

while( number != 0 ) { //flip the number
right_digit = number % 10;
number /= 10;
flip_number = flip_number * 10 + right_digit;

}

while( flip_number !=0 ) { //now pull off the right side digits from the flipped number to get left side digits from the original number

right_digit = flip_number % 10;

switch ( right_digit )
{
case 0:
NSLog(@"zero");
break;
case 1:
NSLog(@"one");
break;
case 2:
NSLog(@"two");
break;
case 3:
NSLog(@"three");
break;
case 4:
NSLog(@"four");
break;
case 5:
NSLog(@"five");
break;
case 6:
NSLog(@"six");
break;
case 7:
NSLog(@"seven");
break;
case 8:
NSLog(@"eight");
break;
case 9:
NSLog(@"nine");
break;
default:
NSLog(@"None of those were a number");
}

flip_number /= 10;

}

    [pool drain];
    return 0;
}
« Last Edit: June 02, 2012, 04:07:47 PM by clusterflux » 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.