Thread: how to change it into one main() function code..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to change it into one main() function code..

    Code:
    #include <stdio.h>
    
    int flip (int num);
    
    int main (void) {
      int i, j, c = 0;
    
      printf( "\nEnter the number to flip: ");
      scanf( "%d", &i);
    
      while (i != flip(i)) {
        j = flip(i);
        printf( "%d + %d = %d\n", i, j, i+j);
        c++;
        i += j;
      }
    
      printf( "Completed in %d steps.\n", c);
      return 0;
    }
    
    int flip (int num) {
      int digit, result = 0;
    
      while (num != 0) {
        digit = num % 10;
        result = result * 10 + digit;
        num /= 10;
      }
    
      return result;
    }
    Last edited by transgalactic2; 12-13-2008 at 12:15 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Integrate the flip() function into the while loop.

    Frankly, I think you are really learning C the wrong way round.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried in every form possible

    i tried to put before and after
    and use a temp variables
    and nothing gives me the same result

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Rewrite the original code such that you only call flip(i) once in the loop (you should be doing that anyway). Additionally, do not call it within the loop condition (this will make it easier for you to integrate flip() into the loop).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried like you said
    it doesnt work as before
    Code:
    #include <stdio.h>
    
    
    
    int main (void) {
      int i, j, c,l = 0;
    int digit, result = 0;
    
    
      printf( "\nEnter the number to flip: ");
      scanf( "&#37;d", &i);
    
    
      digit=0;
      result=0;
      while (i != 0) {
        digit = i % 10;
        result = result * 10 + digit;
        i /= 10;
      }
    
    l=result;
    
      while (i != l) {
    
    
            digit=0;
      result=0;
      while (i != 0) {
        digit = i % 10;
        result = result * 10 + digit;
        i /= 10;
      }
    
        j = result;
        printf( "%d + %d = %d\n", i, j, i+j);
        c++;
        i += j;
      }
    
      printf( "Completed in %d steps.\n", c);
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    thanks for pointing me to the correct thread laserlight.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As I noted, rewrite the code such that you only call flip(i) once in the loop. It is convenient if you do not call flip(i) outside of the loop either. It is even more convenient if flip(i) is the first thing in the loop body. I would use a controlled infinite loop, but a do while loop would also work:
    Code:
    #include <stdio.h>
    
    int flip(int num);
    
    int main(void) {
        int i, j, c = 0;
    
        printf( "\nEnter the number to flip: ");
        scanf( "&#37;d", &i);
    
        do {
            j = flip(i);
    
            if (i != j) {
                printf( "%d + %d = %d\n", i, j, i+j);
                c++;
                i += j;
            }
        } while (i != j);
    
        printf( "Completed in %d steps.\n", c);
        return 0;
    }
    
    int flip(int num) {
        int digit, result = 0;
    
        while (num != 0) {
            digit = num % 10;
            result = result * 10 + digit;
            num /= 10;
        }
    
        return result;
    }
    The transformation is a matter of cut and paste: you just need to assign i to num and result to j.

    By the way: how long more before you are allowed to use functions? If it is not soon, just quit the class: there is no point learning C without the use of proper abstraction being taught.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i solved this problem thanks

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    Quote Originally Posted by transgalactic2 View Post
    i solved this problem thanks
    YOU solved it?!? LOL! (just kidding) Congrats!

    Laserlight: Thanks for pointing out how *not* to call the flip function twice. I should have known better. There are a lot of smart people posting here. I think I will enjoy this forum a lot.

    -Dave

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And when will you learn proper indentation, transgalactic2?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mutandis
    Laserlight: Thanks for pointing out how *not* to call the flip function twice. I should have known better.
    Actually, if transgalactic2 was not trying to "undo the abstraction" by removing the flip() function call, I would rather have called flip() in two places:
    Code:
    j = flip(i);
    while (i != j) {
        printf("&#37;d + %d = %d\n", i, j, i+j);
        c++;
        i += j;
        j = flip(i);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. transforming code to one main function..
    By transgalactic2 in forum C Programming
    Replies: 13
    Last Post: 12-08-2008, 01:22 PM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM