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

This is a discussion on how to change it into one main() function code.. within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int flip (int num); int main (void) { int i, j, c = 0; printf( "\nEnter the ...

  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 11:15 AM.

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

    Frankly, I think you are really learning C the wrong way round.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,794
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,047
    thanks for pointing me to the correct thread laserlight.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    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
    21,794
    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);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:07 AM
  2. transforming code to one main function..
    By transgalactic2 in forum C Programming
    Replies: 13
    Last Post: 12-08-2008, 12: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, 01:53 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21