Thread: transforming code to one main function..

  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
    39,660
    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.

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

  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
    28,413
    Grr... stop trying to "reverse engineer" without understanding the code!

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

  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
    28,413
    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;
    }
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    39,660
    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.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    39,660
    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.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 11:45 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM