Thread: transforming code to one main function..

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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