transforming code to one main function..

This is a discussion on transforming code to one main function.. within the C Programming forums, part of the General Programming Boards category; i got this working code where i built functions and called them how to transform this code into one main ...

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

    transforming code to one main function..

    i got this working code
    where i built functions and called them
    how to transform this code into one main function??
    i tried it didnt give me the output i got before
    ??
    Code:
    #include<stdio.h>
    
    
    
    int is_abundant(int tndex) {
    int jndex,sum;
    int flag_1;
    flag_1=0;
    sum=0;
    for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
                            if (tndex&#37;jndex==0){
                                sum=sum+jndex;
                            }
                            if (tndex<sum){
                            flag_1=1;
                        }
    
                        }//end abbondence check for first num
    
     return flag_1;
    }
    
    void print_if_sum_of_abundants(int n) { 
     int i;
    	for( i=1;i*2<=n;++i) {
    		if(is_abundant(i)&&is_abundant(n-i)) { 
    
    			printf("%d=%d+%d\n",n,i,(n-i));
                  i=n+1;
    
    } 
      } 
    } 
    
    int main() { 
      int limit;
    int i;
    printf("enter number:");
       scanf("%d",&limit);
      
      
      for(i=1;i<=limit;++i) {
        print_if_sum_of_abundants(i); 
      }
      return 0;
    }
    know i need to transform all of my program into one "main" function code
    i tried to do that but it gives me a very bad output

    Code:
    #include<stdio.h>
    
    
    
    
    
    int main() { 
        int index;  
    	int limit,i;
    	int flag_1=0;
    	int flag_2=0;
    	int jndex,sum;
    int number;
    printf("enter number:");
       scanf("%d",&limit);
      
      
      for(number=0;number<=limit;++number) {/////////////////////////////////// start main for
        
    	for( index=1;index*2<=number;++index) {
    
    
    
    ///////////////////////////////////////////////////////////abondence check 1
    
    
    
    sum=0;
    for(jndex=1;jndex<index;jndex++){//start abbondence check for first num (tndex)
                            if (index%jndex==0){
                                sum=sum+jndex;
                            }
                            if (index<sum){
                            flag_1=1;
                        }
    
                        }//end abbondence check for first num
    		/////////////////////////////////////////////////end abondence check 1
    
    
    ///////////////////////////////////////////////////////////abondence check 2
    
    
    
    sum=0;
    for(jndex=1;jndex<(number-index);jndex++){//start abbondence check for first num (tndex)
                            if ((number-index)%jndex==0){
                                sum=sum+jndex;
                            }
                            if ((number-index)<sum){
                            flag_2=1;
                        }
    
                        }//end abbondence check for first num
    		/////////////////////////////////////////////////end abondence check 2
    
    
    
    
    
    
    
    		if((flag_1==1)&&(flag_2==1)) { 
    
    			printf("%d=%d+%d\n",number,index,(number-index));
                  index=number+1;
    			  flag_1=0;
    			  flag_2=0;
    
    } 
      }
    
      } ///////////////////////////////////////////////end main for
      return 0;
    }
    Last edited by transgalactic2; 12-08-2008 at 10:58 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Kinda hoping that you'll learn how to indent code at some point.
    It's a long wait....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Why would you want to have just one main function?
    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

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Because my prof didnt allow us to use matirial that he didnt teach.

    So i need to give one main function.

    I tried to substitute (like i showed ) but i get a very bad output.
    ??

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Grr... stop trying to "reverse engineer" without understanding the code!

    And as Salem noted, indent your code properly.
    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

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I will try.
    how to order the code properly?

    i try to keep each open and close cols parralell

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by transgalactic2
    how to order the code properly?
    The rule of thumb is: whenever the code enters into a block, indent the code by one level, and keep the levels consistent. When the code leaves the block, decrease the indentation by one level.

    Also, most of the time there is no point in leaving more than one blank line to separate code, (and for those exceptions, two blank lines will do).

    Your first code sample would be much easier to read when indented properly, e.g.,
    Code:
    #include<stdio.h>
    
    int is_abundant(int tndex) {
        int jndex,sum;
        int flag_1;
        flag_1=0;
        sum=0;
        for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
            if (tndex&#37;jndex==0){
                sum=sum+jndex;
            }
            if (tndex<sum){
                flag_1=1;
            }
    
        }//end abbondence check for first num
    
        return flag_1;
    }
    
    void print_if_sum_of_abundants(int n) {
        int i;
        for( i=1;i*2<=n;++i) {
            if(is_abundant(i)&&is_abundant(n-i)) {
    
                printf("%d=%d+%d\n",n,i,(n-i));
                i=n+1;
    
            }
        }
    }
    
    int main() {
        int limit;
        int i;
        printf("enter number:");
        scanf("%d",&limit);
    
        for(i=1;i<=limit;++i) {
            print_if_sum_of_abundants(i);
        }
        return 0;
    }
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Well the first thing you do is go into the editor settings, and set it to "use spaces when I press tab" option, and set it to 4 spaces.

    If you only use ALL spaces, or ALL tabs, then the board makes a reasonable job of it.

    But as soon as you mix them up (and throw in some couldn't give a damn as well), then it's just a big sorry mess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Quote Originally Posted by Salem View Post
    Well the first thing you do is go into the editor settings, and set it to "use spaces when I press tab" option, and set it to 4 spaces.
    Are you certain? This is more of a style question, since it has advantages and disadvantages.
    I would just advise to use whatever you feel is best. So long as you do not mix tabs and spaces.
    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.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    If you have enough tools in your tool chain - edit, compare, print, merge, etc etc, then sooner or later, something will make a complete mess of tabs, especially if you set the width to anything other than the de-facto 8 spaces.

    Spaces on the other hand are universal and mean the same everywhere. Proper WYSIWYG.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Quote Originally Posted by Salem View Post
    Spaces on the other hand are universal and mean the same everywhere. Proper WYSIWYG.
    And causes complete headaches, because IDEs are not smart enough to treat them like tabs.
    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.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    20
    Quote Originally Posted by transgalactic2 View Post
    I will try.
    how to order the code properly?

    i try to keep each open and close cols parralell
    Use e-macs it does it for you :P

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Which IDEs are you using?

    Actually, I'm not interested in this x vs. y conversation. If you're using an editor which lacks even basic "smart indent", then you need to find a better editor.

    TG2 needs to learn what works, and the magic of the "preview post" button.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Quote Originally Posted by pc_doctor View Post
    Use e-macs it does it for you :P
    As well as many IDEs and editors.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to change it into one main() function code..
    By transgalactic2 in forum C Programming
    Replies: 10
    Last Post: 12-13-2008, 10:02 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 01:42 PM

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