Amazon.com Widgets Exercise 6 Chapter 6
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 23, 2013, 02:57:53 AM
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
| | | |-+  Exercise 6 Chapter 6
Pages: [1]   Go Down
Print
Author Topic: Exercise 6 Chapter 6  (Read 1278 times)
alexsom
Jr. Member
**
Posts: 63



Email




« on: January 19, 2012, 04:27:10 PM »

Code: (Objective-C)
Here is Exercise 6 backwards. The right way is to display the words from left to right or better said in the order you input the number. Im still thinking on that one.


[#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
@autoreleasepool
{
    int number, modulus;
    
    NSLog(@"Enter your number");
    scanf("%i",&number);
 
    do {
        modulus=number%10;
        if (modulus==1)
            NSLog(@"one");
        else if (modulus==2)
            NSLog(@"two");
        else if (modulus==3)
            NSLog(@"three");
        else if (modulus==4)
            NSLog(@"four");
        else if (modulus==5)
            NSLog(@"five");
        else if (modulus==6)
            NSLog(@"six");
        else if (modulus==7)
            NSLog(@"seven");
        else if (modulus==8)
            NSLog(@"eight");
        else if (modulus==9)
            NSLog(@"nine");
        else
            NSLog(@"zero");
        number/=10;
    }
        
    while (number!=0);
    
      }
    return 0;

}

Enter your number
3205607
seven
zero
six
five
zero
two
three
]
« Last Edit: January 19, 2012, 04:35:11 PM by alexsom » Logged
alexsom
Jr. Member
**
Posts: 63



Email




« Reply #1 on: January 20, 2012, 08:05:57 AM »

Code: (Objective-C)
[Here is the good one. It takes up to ten digits, more than that it acts weird, anyway:

#import <Foundation/Foundation.h> 

int main (int argc, const char * argv[])   
{   @autoreleasepool
    {
    int number, modulus, digits; 
   
    NSLog(@"Type in a number:"); 
    scanf("%i", &number); 
   
        if(number<0)
        number=-number;
   
for (digits = 1, modulus = number; modulus > 10; modulus /= 10, digits *= 10); 
   
        do {
           
            modulus = ((number/digits)%10);
            if (modulus==1)
                NSLog(@"one");
            else if (modulus==2)
                NSLog(@"two");
            else if (modulus==3)
                NSLog(@"three");
            else if (modulus==4)
                NSLog(@"four");
            else if (modulus==5)
                NSLog(@"five");
            else if (modulus==6)
                NSLog(@"six");
            else if (modulus==7)
                NSLog(@"seven");
            else if (modulus==8)
                NSLog(@"eight");
            else if (modulus==9)
                NSLog(@"nine");
            else

            NSLog(@"zero");
            digits/=10;
            }
         
        while (digits!=0);
           
        return 0;

}

Output Window:

Type in a number:
214057080
two
one
four
zero
five
seven
zero
eight
zero

]
Logged
apexmutt
Newbie
*
Posts: 12


Email




« Reply #2 on: February 12, 2012, 09:31:38 PM »

Wow, my brain hurts trying to figure out how you managed to accomplish this...

I'd be forever greatful if you cared to explain!  Huh
Logged
apexmutt
Newbie
*
Posts: 12


Email




« Reply #3 on: February 13, 2012, 04:24:25 PM »

Wow, I just figured it out... The entire time I was looking at your program, I saw the do-while loop being nested within the for loop! Arg!

That's the first time I've seen a for loop without a block or statement. Koodos to you, sir. You've taught me something quite valuable here.  Grin
Logged
judfio
Newbie
*
Posts: 10






« Reply #4 on: February 20, 2012, 11:54:28 AM »

Here is my version of exercise 6.  I like that the first program listed is so clean and elegant.  I don't think I would have thought of doing the for loop that way!  I learned something new today as well.
The only thing I would change is the indentation of the do/while statement.  The indentation makes it appear to be part of the for loop, which was confusing until I saw the comment posted by apexmutt.

Also, I had trouble with numbers larger than ten digits as well, until I changed my declarations to 'long int'.  Now it accepts numbers larger than ten digits.

//
//  main.m
//  Chapter 6 exercise 6
//
//  Created by Administrator on 2/18/12.
//  Copyright (c) 2012 JF. All rights reserved.
//

#import <Foundation/Foundation.h>


int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
        int number_of_digits=0;           
        long int input_number=0, temp_input_number=0, divisor=1;
       
       
        NSLog(@"Enter your number.");
        scanf(" %li", &input_number);
       
        // if the number is negative, write the '-' sign and set the input to a positive.
       
        if (input_number < 0){
            NSLog(@"-");
            input_number = -input_number;
        }
       
       
        temp_input_number = input_number;       
       
         
        // count the number of digits in the input number
           
        do {                                 
            ++ number_of_digits;
            temp_input_number /= 10;
               
        } while (temp_input_number != 0);
           
           
        // when this while loop is done, we'll have the number we need to divide the input by to get the
        // first digit.
        // for example, if it's a four digit number, we loop through this while statement
        // three times so that the divisor = 1000.  Then we will divide our four digit input by 1000 to get the first
        // digit in the next section.
           
        while (number_of_digits > 1){       
            divisor *= 10;                 
            -- number_of_digits;
        }
   
           
        //  here we divide to get the first digit then reset the temp_input_value to exclude the first digit.
        //  divisor is divided by ten to repeat and isolate the next digit.  If the input number is a single
        //  digit number, we don't do the division - just go through the switch statement to write the number
        //  and the while statement will be satisfied after the first loop because divisor = 1 divided by 10
        //  is zero.
           
        temp_input_number=input_number;
           
        do {
            if (temp_input_number > 9)
                temp_input_number /= divisor;
                   
            switch ( temp_input_number )
            {
                case 9:
                    NSLog(@"Nine");
                    break;
                case 8:
                    NSLog(@"Eight");
                    break;
                case 7:
                    NSLog(@"Seven");
                    break;
                case 6:
                    NSLog(@"Six");
                    break;
                case 5:
                    NSLog(@"five");
                    break;
                case 4:
                    NSLog(@"Four");
                    break;
                case 3:
                    NSLog(@"Three");
                    break;
                case 2:
                    NSLog(@"Two");
                    break;
                case 1:
                    NSLog(@"One");
                    break;
                case 0:
                    NSLog(@"Zero");
                    break;
                           
            }

            temp_input_number = input_number % divisor;
            divisor /= 10;
        } while (divisor > 0);
   
    }
   
    return 0;
}
Logged
steveanthony999
Newbie
*
Posts: 2


Email




« Reply #5 on: February 21, 2012, 11:32:39 PM »

Okay...this was a toughy, and I had to jump around to find a solution...

#import <Foundation/Foundation.h>


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

    @autoreleasepool {
       
        int number, mod;
       
        int count = 0;
       
        NSMutableArray *numOutput = [NSMutableArray new];
       
        NSLog(@"Enter your number: ");
        scanf("%i", &number);
       
        mod = number % 10;
       
        if ( number < 0 )
            number = -number;
       
        do {
           
            switch ( mod ) {
                case 9:
                    [numOutput addObject:@"Nine"];
                    break;
                case 8:
                   [numOutput addObject:@"Eight"];
                    break;
                case 7:
                    [numOutput addObject:@"Seven"];
                    break;
                case 6:
                    [numOutput addObject:@"Six"];
                    break;
                case 5:
                    [numOutput addObject:@"Five"];
                    break;
                case 4:
                    [numOutput addObject:@"Four"];
                    break;
                case 3:
                    [numOutput addObject:@"Three"];
                    break;
                case 2:
                    [numOutput addObject:@"Two"];
                    break;
                case 1:
                    [numOutput addObject:@"One"];
                    break;
                 
                default:
                    [numOutput addObject:@"Zero"];
                    break;
            }
           
            number /= 10;
            mod = number % 10;
            count++;
        } while ( mod != 0 );
       
        for ( int i = count-1; i >= 0; i-- )
            NSLog(@"%@", [numOutput objectAtIndex:i]);
    }
    return 0;
}
Logged
elkie13
Newbie
*
Posts: 17






« Reply #6 on: February 22, 2012, 01:11:47 PM »

Here is my solution with comments, boy is my brain tired!

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

{
    @autoreleasepool {
        int origNumber,n , number_of_digets = 0;
       
        double workingNumber;
        NSLog(@"Enter your Number");
        scanf("%i",&origNumber);
        n = (origNumber > 0)? origNumber : -origNumber;//turns negative into positive number if nessesary
        workingNumber = n; // convert number into double
       
        do {                                    //figure out how many digets number has
            ++ number_of_digets;
           n /=10;
        } while (n !=0);
       
       
       
            for(int x = 1; x < number_of_digets;++x)//divide by 10 until first diget is only one on left of decimal
            {
            workingNumber /= 10;
           
            }
       
        if (origNumber < 0) //write word negative if the number entered was negative (i added this extra Smiley)
            NSLog(@"negative");
       
           
           
           
    for(int y = 1; y <= number_of_digets; ++y)
        {   
        n=workingNumber;//isolate current diget by converting to int
        switch (n) {
                case 9:
                    NSLog(@"nine");
                    break;
                case 8:
                    NSLog(@"eight");
                    break;
                case  7:
                    NSLog(@"seven");
                    break;
                case  6:
                    NSLog(@"six");
                    break;
                case 5:
                    NSLog(@"five");
                    break;
                case 4:
                    NSLog(@"four");
                    break;
                case 3:
                    NSLog(@"three");
                    break;
                case  2:
                    NSLog(@"two");
                    break;
                case 1:
                    NSLog(@"one");
                    break;
                case 0:
                    NSLog(@"zero");
                    break;
                   
                default:
                    break;
            }
               
        workingNumber = (workingNumber - n)*10;//remove current diget move decimal
       
        } 
    }   
       
   
    return 0;
}
Logged
Antoniogra7
Newbie
*
Posts: 6



Email




« Reply #7 on: February 26, 2012, 02:22:35 PM »

Here is my try. It seems to work fine, even with negative numbers. Some expressions are "commented" because they are only for testing purposes. Probably it's not very well explained and variables names may not mean in English what I think they mean  Grin (sorry for that). Anyway:

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

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

    @autoreleasepool {
        
        int number, divider, nD, numberOfDigits, meanwhileNumber, numberOfDigitsMeanwhile, n, digit;
                
        NSLog(@"Introduce your number:");
        scanf("%i", &number);
        
        // Checking if the number is negative and if so, print "minus" and converting the number to positive
        
        if (number < 0) {
            NSLog(@"minus");
            meanwhileNumber = - number;
        } else {
            meanwhileNumber = number;
        }
        
        // Checking the number of digits of the number
        
        nD = meanwhileNumber;
        do {
            ++numberOfDigits;
            nD /= 10;
        } while (nD > 0);
        
        // NSLog(@"Number of digits: %i", numberOfDigits);
        
        // Isolating the digits and printing it in english
        
        for (n = 0; n < numberOfDigits; ++n) {
            
            // Calculating the number of digits our meanwhile number has
            int x = meanwhileNumber;
            numberOfDigitsMeanwhile = 0;
            do {
                ++numberOfDigitsMeanwhile;
                x /= 10;
            } while (x > 0);
            // NSLog(@"Number of digits meanwhile: %i", numberOfDigitsMeanwhile);

            
            // Calculating the divider which will give us the first digit (using integer division particular result: first digit of 123 -> 123 / 100 = 1)
            
            divider = 1;
            
            for (int n = 1; n < numberOfDigitsMeanwhile; ++n) {
                divider *= 10;
            }
            // NSLog(@"Divider: %i", divider);
            // NSLog(@"Number: %i", number);

            
            // Calculating the first digit
            digit = meanwhileNumber / divider;
            // NSLog(@"Digit: %i", digit);
            
            // Eliminating the first digit
            meanwhileNumber = meanwhileNumber - digit * divider;
            // NSLog(@"Meanwhile number: %i", number);

            if (digit == 0) {
                NSLog(@"zero");
            } else if (digit == 1) {
                NSLog(@"one");
            } else if (digit == 2) {
                NSLog(@"two");
            } else if (digit == 3) {
                NSLog(@"three");
            } else if (digit == 4) {
                NSLog(@"four");
            } else if (digit == 5) {
                NSLog(@"five");
            } else if (digit == 6) {
                NSLog(@"six");
            } else if (digit == 7) {
                NSLog(@"seven");
            } else if (digit == 8) {
                NSLog(@"eight");
            } else if (digit == 9) {
                NSLog(@"nine");
            } else {
                NSLog(@"Error");
            }
        }
        
    }
    return 0;
}
Logged
jgross
Newbie
*
Posts: 8






« Reply #8 on: February 28, 2012, 04:23:59 PM »

Here is the good one. It takes up to ten digits, more than that it acts weird, anyway:

I also ran into the ten digit thing and figured it was a problem with the size of the data type.  I changed the data type to long and that solved it.  It made me realize that I'm still slightly unclear on data type sizes.  If I had to take a guess, I would say that the int and float data types are one word in size and that long and double are two words and long long and long double are then...four?  Is this correct?
The other question is how large is a word?  I would have thought it was 8 bytes (264) since the debugger is configured for x86_64, but clearly it's 4 bytes (232) because a 64-bit integer should have been able to handle an 18 digit number (264 = 1.84467441 × 1019).  For the purposes of learning the language and writing simple programs 32 bit words should be plenty large, but is there a way to set the compiler to use 64-bit words?

Jim
Logged
DME
Newbie
*
Posts: 2






« Reply #9 on: April 17, 2012, 02:10:06 PM »

Has anyone tried to run any of the code above? The only one that seems to work is the first one and it print backwards. Try to enter 105 as the number or 015. Check out the code submitted by Jemcik on 04/08/12. The code appears to function correctly. I was just wondering why my code looked so much different than everyone else's.
Logged
ruyjau
Newbie
*
Posts: 1






« Reply #10 on: April 18, 2012, 06:57:18 AM »

Hi!

Tough one, this exercise.

Even after changing the variables to long, I still get garbage with long numbers (more than 10 digits). I hope someone can finally clarify this point.

Here is my code:

Code: (Objective-C)
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        long int number, residue, numberLength;
        residue = 1;  //initialize
        NSLog(@"Please give a number");
        scanf("%i",&number);
        if (number == 0){
            NSLog(@"ZERO!");
        }
        else {
            numberLength = 0;
            long int mynumber = number, myresidue=residue, divisor=1;
            while (mynumber >= 1 ) {
                numberLength++;
                mynumber /= 10;
            }
            NSLog(@"numberLength has %li",numberLength);
            while (numberLength > 1){
                divisor *= 10;
                numberLength--;
            }
            //NSLog(@"numberLength has now %i, number is %i, divisor is %i",numberLength,number,divisor);
            //NSLog(@"residue has %i",residue);
            while (divisor > 0)  {
                residue = number % divisor; //the long residue, divisor has as many digits as number
                myresidue = (number / divisor) % 10; //this has the current digit to call
                //NSLog(@"number is %i, residue is %i",number,numberLength);
                switch (myresidue) {
                    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(@"Wrong entry!");
                        break;
                }
                divisor /= 10;
                number = residue;
            }
        }
    }
    return 0;
}
Logged
DME
Newbie
*
Posts: 2






« Reply #11 on: April 18, 2012, 11:56:48 AM »

ruyfau

Online 8 you have scanf("%i",&number); try scanf("%li",&number); see if this helps. You still have a limit of 18 numbers
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.