Amazon.com Widgets Exercise 6.6 - Can someone please help me
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 01, 2014, 01:54:40 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
| |-+ Chapter Study
| | |-+ Chapter 6 - Making Decisions
| | | |-+ Exercise 6.6 - Can someone please help me
Pages: [1] Go Down
Print
Author Topic: Exercise 6.6 - Can someone please help me (Read 1521 times)
CloudDweller
Newbie
*
Posts: 17






on: February 13, 2012, 03:00:33 AM

I've spent a couple of hours on this one and I still can't get my head around it so I was hoping that someone could please help me.

My code takes the number the user inputs and converts this to text as per the example below:

Input
12345

Output
Five
Four
Three
Two
One

The problem is that I need to print the numbers in their original order and I dont know how to achieve this.

I know that to get the last digit I divide 12345 by 10 (using the % operator) which gives me 1234.5.  The problem is that I need to store the number 5 somewhere and then do the same again with 1234 and append 4 to the first number 5 and so on until I have the 54321. Then I can simply run my code to give me the numbers in text form in the correct order.

Can someone, anyone please help explain how I can achieve this as Im banging my head against a brick wall and its starting to really hurt  Cry
Logged
CloudDweller
Newbie
*
Posts: 17






Reply #1 on: February 13, 2012, 10:11:21 AM

Okay I've been thinking long and hard about this while I've been at work and I think that another way to approach it is to count the number of digits in the number and use that count in a program.

I'm going to test this when I get home but here's my thinking (and yes I know its not code) not yet anyhow  Wink

Int number                        //user enters a number (12345)
Int counter = 10                  //used to count the number of digits in the number
Int tempNumber                    //used to temporarily store a value

tempNumber = number               //Create a copy of number called tempNumber

while ( tempNumber > 0 )          //build a loop that counts the number of digits in the number
Divide tempNumber by 10 using %   //12345 % 10 = 1234.5 (% = 5)
Multiply counter by 10            //10 * 10 = 100
                                  //Loop until tempNumber = 0

                                  //at the end of the loop the values are as follows:
tempNumber = 0                    //tempNumber = 0 because it was divided 5 times
counter = 100,000                 //10*10*10*10*10 because tempNumber was divided 5 times

while ( counter > 0 )             //build a loop that outputs each digit as text
tempNumber = number               //reset tempNumber to number each time the loop repeats (12345)
tempNumber % counter              //divide tempNumber by counter (12345 % 100,000 = 0.12345 / modulus = 1)
NSLog(@%i, Number % counter)    //display 1 in the console as text
counter /= 10                     //divide counter by 10 and assign the value to counter (100,000 / 10 = 10,000)
                                  //Loop until counter = 0


I know that I haven't included the conversion from number to text, but that part I don't have an issue with.  Its the bit above that been giving me a headache.

I'll let you know how I get on when I've tested it out.
Last Edit: February 13, 2012, 10:31:55 AM by CloudDweller Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #2 on: February 13, 2012, 10:17:26 AM

Keep plugging away at it.  These exercises are designed to teach you how to develop an algorithm to solve a problem.  That's a key component of becoming a good programmer.

Cheers,

Steve
Logged
CloudDweller
Newbie
*
Posts: 17






Reply #3 on: February 13, 2012, 03:47:05 PM

Okay its taken me all evening to get this working but here's my code, however theres a problem.

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

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

    @autoreleasepool {
        
        int  number, counter = 1, tempNumber;
        
        NSLog (@"Enter your number.");
        scanf ("%i", &number);

        if (number == 0)                            // check if number = 0
            NSLog(@"Zero");                         // if number = 0 print "Zero"
        
        tempNumber = number;                        // assign the value of number to tempNumber

                                                    // this loop counts the number of  digits in number        
        while (tempNumber > 1) {                    // while tempNumber is greater than one...
            tempNumber /= 10;                       // divide tempNumber by 10 and assign the value back to tempNumber
            counter *= 10;                          // multiply the value of counter by 10
        }

                                                    // this loop uses the value of counter to extract the digits from number  
        while (counter > 0) {                       // while counter is greater than one...
            tempNumber = number;                    // reassign the value of number to tempNumber
            tempNumber /= counter;                  // divide tempNumber by the value of counter and assign the value back to tempNumber
            counter /= 10;

            switch (tempNumber % 10) {              // divide tempNumber by 10 to give you the modulus of tempNumber
                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:
                    break;
            }
        }
    }
    return 0;
}

Everything works okay if enter 12345 I get:

One
Two
Three
Four
Five

but if I enter 901 I get:

Zero
Nine
Zero
One

Can someone please explain why this is as I can't see the problem and its driving me nuts  Huh
Last Edit: February 13, 2012, 04:10:23 PM by CloudDweller Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #4 on: February 13, 2012, 05:09:27 PM

Look at what happens to your code if the number ends in "1".

You just stumbled upon your logic error by accident!

Cheers,

Steve
Logged
CloudDweller
Newbie
*
Posts: 17






Reply #5 on: February 13, 2012, 05:39:57 PM

I hadn't realised that 123 would stop looping after two, where as 901 wouldn't stop until after.

Incase anyone runs into the same problem:
Test using 123
123/10 = 12  |  = 1 * 10 = 10
(12 is higher than 1 so continue)
12/10 = 1  |  = 10 * 10 = 100
(1 is not than 1 so stop)

Test using 901
901/10 = 90  |  = 1 * 10 = 10
(90 is higher than 1 so continue)
90/10 = 9  |  = 10 * 10 = 10
(9 is higher than 1 so continue)
9/10 = 0  |  counter = 10 * 10 = 10
(0 is not higher than 1 so stop)

I gotta say I've found this exercise very difficult indeed.  Its very disheartening when you've spent hours trying things out and you still can't find the solution Cry  And you have to resort to either a. asking for help or b. looking at other peoples answers.  Either way I feel like I'm cheating  Embarrassed

Thanks though Steve for all your help.  I really do appreciate it.
Last Edit: February 13, 2012, 05:41:42 PM by CloudDweller Logged
CloudDweller
Newbie
*
Posts: 17






Reply #6 on: February 14, 2012, 11:59:56 AM

Hi Steve,
Okay I stuck as it and spent some time refining my code to make it as efficient as possible.  I don't think that its as easy to read as my first attempt, but its definitely cleaner and I guess so long as I include detailed comments it should be okay.  I can't see any major problems but if you do, please let me know and I'll take a look.

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

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

    @autoreleasepool {
        
        int  number, counter;
        
        NSLog (@"Enter your number.");
        scanf ("%i", &number);
        
        for (counter = 1; (number / counter) > 9; counter *= 10);   // while number divided by counter is greater than 9, mutiply counter by 10 and assign value back to counter
        for (; counter > 0; counter /= 10) {                        // while counter is greater than 0 execute the loop and then divide counter by 10
            switch ((number / counter) % 10) {                      // divide number by counter and then divide again by 10 using the modulus operator
                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:
                    break;
            }
        }
    }
    return 0;
}

Thanks again for al your help.  I find that if I spend a few hours re-reading through the chapters, it really helps  Smiley
Last Edit: February 14, 2012, 12:01:59 PM by CloudDweller 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.