Amazon.com Widgets Exercise 6.6
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 28, 2014, 11:27:20 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
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 6
| | | |-+ Exercise 6.6
Pages: 1 ... 4 5 [6] Go Down
Print
Author Topic: Exercise 6.6 (Read 19562 times)
mr_white76
Newbie
*
Posts: 3






Reply #75 on: January 07, 2011, 06:19:44 AM

Here's my solution, I'm quite happy with it - even if for sake of simplicity I've used the standard (pow(x,y)) c function Smiley

Code: (Objective-C)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int number, dummy, lenght, cypher, i;

NSLog(@"Insert your integer number, please.");
scanf("%i", &number);

dummy = number;
lenght = 0;

// Calculates the number of cyphers to be shown

do {
++lenght;
dummy /= 10;
} while (dummy !=0);

// Handle negatives

if (number < 0) {
NSLog(@"Minus");
number = -number;
}

// Cyphers printing

for (i = lenght - 1; i >= 0; --i){

cypher = (int) (number / (pow(10, i)));
number = number - (cypher * (pow(10, i)));

switch (cypher) {
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(@"Invalid character");
break;
}
}

    [pool drain];
    return 0;
}
Logged
nielsk
Newbie
*
Posts: 1






Reply #76 on: March 19, 2011, 03:23:00 AM

My solution to the problem. It seems to work w/out any problems. Any comments appreciated.
Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
   
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
    NSString *myString;
    NSInteger objectNumber;
    int i = 0;
   
    NSLog(@"Type your Number.");
    scanf("%i",&objectNumber);
     
    myString = [NSString stringWithFormat:@"%d", objectNumber];
 
    for(i=0; i < [myString length]; i++) {
        if ([myString characterAtIndex:i] == '0')
            NSLog(@"Zero");
        else if ([myString characterAtIndex:i] == '1')
            NSLog(@"One");
        else if ([myString characterAtIndex:i] == '2')
            NSLog(@"Two");
        else if ([myString characterAtIndex:i] == '3')
            NSLog(@"Three");
        else if ([myString characterAtIndex:i] == '4')
            NSLog(@"Four");
        else if ([myString characterAtIndex:i] == '5')
            NSLog(@"Five");
        else if ([myString characterAtIndex:i] == '6')
            NSLog(@"Six");
        else if ([myString characterAtIndex:i] == '7')
            NSLog(@"Seven");
        else if ([myString characterAtIndex:i] == '8')
            NSLog(@"Eight");
        else if ([myString characterAtIndex:i] == '9')
            NSLog(@"Nine");
        else if ([myString characterAtIndex:i] == '-')
            NSLog(@"Minus");
        else NSLog(@"Not recognized");
    }

    [myString release];
    [pool drain];
    return 0;
}
Logged
jcd
Newbie
*
Posts: 5






Reply #77 on: April 01, 2011, 09:49:11 AM

My trial.

I also add the minus sign, in front of the number in case it's negative. My flowchart was:

1. Ask if it's negative. If it's negative print 'minus'.
2. Invert the number and count digits.
3. With the inverted number, start printing the last digit and count back digits.
4. If counter not zero, print as may zeros as needed.

#import <Foundation/Foundation.h>

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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   int number, invertedNumber, workingNumber, lastDigit;
   int counter=0;
   
       
    NSLog(@"Please insert an integer:");
    scanf("%i", &number);
    if(number<0) NSLog(@"minus");
    if(number<0) number *= -1;
   
    workingNumber=number;
    invertedNumber=0;
           
    do
        {
            lastDigit=workingNumber % 10;
            counter ++;
            workingNumber=(workingNumber-lastDigit)/10;
            invertedNumber=invertedNumber*10+lastDigit;
       
        }
    while (workingNumber>0);       
       
    workingNumber=invertedNumber;
       
    do
        {
            lastDigit=workingNumber % 10;
            counter --;
       
            switch (lastDigit)
                {       
                    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:
                        break;
                }       
       
            workingNumber=(workingNumber-lastDigit)/10;
            invertedNumber=invertedNumber*10+lastDigit;
       
        }
    while (workingNumber>0);       
 
    while (counter != 0)
    {
        NSLog(@"Zero");
        counter --;
    }

    [pool drain];
    return 0;
}
Logged
Alistair
Newbie
*
Posts: 18






Reply #78 on: April 18, 2011, 08:56:23 AM

My solution, after MUCH thinking and tinkering.  A purely maths based approach without converting to a string, which obviously means it has an upper limit on the numbers it can process.

Code: (Objective-C)

//
//  main.m
//  Ch6Ex6Part2
//
//  Created by Alistair Hutchinson on 18/04/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
    long long int number, counter = 0, tempNumber, tempNumberTwo = 0, n, tempNumberThree = 0;
    NSLog (@"Please enter your integer");
    scanf ("%lld", &number);
   
    //Check to see if the user entered zero
    if (number == 0)
        NSLog (@"Zero");
    else
    {
        //Figure out how many digits long the number is
        tempNumber = number;
        while (tempNumber > 0)
        {
            tempNumber = tempNumber / 10;
            ++counter;
        }
        //Loop to extract each digit
        while (counter > 0 )
        {
            tempNumber = number;
            //Loop to get to the current needed decimal place
            for (n = counter -1; n >0 ; --n)
            {
                tempNumber /=  10;
            }
           
            //Store the current integer (we'll need it later)
            tempNumberThree = tempNumber;
           
            //Strip off any digits to the left of the digit we need
            tempNumber -= tempNumberTwo;
           
            //Find out which digit we have and print the corresponding word               
                switch (tempNumber)
            {
                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;
            }
            //Multiply the integer we found earlier by ten to enable stripping off the leftmost digits
            tempNumberTwo = tempNumberThree * 10;
            --counter;
       
           
        }
    }
   
   

    [pool drain];
    return 0;
}
Logged
Overcoming
Newbie
*
Posts: 7






Reply #79 on: July 11, 2011, 09:34:32 PM

Great back and forth discussions and troubleshooting on this post. I know this is an old post but if any of you see this message please checkout the solution I submitted for this problem yesterday and provide some feedback/thoughts. Really would appreciate it.
Thank You
Logged
ironcrush
Newbie
*
Posts: 1






Reply #80 on: July 12, 2014, 08:36:21 AM

My attempt in C.
I am actually following the "Programming in C" a wonderful book for C programming by Stephen Kochan.  But unfortunately i haven't found a support site like this of this book. This site is a great help.

Any feedback would be highly appreciated. Thanks.

Code: (Objective-C)
/* A program that displays integer digits in English */

#include <stdio.h>

int main(void)
{
    int number, i, DigitToPrint;
   
    printf("Enter a number: ");
    scanf("%i", &number);
   
    if(number < 0)
    {
        number = -number;
        printf("Negetive ");
    }
    if( number ==  0)
    {
        printf("zero ");
    }
    else
    {
        /*
        *Get the place value of the number in base 10
        *i.e if number is 12, then its place value is 10 since 10^1 = 10
        */
        for(i = 1; number > (i * 10); i *= 10);
       
        while(i > 0)
        {
            /*We have the place value in 'i'
            *to get the left-most digit devide the number by its place value 'i'.
            *The modulo 10 of the result will return the digit.
            */
            DigitToPrint = (number / i) % 10;
            switch (DigitToPrint)
            {
                case 0: printf("zero "); break;
                case 1: printf("one "); break;
                case 2: printf("two "); break;
                case 3: printf("three "); break;
                case 4: printf("four "); break;
                case 5: printf("five "); break;
                case 6: printf("six "); break;
                case 7: printf("seven "); break;
                case 8: printf("eight "); break;
                case 9: printf("nine "); break;
            }
            //Set the place value to the next left-most digit
            i /= 10;
        }
    }

    printf("\n");

    return 0;
}
Logged
Pages: 1 ... 4 5 [6] 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.