Amazon.com Widgets Chapter 6 excercise 6
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 19, 2013, 09:26:16 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
| | | |-+  Chapter 6 excercise 6
Pages: [1]   Go Down
Print
Author Topic: Chapter 6 excercise 6  (Read 1501 times)
gooda
Newbie
*
Posts: 1






« on: March 07, 2012, 07:33:16 AM »

Hello,

I'm new to programming and I'm trying to do this exercise.  I know how to spell out the numbers but I'm not sure how to get them in the right order.  For example, the mod function gets the remainder and my program spells out the number in the same sequence so the number is spelled backwards.  Can someoen please post the pseudocode or just an explanation on how to do this so when the user enters the number it is spelled correclty? Thanks!
Logged
judfio
Newbie
*
Posts: 10






« Reply #1 on: March 07, 2012, 08:41:35 AM »

Count the number of digits in your number by using (number % 10);
  each time,
  -you increase the value of a numberofdigits variable,
  -you set a divisor to be divisor * 10,
  -reset your number to be the value without the rightmost digit by dividing by 10.

Once you know how many digits you have you can start writing your number by dividing by your divisor
  each time you must divide the divisor by 10 and repeat.

You will need temporary variables so that you don't lose your original values.

Practice the logic by using a four digit number on paper so you can see it.  Think about how you need to initialize your variables before you begin the process.  Shout if you need more help. 

For example:  9876

  9876 % 10 = 6
  #digits = 1
  divisor = 1
  newnumber = 9876 / 10 = 987

  987 % 10 = 7
  #digits = 2
  divisor = 10
  newnumber = 987 / 10 = 98

  98 % 10 = 8
  #digits = 3
  divisor = 100
  newnumber = 98 / 10 = 9

  9 % 10 = 0
  #digits = 4
  divisor = 1000
  newnumber = 9 / 10 = 0

  stop

 take original number 9876 / divisor (which is 1000) = 9
 divisor = divisor / 10
 repeat


I hope this helps.

« Last Edit: March 07, 2012, 08:45:16 AM by judfio » Logged
kevin
Newbie
*
Posts: 6


Email




« Reply #2 on: March 21, 2012, 10:13:31 AM »

For what it's worth, here's my code for exercise 6.6. (be gentle, I'm sure it's ugly Smiley )

This was a tough one, as noted in the book, but I think it was mainly because of the looping. For me, at least, keeping track of loops within loops is quite difficult.
On that note, does anyone here have a good method for how to keep track of loops?

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

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

    @autoreleasepool
    {
       
        int number, numOfDigits, currentDigit;
       
        NSLog (@"Please input a number:");
        scanf (" %i", &number);
       
        if (number != 0)
        {
            //count how many digits there are. x is the counter//
            for (int x = number;  x != 0;)
            {
                x /= 10;
                numOfDigits++;
            }
           
            //loop numOfDigits times
            do
            {
                int tempNumber = number;
               
                for (int i = (numOfDigits - 1); i != 0; i--)
                {
                    tempNumber /= 10;
                }
               
                currentDigit = tempNumber % 10;
               
                switch (currentDigit)
                           {
                               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 (@"The input was not a number");
                           }
                numOfDigits--;
            }
            while (numOfDigits != 0);
        }
        else if (number == 0)
            NSLog (@"Zero");
       
        return 0;
    }
}


Note: This program does just what the exercise asks. There are no checks for user input error. Well, there is one: the "default" in the "switch" statement throws up an error if it encounters something that isn't a digit. In that case it just stops the program, which isn't an ideal solution.
« Last Edit: March 21, 2012, 10:17:42 AM by kevin » Logged
jackfig
Newbie
*
Posts: 2






« Reply #3 on: May 09, 2012, 09:50:57 PM »

I wrote this routine to reverse the order of the digits.  There's probably a cleaner way that would eliminate the last line after the "while" statement.

new_number, remainder = 0;

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

do {                                     
      remainder = number % 10;             
      number /= 10;
      new_number = new_number + remainder;
      new_number = new_number * 10;
   }
   while ( number > 9 );

new_number = new_number + number;
Logged
clouded
Full Member
***
Posts: 123






« Reply #4 on: May 11, 2012, 08:54:13 PM »

This one took a while to think up, but a counter hint could have helped:

Code: (Objective-C)
// Chapter 6 Exercise 6.
// Write a program that takes an integer keyed in from the
// terminal and extracts and displays each digit of the integer
// in English. So if the user types in 932, the program should
// display the following:
//  nine
//  three
//  two
// (Remember to display zero if the user types in just 0.)
// Note:This exercise is a hard one!

#import <Foundation/Foundation.h>

int main (int argc, char * argv[]) {
   
    @autoreleasepool {
        int input, value, n, a, b, c;
       
        NSLog (@"Enter your number.");
        scanf ("%i", &input);
        NSLog(@"Your Input was: %i", input);
       
        if ( input < 0 ) {
            input *= -1;
            NSLog (@"negative");
        }
       
        value = input;
        n = 0;
        do {
            value /= 10;
            n++;
        } while ( value != 0 );
       
        value = input;
        int stor[n + 1];
        for ( a = 1 ; a < n + 1 ; ++a ) {
            stor[a] = value % 10;
            value /= 10;
        }
       
        for ( b = n ; b > 0 ; --b ) {
            c = stor[b];
            switch (c) {
                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 (@"Unknown number.");
                    break;
            }
        }
    }
    return 0;
}

Output:

Enter your number.
-987
Your Input was: -987
negative
nine
eight
seven
Logged
siavashApp
Newbie
*
Posts: 17






« Reply #5 on: June 26, 2012, 07:40:57 AM »

I think you misunderstood my point here clouded. By zero I mean when for example typing (009) then the program answer ( zero zero nine).
The code that I put in http://classroomm.com/objective-c/index.php?topic=7284.0 just deal with 0 and two digits after that and I don't know why.
But I think the main goal of this question is that to deal with digits and just one zero entered, which means both code here are right.
Logged
Ahmed Al H
Newbie
*
Posts: 11







« Reply #6 on: July 16, 2012, 08:55:18 PM »

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

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

    @autoreleasepool
    {
       
        int number, rightDigit, numberOfDigits, memory;
       
        scanf("%i", &number);
        memory = number;
       
        //FINDS THE NUMBER OF DIGITS.
        do
        {
            if (number < 0)
            {
               number = -number;
            }
            rightDigit = number % 10;
            numberOfDigits += 1;
            number /= 10;
           
           
        } while (number != 0);
       
        //LOOP AND MEMORY BECAUSE THE VALUE OF NUMBER CHANGED WHEN COUNTING THE DIGITS
        number = memory;
        while (numberOfDigits != 0)
        {
            for (int x = numberOfDigits; x > 0; --x)
            {
                rightDigit = number % 10;
                number /= 10;
               
            }
            switch (rightDigit) {
                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(@"Unknown Character");
                    break;
            }
            //AGAIN WITH THE MEMORY TO SET IT BACK TO ITS ORIGINAL VALUE BEFORE THE LOOP BEGINS AGAIN.
            number = memory;
            --numberOfDigits;
        }
    }
    return 0;
}

Logged
mo7ionsickness
Jr. Member
**
Posts: 50






« Reply #7 on: July 25, 2012, 03:02:52 PM »

quite difficult this one  Huh

here's mine:

Code: (Objective-C)
int main(int argc, const char * argv[])
{
    @autoreleasepool {        

        long int number, new_number, divisor = 1;
        
        NSLog(@" Enter your number");
        scanf("%li", &number);
        
        new_number = number;

        do {
            new_number /= 10;
            divisor *= 10;
        }
        while (new_number != 0);
        
        for ( divisor /= 10; divisor > 0; divisor /= 10) {
            
            switch ((number / divisor) % 10) {
                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;
}
output 1:

2012-07-25 22:48:07.651 prog5[6912:403]  Enter your number
1234567890
2012-07-25 22:48:11.892 prog5[6912:403] one
2012-07-25 22:48:11.893 prog5[6912:403] two
2012-07-25 22:48:11.893 prog5[6912:403] three
2012-07-25 22:48:11.894 prog5[6912:403] four
2012-07-25 22:48:11.894 prog5[6912:403] five
2012-07-25 22:48:11.894 prog5[6912:403] six
2012-07-25 22:48:11.895 prog5[6912:403] seven
2012-07-25 22:48:11.895 prog5[6912:403] eight
2012-07-25 22:48:11.896 prog5[6912:403] nine
2012-07-25 22:48:11.896 prog5[6912:403] zero

output 2:

2012-07-25 23:01:52.958 prog5[6925:403]  Enter your number
0
2012-07-25 23:01:54.775 prog5[6925:403] zero
« Last Edit: July 25, 2012, 03:07:31 PM by mo7ionsickness » Logged
Lightenbulb
Newbie
*
Posts: 14






« Reply #8 on: September 09, 2012, 11:10:57 AM »

Okay everyone. I am feeling like a major moron. I love Ahmed's program and I understand every line of it (the counter loop and the loop to print out the numbers are very nicely isolated) but I still do not understand how it flips the isolated right digit around so that it prints out in the correct order. Can anyone explain that to mean please?
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.