Amazon.com Widgets Exercise 6.6
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 19, 2014, 04:46:39 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 2 3 [4] 5 6 Go Down
Print
Author Topic: Exercise 6.6 (Read 20101 times)
jclermont
Jr. Member
**
Posts: 55


Email




Reply #45 on: June 22, 2009, 12:41:51 PM

I took one semester of C and one semester of C++ a while back, but that's about it. I am proficient in PHP and VB.NET though, so sometimes I know the function in those languages, and dig a little to find the matching function in C.

Thanks for the kind words.
Logged
ryanhunt
Newbie
*
Posts: 2


Email




Reply #46 on: July 01, 2009, 11:09:10 PM

I was a bit puzzled by this one. I read everyone else's answers and I was impressed by them. The old English saying of 'there is more than one way to skin a cat' holds true here.

I've done programming at University, but it was many, many years ago. Despite this, I remembered arrays. So I flicked through the book, found the NSArray example in the back and applied it to this exercise. I also looked up NSMutableArray on apple.com as I knew I needed one of these to chop & change the array.

This is my solution to this exercise.

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

int main (int argc, const char* argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

int number, rightDigit;

NSArray* numberWords = [NSArray arrayWithObjects: @"zero", @"one", @"two", @"three", @"four", @"five", @"six", @"seven", @"eight", @"nine", nil];
NSMutableArray* arr = [[NSMutableArray alloc] init];

NSLog(@"Enter your number");
scanf("%i", &number);
if (number < 0) {
number = -number;
NSLog(@"Negative");
}

do {
rightDigit = number % 10;
number /= 10;
[arr insertObject:[numberWords objectAtIndex:rightDigit] atIndex:0];
} while (number!=0);

int i=0;

while (i < [arr count]) {
NSLog(@"%@", [arr objectAtIndex:i++]);
}

[arr release];
[pool drain];
return 0;
}
Logged
harrydeo
Newbie
*
Posts: 4


Email




Reply #47 on: July 04, 2009, 09:53:21 PM

My solution is different from all the posted answers so I thought I will share

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

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int number, posnumber, right_digit;

int len; //Length of the enterd value
NSLog(@"Enter your number.");
scanf("%i", &number);

//Calculating the length of the value
char buffer[50];


sprintf(buffer,"%d",number); //Converting int to string

//Using strlen to calculate the length of the value
len = strlen(buffer);

//Creating an array with the size of the length
int name[len];
int index=0; //Initializing index value
if (number > 0)
{
do {
right_digit = number % 10;

//Storing values in the array
name[index] = right_digit;

//Incrementing the index
index++;

number /= 10;
}
while (number != 0);
int l;

//Running the reverse loop to print the values of the array
for(l=len-1;l>=0;l--)
{
switch (name[l]) {
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;
}
}
}
else
{
NSLog(@"NEGATIVE NUMBER");
}

    [pool drain];
    return 0;
}
Last Edit: July 04, 2009, 10:08:09 PM by harrydeo Logged
James
Newbie
*
Posts: 12






Reply #48 on: July 06, 2009, 08:26:05 PM

Here's mine:
Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int number,reverseNumber,rightDigit,count;

NSLog(@"Enter your number:");
scanf("%i",&number);
NSLog(@"Number entered: %i",number);

//check for negative
if(number<0){
//print negative
NSLog(@"Negative");
//remove sign
number=-number;
}
reverseNumber=0;
count=0;
do {
rightDigit=number%10;
reverseNumber=(reverseNumber*10)+rightDigit;
number=(number-rightDigit)/10;
count++;
}while(number>0);

do {
rightDigit=reverseNumber%10;
switch (rightDigit) {
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(@"Something went wrong");
break;
}

reverseNumber=(reverseNumber-rightDigit)/10;
count--;
}while(reverseNumber>0);
while(count>0){
NSLog(@"Zero");
count--;
}
    [pool drain];
    return 0;
}
Logged
SiriusA
Newbie
*
Posts: 22






Reply #49 on: July 14, 2009, 11:42:25 PM

don't think I'm adding much here -- my take is pretty straight forward, handling zero and negative numbers, etc. just wanted to get into the pool with everyone!

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   int userInput, displayNumber;
   BOOL isNegative;
   
   
   NSLog (@"Please give me a number!");
   scanf ("%i", &userInput);
   
   isNegative = NO; // initialize this to NO
   
   if (userInput<0)
   {
      isNegative = YES;
      NSLog (@"negative");
      userInput = -(userInput);
   }
   
   if (!(userInput==0)) // as long as it's not zero, go through the number
   {
      while (userInput>0)
      {
      displayNumber = userInput % 10;
      userInput /= 10;
      //NSLog (@"'%i'", displayNumber);

      
         switch (displayNumber)
         {
            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;   
         }
         
      }
   } else
      NSLog (@"zero");
      
    [pool drain];
    return 0;
}
Logged
oraculo666
Newbie
*
Posts: 5






Reply #50 on: July 17, 2009, 11:22:52 PM

Here's my version of it:

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

@interface SpellNumber : NSObject
{
int number;               // Stores user number
int revNum; // Stores reversed number
int numLen; // Stores the number length
}

-(void) setNumber: (int) value; // Sets passed value into 'number' instance variable

-(void) testNegative; // Tests if number is negative. If YES - turns it into
// it's absolute value and prints the word 'Negative'

-(void) reverseNumber; // Reverses the number stored in 'number', sets it
// in 'revNum' and sets it's length

-(void) spellReverse; // Reverse spell the number stored in 'revNum'

@end

@implementation SpellNumber

-(void) setNumber: (int) value {
number = value;
}

-(void) testNegative {
if (number < 0) {
number = -number;
NSLog(@"Negative");
}
}

-(void) reverseNumber {
int rightDigit = 0;
numLen = 0;
do {
rightDigit = number % 10;
revNum *= 10;
revNum += rightDigit;
number /= 10;
++numLen;
}while (number!= 0);
}

-(void) spellReverse {
int rightDigit;
for(; numLen > 0 ; numLen--) {

rightDigit = revNum % 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;
default:
NSLog(@"Zero");
break;
}
revNum /= 10;
}
}

@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int num;

NSLog(@"Please enter an integer number:"); // Asks user for an integer number
scanf("%i", &num);

SpellNumber *myNumber = [[SpellNumber alloc] init];

[myNumber setNumber: num];
[myNumber testNegative];
[myNumber reverseNumber];
[myNumber spellReverse];

[myNumber release];

    [pool drain];
    return 0;
}
Logged
TheWoz
Newbie
*
Posts: 27






Reply #51 on: July 18, 2009, 02:10:07 PM

don't think I'm adding much here -- my take is pretty straight forward, handling zero and negative numbers, etc. just wanted to get into the pool with everyone!

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   int userInput, displayNumber;
   BOOL isNegative;
   
   
   NSLog (@"Please give me a number!");
   scanf ("%i", &userInput);
   
   isNegative = NO; // initialize this to NO
   
   if (userInput<0)
   {
      isNegative = YES;
      NSLog (@"negative");
      userInput = -(userInput);
   }
   
   if (!(userInput==0)) // as long as it's not zero, go through the number
   {
      while (userInput>0)
      {
      displayNumber = userInput % 10;
      userInput /= 10;
      //NSLog (@"'%i'", displayNumber);

      
         switch (displayNumber)
         {
            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;   
         }
         
      }
   } else
      NSLog (@"zero");
      
    [pool drain];
    return 0;
}
I think you didn't get the real exercise. The exercise is to display each digit in the sequence they are written in the number. So if the user types 922, your app has to display nine two two and not two two nine as your app does.
Last Edit: July 18, 2009, 02:11:38 PM by TheWoz Logged
SiriusA
Newbie
*
Posts: 22






Reply #52 on: July 18, 2009, 06:47:25 PM

I don't think I got the full exercise. Thanks, it was valuable to re-read that and work on solving that part of the problem.
Logged
TheWoz
Newbie
*
Posts: 27






Reply #53 on: July 19, 2009, 02:34:07 AM

Post it then. ;-)
Logged
TheWoz
Newbie
*
Posts: 27






Reply #54 on: July 19, 2009, 05:04:45 AM

I've used a bit of the code of slessardjr. I wasn't able to find the calculations to reverse the number... Anyway, I was on the right way.

I think the code of slessardjr could be written a bit cleaner. You don't need as many variables as he uses. You can solve the problem with a few simple do-while's.

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

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

int right_digit;
long long int number, reverseNumber;


right_digit = 0;
reverseNumber = 0;
number = 0;

printf("Enter your number: ");
scanf(" %lli", &number);

if (number < 0)
{
printf("negative\n");
number = -number;
}

do
{
right_digit = number % 10;
reverseNumber *= 10;
reverseNumber += right_digit;
number /= 10;
}
while (number!= 0);

do
{
right_digit = reverseNumber % 10;

switch (right_digit)
{
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;
default:
printf("zero ");
break;
}

reverseNumber /= 10;
}
while(reverseNumber != 0);

    return 0;
}
Logged
frank_ribson
Newbie
*
Posts: 6






Reply #55 on: July 22, 2009, 08:04:42 PM

seems like I used a similar approach though ... took awhile to figure out how to reverse the number and then solve the problem of integers that end in zero ....

***********************************

#import <Foundation/Foundation.h>


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   int number_to_translate;
   int last_digit;
   int number_of_digits;
   int temporary_number;
   int reversed_number;
   int zero_checker;
   int n;
   
    NSLog(@"Type the integer number you would like translated here");
   scanf ("%i", &number_to_translate);
   
   last_digit = number_to_translate;
   temporary_number =  number_to_translate;
   reversed_number = 0;
   zero_checker = number_to_translate;
   
   while (temporary_number > 0)
   {
      reversed_number *= 10;
      last_digit = temporary_number % 10;
      reversed_number += last_digit;
      temporary_number /= 10;
   }
   
   number_to_translate = reversed_number;
   
   for (n=2; reversed_number != 0; ++n)
   {
   last_digit = reversed_number % 10;
   reversed_number = reversed_number / 10;
   number_of_digits = n;
   }

   
   for (n=1; n < (number_of_digits); ++n)
   {   
      last_digit = number_to_translate % 10;
      number_to_translate = number_to_translate / 10;
      
      switch (last_digit)
      {
         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;
         default:
            printf(" nine ");
            break;
         }
   }

      
   if (zero_checker % 10 == 0)
      printf("zero");
   

   [pool drain];
    return 0;
}
Logged
Kageri
Newbie
*
Posts: 2






Reply #56 on: July 30, 2009, 01:05:31 PM

Here is my solution for exercise 6.6. 

#import <Foundation/Foundation.h>
#import <math.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   int number,numberCopy, numberReversed;
   int digitCount;
   int right_digit, numberCopy_right_digit, numberReversed_right_digit;
   int power;
   int i;
   BOOL isNegative = FALSE;
   
   NSLog (@"Enter your number.");
   scanf ("%i", &number);
   
   numberCopy = number;
   
   //  Test for negative number
   if ( number < 0 )
      {
         number = -number;
         numberCopy = -numberCopy;
         isNegative = TRUE;
      }
   

   //  loop to determine the number of digits in the number
   do   {
         right_digit = number % 10;
         digitCount++;
         number /= 10;
      }
      while ( number != 0 );
   
   power = digitCount - 1;
   
   //  loop to create the reversed entered number
   for ( i = 0; i <= digitCount; i++ )
   {
      numberCopy_right_digit = numberCopy % 10;
      numberReversed += numberCopy_right_digit * pow( 10, power);
      power--;
      numberCopy /= 10;
   }
   
   if ( isNegative)
      NSLog ( @"Negative");
   
   
   //  loop to print out the number in words
   for ( i = 1; i <= digitCount; i++ )
   {
         numberReversed_right_digit = numberReversed % 10;
         
         switch ( numberReversed_right_digit )   
         {
            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;
            }
            
         numberReversed /= 10;
      }

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






Reply #57 on: July 30, 2009, 02:38:26 PM

I thought this exercise was quite hard, but I seem to have come up with something similar(ish) to most of you

Here's mine:
Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int number, placeValue, leftDigit, x;  //placeValue = whether it's a one, ten, hundred.... etc..

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

if (number < 0) {
NSLog(@"Error - Negative number");
return 1; }  //Negative no. produces error

if (number == 0)
NSLog(@"zero");  //Handles a single zero

while (number >= 1) {


for (placeValue = 1, leftDigit = number; leftDigit >= 10; placeValue *= 10)
leftDigit /= 10;  //This calculates the value of the leftDigit and records its placeValue


switch (leftDigit) { //This switch statement prints the leftDigit
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;
}
//Checks to see if the number drops by more than 1 place value, and therefore starts with at least one zero
if ((number - placeValue * leftDigit) < placeValue /10) {
x = placeValue;  //x is used to temporarily hold the value of placeValue so that placeValue itself is not altered

do {
NSLog(@"zero");
x /= 10; }
while ((number - placeValue * leftDigit) < x /10); }  //Then prints "zero" until the place value is correct

/* The calculation below removes the left most digit
e.g. if the number is 432 then the place value is 100 and the left digit is 4
100 * 4 = 400, 432 - 400 leaves 32, which is sent back into the loop to extract the 3 & then the 2 */

number -= placeValue * leftDigit;
}

    [pool drain];
    return 0;
}
Logged
Shifty
Newbie
*
Posts: 8


Email




Reply #58 on: July 31, 2009, 06:36:16 PM

Here is mine. My first post here even!

Code: (Objective-C)
// Program to display each digit of the integer in English

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int right_digit1, right_digit2, number, reversed_number = 0, counter= 0;

    NSLog(@"Enter your integer value:");
scanf("%i", &number);

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


do {
right_digit1 = number % 10;
if (right_digit1 == 0)
++counter;
reversed_number = reversed_number * 10 + right_digit1;
number /= 10;
}
while (number != 0);

do {
right_digit2 = reversed_number % 10;
switch (right_digit2) {
case 0:
NSLog(@"zero");
counter--;
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 command.");
break;
}
reversed_number /= 10;
}
    while (reversed_number != 0);

for (int n = 1; n <= counter; n++) {
NSLog(@"zero");
}

[pool drain];
    return 0;
}
Last Edit: July 31, 2009, 06:38:01 PM by Shifty Logged
akishore
Newbie
*
Posts: 1


Email




Reply #59 on: February 11, 2010, 08:05:21 PM

Here is my solution:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   int i, j, number, numbernew, missingzero, n, count, multiplier;

    NSLog(@"Please enter an integer number:");
   scanf("%i", &number);
   
   numbernew = number;
   count = 0;
   j = 0;
   multiplier = 1;
   
   while (number != 0) {
   
      while (numbernew != 0) {
         n = numbernew % 10;
         numbernew /= 10;
         count++;
      }
      
      switch (n) {
         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(@"Error: Unknown value");
            n = 0;
            break;
      }
      
      
      for (i = 1; i< count; i++) {
         multiplier *= 10;
      }
      
      number = number % (multiplier);
      
      missingzero = number;
      
      while (missingzero != 0) {
         missingzero /= 10;
         j++;
      }
      
      if ((count - 1) != j)
      {
         for (i = 1; i <= ((count - 1) - j); i++)
         {
            NSLog(@"zero");
         }
      }
      
      numbernew = number;
      missingzero = numbernew;
      count = 0;
      j=0;
      multiplier = 1;
      
   }
   
   
    [pool drain];
    return 0;
}
Logged
Pages: 1 2 3 [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.