Amazon.com Widgets Help, Exercise 6, Chapter 6, programme doesn't work, Don't know why... Please
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 19, 2013, 02:35:49 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
| | | |-+  Help, Exercise 6, Chapter 6, programme doesn't work, Don't know why... Please
Pages: [1]   Go Down
Print
Author Topic: Help, Exercise 6, Chapter 6, programme doesn't work, Don't know why... Please  (Read 622 times)
Justwarrior
Newbie
*
Posts: 25


Email




« on: June 16, 2012, 10:11:49 AM »

Hello, programm doesn't work. It just stops after typing in any number such as 923, 933, 999

What is the problem ? Please help

Code: (Objective-C)
 int number, digits;
        scanf(" %i",&number);
        int s = number;
        if (number > 0) {
            for (int x = number; x!=0; digits++) { x /= 10;}
            for (int i = (digits - 1); i!=0; i--) {s /=10 ;}
            int t = number % 10;
           
           
            switch (t) {
                case '9':
                    NSLog(@"Nine");
                    break;
                case '8':
                    NSLog(@"Eight");
                    break;
                case '7':
                    NSLog(@"Seven");
                    break;
                case '6':
                    NSLog(@"Six");
                    break;
                case '5':
                    NSLog(@"Fife");
                    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;
            }
       
        } else { NSLog(@"ttt");}
       
Logged
Justwarrior
Newbie
*
Posts: 25


Email




« Reply #1 on: June 16, 2012, 01:14:35 PM »

Come on guys... help please
Logged
mitchb
Jr. Member
**
Posts: 98






« Reply #2 on: June 16, 2012, 07:18:59 PM »

Justwarrior,

Try this:

Code: (Objective-C)
int number, digits;  
    scanf(" %i",&number); 
    int s = number; 
    if (number > 0) { 
        for (int x = number; x!=0; digits++)
        {
            x /= 10;
        } 
        for (int i = digits; i!=0; i--)
        {
         
        int t = s % 10; 
       
       
        switch (t) {  // t is an int not a char.
            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; 
            }
            s /= 10;
        }
       
    } else { NSLog(@"ttt");}

Mitch
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
Justwarrior
Newbie
*
Posts: 25


Email




« Reply #3 on: June 17, 2012, 02:18:59 AM »

Thank you, but it says numbers from right to left... e.g.

932
two
three
nine


How to make it  nine, three, two ?

Thank you again Smiley
Logged
mitchb
Jr. Member
**
Posts: 98






« Reply #4 on: June 17, 2012, 10:26:50 AM »

You need to reverse the order of the digits. Just look back through the posts in this thread and you will see many examples of how to do this.

Mitch
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
Justwarrior
Newbie
*
Posts: 25


Email




« Reply #5 on: June 17, 2012, 12:37:34 PM »

Sorry... I didn't get you
Logged
john67
Newbie
*
Posts: 32






« Reply #6 on: June 17, 2012, 02:17:46 PM »

okay, breaking this program into chunks....

the following
Code: (Objective-C)
        for (int x = number; x!=0; digits++)   
        { 
            x /= 10; 
        }

ensures that digits is the number of digits in the entered number i.e. for 932 digits would equal 3

once we have the number of digits we use the modulus operator (%) to find the actual number

that's done in the next loop...

Code: (Objective-C)
        for (int i = digits; i!=0; i--)   
        { 
           
              int t = s % 10;
     
              // find text for 't' and output

             s/=10;
      }



LOOP1
digits = 3
t = s%10 = 932%10 = 2
(find text for 2 and output)
s/=10 = 932/=10 = 93

LOOP2
digits = 2
t = s%10 = 93%10 = 3
(find text for 3 and output)
s/=10 = 93/=10 = 9

LOOP3
digits = 1
t = s%10 = 9%10 = 9
(find text for 9 and output)
s/=10 = 9/=10 = 0 (note that as we are dealing with ints 9 divided by 10 = 0)

Now having explained that what you need to do in the looping is this...

LOOP1
digits = 3
t = s/100 = 932/100 = 9 (note only whole number as we're dealing with ints!)
(find text for 9 and output)
s-=(t*100)  = 32

LOOP2
digits = 2
t = s/10 = 32/10 = 3
(find text for 3 and output)
s-=(t*10)  = 2

LOOP3
digits = 1
t = s/1 = 2/1 = 2
(find text for 2 and output)
s-=(t*1)  = 0

program


Code: (Objective-C)
 for (int i = digits; i!=0; i--)  
           { 
                // get 10 to the power of i-1
                int mod = pow(10, i-1);
               
                // find our leftmost digit
                int t = s / mod;   
                         
                switch (t) { 
                // NSLog text value of t
                } 
               
                // remove our leftmost digit
                s = s -(t*mod);
            }
Logged
Justwarrior
Newbie
*
Posts: 25


Email




« Reply #7 on: June 17, 2012, 09:28:51 PM »

john67, Now I got everything, thank you so much!

Logged
mitchb
Jr. Member
**
Posts: 98






« Reply #8 on: June 18, 2012, 12:28:07 AM »

I added the reverse digits code.

Code: (Objective-C)
 int number, digits;
    int right_digit, rev_num;
    
    scanf(" %i",&number);  
    int s = number;  
    if (number > 0) {  
        for (int x = number; x!=0; digits++)
        {
            x /= 10;
        }
        
        while (number != 0) // reverse the digits
        {
            right_digit = number % 10;
            rev_num += right_digit;
            rev_num *= 10;
            number /= 10;
        }
        rev_num /= 10; // undo one to many divides
        
        for (int i = digits; i!=0; i--)
        {
          
        int t = rev_num % 10;  
        
        
        switch (t) {  // t is an int not a char.
            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;  
            }
            rev_num /= 10;
        }
        
    } else { NSLog(@"ttt");}  
}
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
siavashApp
Newbie
*
Posts: 17






« Reply #9 on: June 24, 2012, 10:45:00 AM »

Hi, I think the previous code by (mitchb) does not write "zero" when we just enter zero and also it does not work with negative numbers. So I appreciate you guys if you try this code and give your advices. 
Thanks.



#import<foundation/foundation.h>

int main()
{
    int num,n,i=-1,j=1,b;
    NSLog(@"Enter a number:");
    scanf("%i",&num);
    if(num<0)
    {
        NSLog(@" Negative");
        num=-num;
   
    }   
    n=num;
    if(num==0)
        NSLog(@" Zero");
    else
    {
        do
        {
           
            i++;
            num/=10;
           
        }while(num!=0);
        do
        {
            j=j*10;
            i=i-1;
        }while(i>0);
       
        do
        {
            b=n/j;
            n=n%j;
            j/=10;
           
            switch (b)
            {
                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(Cool:
                    NSLog(@" Eight");
                    break;
                case(9):
                    NSLog(@" Nine");
                    break;
            }
           
           
        }while(j!=0);
       

    }
       return 0;
   
   
   
}

and also I don't know why when I copied my code here, the 8 turns to a smiley face!!!!:D

« Last Edit: June 24, 2012, 10:54:39 AM by siavashApp » Logged
mitchb
Jr. Member
**
Posts: 98






« Reply #10 on: June 24, 2012, 03:55:28 PM »

Ok slavashApp,

Here is the missing code:
Code: (Objective-C)
#import<foundation/foundation.h>

int main()
{
    int number, digits; 
    int right_digit, rev_num; 
   
    NSLog(@"Enter number: ");
    scanf(" %i",&number);
   
    if (number < 0)
    {
        number = -number;
        NSLog(@"minus");
    }   
    if (number == 0)
        NSLog(@"zero");
   
    /*
     Count the number of digits. When a number like 9400 is entered reversing it becomes 49.
     This is used to fill in the missing zeros.
    */
   
    for (int x = number; x!=0; digits++) 
    { 
        x /= 10; 
    } 
       
    while (number != 0) // reverse the digits 
    { 
        right_digit = number % 10; 
        rev_num += right_digit; 
        rev_num *= 10; 
        number /= 10; 
    } 
    rev_num /= 10; // undo one to many divides 
       
    for (int i = digits; i!=0; i--) 
    { 
           
        int t = rev_num % 10;   
           
           
        switch (t)
        {   
            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;   
        } 
        rev_num /= 10; 
    }
    return 0;
   

I found your code hard to follow, more comments please.
Mitch
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
siavashApp
Newbie
*
Posts: 17






« Reply #11 on: June 25, 2012, 01:15:45 AM »

Sorry, you were right, code with no comments seems......
Ok, here are some comments. Im not good in describing things.


#import<foundation/foundation.h>

int main()
{
    int num,n,i=-1,j=1,b;
    NSLog(@"Enter a number:");
    scanf("%i",&num);
    if(num<0)
    {
        NSLog(@" Negative");    //Here we check the number to see if it is negative or positive,   
        num=-num;                  // in order to mention "negative" in terminal"
   
    }    n=num;
    if(num==0)
        NSLog(@" Zero");
    else
    {
        do
        {
           
            i++;             // here the first "do" check the numbers to find out how many digits
            num/=10;         // are entered.
           
        }while(num!=0);
        do
        {
            j=j*10;          // after finding out, for example if we type 432, then "i=3"
            i=i-1;           // now I declare another counter in order to start dividing the
        }while(i>0);         // number considering its digits, for 432, "j=100"
       
        do
        {
            b=n/j;           // finally this "do" start dividing the number, with this method we do                                                   
            n=n%j;           // not need to reverse the number.
            j/=10;
           
            switch (b)
            {
                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(Cool:
                    NSLog(@" Eight");
                    break;
                case(9):
                    NSLog(@" Nine");
                    break;
            }
           
           
        }while(j!=0);
       

    }
       return 0;
   
   
   
}

one problem with my code is , if we type "0235" or any other number starting with zero, the program does not work correctly. Although it seems ridicules because we say enter a number, numbers actually don't start with zero. But I'm working on it.

Siavash
Logged
clouded
Full Member
***
Posts: 123






« Reply #12 on: June 25, 2012, 05:45:32 AM »

http://classroomm.com/objective-c/index.php?topic=6862.0 my solution at the bottom solves this... And leading zeroes are handled differently
« Last Edit: June 25, 2012, 05:50:00 AM by clouded » 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.