Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

seriypshick

macrumors member
Original poster
Apr 4, 2005
76
0
Everywhere
This is what it say in the book:
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 a 0.) Note: This exercise is a hard one!

How can I do this without using arrays? I'm using Objective-C.
 

durvivor

macrumors member
Jun 23, 2003
87
75
seriypshick said:
This is what it say in the book:


How can I do this without using arrays? I'm using Objective-C.

How about a switch or a case statement.

switch [ incoming ]

1: printf " One ";
2: printf " Two ";
3: printf " Three ";
 

seriypshick

macrumors member
Original poster
Apr 4, 2005
76
0
Everywhere
durvivor said:
How about a switch or a case statement.

switch [ incoming ]

1: printf " One ";
2: printf " Two ";
3: printf " Three ";
Yes but that would only work with 1 digit number. How can i make it work with 2 or more digits?
 

yippy

macrumors 68020
Mar 14, 2004
2,087
3
Chicago, IL
Divide the number. First devide by say 1000, and discard the remainder (there is a function in C that does this at least) and apply the switch rule to it. Then take the remainder and divide again by 100 and repeat.
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
Since you get the input as a string, you can loop through all the characters and do a switch on each.
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
This could have some typos, and it's plain C, no Objective C is used. Make sure you understand everything as I assume you want to learn this. Feel free to ask if you have any questions.
Code:
#include <stdio.h>

int main (int argc, const char *argv[]) {
  char ch;
  char buffer[256];
  int bufferIndex;

  // loop and read new numbers until the user enters an empty line
  printf("Hit Enter twice to quit\n");
  do {
    bufferIndex = 0;

    // read a number
    printf("Enter a number: ");
    while (bufferIndex < 255 && (ch = getchar()) != '\n') {
      buffer[bufferIndex] = ch;
      bufferIndex++;
    }
    buffer[bufferIndex] = 0;

    // write back the digits in plain English
    bufferIndex = 0;
    while (buffer[bufferIndex] != 0) {
      switch (buffer[bufferIndex]) {
      case '0':
        printf("zero ");
        break;
      case '1':
        printf("one ");
        break;
      // and so on until '9'
      bufferIndex++;
    }
    printf("\n");

  } while (bufferIndex > 0);

  return 0;
}
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
40,077
8,336
Los Angeles
Here's a version in C that takes the input from the command line. So in a terminal window (shell) you can type "showdigits 49971" and it will output the line "four nine nine seven one". (49971 is your member number.)

Here's the program:
Code:
/* showdigits.c - by Doctor Q */

#include <stdio.h>

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

char *mypointer ;

if ( argc > 1 ) {
    for ( mypointer = argv[1] ; *mypointer != '\0' ; ++mypointer )
        switch(*mypointer) {
            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 ;
            }
    printf("\n") ;
    }

return 0 ;
}
 

seriypshick

macrumors member
Original poster
Apr 4, 2005
76
0
Everywhere
Thanks, everyone.

gekko513 said:
This could have some typos, and it's plain C, no Objective C is used. Make sure you understand everything as I assume you want to learn this. Feel free to ask if you have any questions.
Code:
#include <stdio.h>

int main (int argc, const char *argv[]) {
  char ch;
  char buffer[256];
  int bufferIndex;

  // loop and read new numbers until the user enters an empty line
  printf("Hit Enter twice to quit\n");
  do {
    bufferIndex = 0;

    // read a number
    printf("Enter a number: ");
    while (bufferIndex < 255 && (ch = getchar()) != '\n') {
      buffer[bufferIndex] = ch;
      bufferIndex++;
    }
    buffer[bufferIndex] = 0;

    // write back the digits in plain English
    bufferIndex = 0;
    while (buffer[bufferIndex] != 0) {
      switch (buffer[bufferIndex]) {
      case '0':
        printf("zero ");
        break;
      case '1':
        printf("one ");
        break;
      // and so on until '9'
      bufferIndex++;
    }
    printf("\n");

  } while (bufferIndex > 0);

  return 0;
}
Because I also know C I was thinking the same thing. I also think this is the most natural way of doing it.

However, in the book that I'm reading(Programming in Objective-C) author(Stephen Kochan) assumes that previous example would be possible without the use of arrays. (I'm reading 6th chapter, and arrays are introduced in 13th chapter).

Is it at all possible?
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
Well, the program either needs to process each character as you enter it and write out the digit straight away, or it has to store the characters somewhere.

Of course you can make a bunch of variables and use a clumsy kind of nested if to read some characters and store them, but that would be just silly.

Code:
int a, b, c, d, e;
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
if ((a = getchar()) != '\n') {
  if ((b = getchar()) != '\n') {
    if ((c = getchar()) != '\n') {
      if ((d = getchar()) != '\n') {
        e = getchar()) != '\n');
      }
    }
  }
}
// and then write them out in a similar nested if

Has the book covered strings in some way or another?
 

colocolo

macrumors 6502
Jan 17, 2002
480
132
Santiago, Chile
As a former student/teacher of computer science related subjects, i bet whoever corrects this would love a nice recursive function... it just goes off to show you know what you are doing and its far more modular than a simple iteration in the main.

Just a tip, always use functions. If the assignment has any follow-ups, it will make things way easier and faster most of the time. Simple C code will suffice, no need to implement any classes.

I won't elaborate on the function to do this, as I believe the only way of learning is to try by yourself. It's better to get a C and learn something than to copy to get an A.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
40,077
8,336
Los Angeles
This reminds me of a problem I had on a final exam in a programming class. "Write a program that reads a decimal amount in dollars and cents, between 0.01 and 999.99, and prints the English equivalent, e.g., 24.13 would produce "twenty four dollars and thirteen cents". We were to write the program on paper, not on a computer, during the test.

I think I constructed an algorithm to do it, and I got an A on the test. But a classmate told me later that he wrote something like this:
if ( amount == 0.01 ) printf("one cent") ;
else if ( amount == 0.02 ) printf("two cents") ;
...
else if ( amount == 999.99 ) printf("nine hundred ninety nine dollars and ninety nine cents") ;​
I guess he assumed either the compiler knew how to fill in the "..." part, or else the professor would understand that his solution was a 99,999-line nested if statement. Either way, I doubt he got a good grade on that problem!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.