How can I do this?

Discussion in 'Mac Programming' started by seriypshick, Sep 7, 2005.

  1. seriypshick macrumors member

    seriypshick

    Joined:
    Apr 4, 2005
    Location:
    Everywhere
    #1
    This is what it say in the book:
    How can I do this without using arrays? I'm using Objective-C.
     
  2. pdpfilms macrumors 68020

    pdpfilms

    Joined:
    Jun 29, 2004
    Location:
    Vermontana
    #2
    I'll just say that i'm glad i'm not taking that class....

    I am certainly NOT a programmer.
     
  3. seriypshick thread starter macrumors member

    seriypshick

    Joined:
    Apr 4, 2005
    Location:
    Everywhere
    #3
    If you're not a programmerm, then what are you doing here?
     
  4. durvivor macrumors member

    Joined:
    Jun 23, 2003
    #4
    How about a switch or a case statement.

    switch [ incoming ]

    1: printf " One ";
    2: printf " Two ";
    3: printf " Three ";
     
  5. seriypshick thread starter macrumors member

    seriypshick

    Joined:
    Apr 4, 2005
    Location:
    Everywhere
    #5
    Yes but that would only work with 1 digit number. How can i make it work with 2 or more digits?
     
  6. yippy macrumors 68020

    yippy

    Joined:
    Mar 14, 2004
    Location:
    Chicago, IL
    #6
    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.
     
  7. gekko513 macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #7
    Since you get the input as a string, you can loop through all the characters and do a switch on each.
     
  8. seriypshick thread starter macrumors member

    seriypshick

    Joined:
    Apr 4, 2005
    Location:
    Everywhere
    #8
    Can you show me the code. I'm still a noob.
     
  9. gekko513 macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #9
    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;
    }
    
     
  10. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #10
    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 ;
    }
     
  11. seriypshick thread starter macrumors member

    seriypshick

    Joined:
    Apr 4, 2005
    Location:
    Everywhere
    #11
    Thanks, everyone.

    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?
     
  12. gekko513 macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #12
    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?
     
  13. colocolo macrumors 6502

    Joined:
    Jan 17, 2002
    Location:
    Santiago, Chile
    #13
    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.
     
  14. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #14
    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!
     

Share This Page