Thread: i cant see why the code is not working??

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

    i cant see why the code is not working??

    Abundant numer is a number which the sum of it dividers is bigger
    then the divider itself.
    12 : 1+2+3+4+6=16>12

    I need to build a program which gets a number input.
    It prints all the numbers bellow it which could be written as the sum of two
    abundant numbers and present its sum.
    If there is more then one combination for the sum,we need to print the sum with the smallest number for all the sums .
    for example if i enter 54:
    There are two possible abbondant number sum 54=12+42 =18+36
    The program will print only
    54=12+42

    I have built a program and who prints only the pairs of the number
    but it shows -1 -1 as if on all the range the is no such numbers
    ??

    Code:
    #include <stdio.h>
    
    void  main(){
    int num;
    int index,tndex,kndex,jndex;
    int flag_1,flag_2;
    int sum;
    int smallest,smallest_ever,smallest_ever_2;
    sum=0;
    flag_1=0;
    flag_2=0;
    smallest=-1;
    smallest_ever=-1;
    smallest_ever_2=-1;
    printf("enter number\n");
    scanf("%d",&num);
    
    for(index=0;index<num;index++){ //start for main number
    
    	for(tndex=0;tndex<index;tndex++){//start for first num
            for(kndex=0;kndex<index;kndex++){//start for second num
    
    			if (tndex+kndex==index){ //start sum check
    		
    				for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num
                        
    					if (tndex%jndex==0){
                        sum=sum+jndex;
    					}
    
    				}//end abbondence check for first num
    				if (tndex<sum){
    					flag_1=1;
    				}
    
                      sum=0;
    				for(jndex=1;jndex<kndex;jndex++){//start abbondence check for first num
                        
    					if (kndex%jndex==0){
                        sum=sum+jndex;
    					}
    
    				}//end abbondence check for first num
    				if (kndex<sum){
    					flag_2=1;
    				}
                        
    				if ((flag_2==1)&&(flag_1==1)){  //start flag check
    					if (kndex>=tndex){
                          smallest=tndex;
    
    					  if (smallest<=smallest_ever){
    					smallest_ever=smallest;
    					smallest_ever_2=kndex;
    					  }
    					}
    					else
    					{
    						smallest=kndex;
                          
    						if (smallest<=smallest_ever){
    					smallest_ever=smallest;
    					smallest_ever_2=tndex;
    					  }
    
    					}
    					
    				}//end flag check
    
    			} //end sum check
    
    		}//end for second num
    	}//end for first num
    
    printf("%d %d",smallest_ever,smallest_ever_2);
    
    }//end for main number
    
    }//end main

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Even with good indentation, it's still hard to read.
    Code:
    #include <stdio.h>
    
    void  main(){
        int num;
        int index,tndex,kndex,jndex;
        int flag_1,flag_2;
        int sum;
        int smallest,smallest_ever,smallest_ever_2;
        sum=0;
        flag_1=0;
        flag_2=0;
        smallest=-1;
        smallest_ever=-1;
        smallest_ever_2=-1;
        printf("enter number\n");
        scanf("%d",&num);
    
        for(index=0;index<num;index++){ //start for main number
            for(tndex=0;tndex<index;tndex++){//start for first num
                for(kndex=0;kndex<index;kndex++){//start for second num
                    if (tndex+kndex==index){ //start sum check
                        for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num
                            if (tndex%jndex==0){
                                sum=sum+jndex;
                            }
                        }//end abbondence check for first num
    
                        if (tndex<sum){
                            flag_1=1;
                        }
    
                        sum=0;
                        for(jndex=1;jndex<kndex;jndex++){//start abbondence check for first num
                            if (kndex%jndex==0){
                                sum=sum+jndex;
                            }
    
                        }//end abbondence check for first num
                        if (kndex<sum){
                            flag_2=1;
                        }
                        
                        if ((flag_2==1)&&(flag_1==1)){  //start flag check
                            if (kndex>=tndex){
                                smallest=tndex;
                                if (smallest<=smallest_ever){
                                    smallest_ever=smallest;
                                    smallest_ever_2=kndex;
                                }
                            }
                            else
                            {
                                smallest=kndex;
                                if (smallest<=smallest_ever){
                                    smallest_ever=smallest;
                                    smallest_ever_2=tndex;
                                }
                            }
                        }//end flag check
                    } //end sum check
                }//end for second num
            }//end for first num
    
            printf("%d %d",smallest_ever,smallest_ever_2);
        }//end for main number
    }//end main
    1. You really should be off the void main kick by now.
    2. Appending 'ndex' to your single letter variables does not make it any more readable.

    To break up the massive nesting of loops, use a function which does some of the work. It'll simplify your main()
    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
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    first i take all the possible sums of each number from one till my input number
    then i check if they both are abundant .then i take the smallest pair possible

    its very straight forward
    where is m problem??

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's no need to have a loop on kndex; just set kndex = num - tndex and be done with it. (Note that you don't want the two to sum to index, but to num.) Actually index doesn't appear to do anything either, so you can get rid of that too.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    still i get pairs of -1 -1
    Code:
    #include <stdio.h>
    
    
    #include <stdio.h>
    
    void  main(){
        int num;
        int index,tndex,kndex,jndex;
        int flag_1,flag_2;
        int sum;
        int smallest,smallest_ever,smallest_ever_2;
        sum=0;
        flag_1=0;
        flag_2=0;
        smallest=-1;
        smallest_ever=-1;
        smallest_ever_2=-1;
        printf("enter number\n");
        scanf("&#37;d",&num);
    
        for(index=0;index<num;index++){ //start for main number
            for(tndex=0;tndex<num;tndex++){//start for first num
                 kndex=num-tndex;
                  sum=0;
                        for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
                            if (tndex%jndex==0){
                                sum=sum+jndex;
                            }
                        }//end abbondence check for first num
    
                        if (tndex<sum){
                            flag_1=1;
                        }
    
                        sum=0;
                        for(jndex=1;jndex<kndex;jndex++){//start abbondence check for second num (kndex)
                            if (kndex%jndex==0){
                                sum=sum+jndex;
                            }
    
                        }//end abbondence check for first num
                        if (kndex<sum){
                            flag_2=1;
                        }
    
                        if ((flag_2==1)&&(flag_1==1)){  //start flag check
                            if (kndex>=tndex){
                                smallest=tndex;
                                if (smallest<=smallest_ever){
                                    smallest_ever=smallest;
                                    smallest_ever_2=kndex;
                                }
                            }
                            else
                            {
                                smallest=kndex;
                                if (smallest<=smallest_ever){
                                    smallest_ever=smallest;
                                    smallest_ever_2=tndex;
                                }
                            }
                        }//end flag check
    
    
            }//end for first num
    
            printf("%d %d",smallest_ever,smallest_ever_2);
        }//end for main number
    }//end main
    Last edited by transgalactic2; 12-07-2008 at 03:19 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It is not possible for you to run this code. This code doesn't compile, which means you could possibly be running the previous code. Specifically, you get these errors:
    Code:
    $ gcc -Wall -Wextra -o temp temp.c
    temp.c: In function ‘main’:
    temp.c:22: error: syntax error before ‘for’
    temp.c:22: warning: statement with no effect
    temp.c:22: error: syntax error before ‘)’ token
    temp.c:62: warning: control reaches end of non-void function
    temp.c: At top level:
    temp.c:64: error: syntax error before string constant
    temp.c:64: warning: type defaults to ‘int’ in declaration of ‘printf’
    temp.c:64: error: conflicting types for ‘printf’
    temp.c:64: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
    temp.c:64: warning: data definition has no type or storage class
    which must be fixed before you get anything at all that you can run. (I changed void main to int main for you.)

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i have changed it
    i switched the IF to smallest>=smallest_ever
    for input 21
    i get 9 1210 1110 1110 1110 1110 1110 1110 1110 1110 1110 1110 1110

    it should give me only 9 12
    why i get those extra numbers???
    Code:
    #include <stdio.h>
    
    
    #include <stdio.h>
    
    int  main(){
        int num;
        int index,tndex,kndex,jndex;
        int flag_1,flag_2;
        int sum;
        int smallest,smallest_ever,smallest_ever_2;
        sum=0;
        flag_1=0;
        flag_2=0;
        smallest=-1;
        smallest_ever=-1;
        smallest_ever_2=-1;
        printf("enter number\n");
        scanf("&#37;d",&num);
    
        for(index=0;index<num;index++){ //start for main number
            for(tndex=0;tndex<num;tndex++){//start for first num
                 kndex=num-tndex;
                           sum=0;
                        for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
                            if (tndex%jndex==0){
                                sum=sum+jndex;
                            }
                        }//end abbondence check for first num
    
                        if (tndex<sum){
                            flag_1=1;
                        }
    
                        sum=0;
                        for(jndex=1;jndex<kndex;jndex++){//start abbondence check for second num (kndex)
                            if (kndex%jndex==0){
                                sum=sum+jndex;
                            }
    
                        }//end abbondence check for first num
                        if (kndex<sum){
                            flag_2=1;
                        }
    
                        if ((flag_2==1)&&(flag_1==1)){  //start flag check
                            if (kndex>=tndex){
                                smallest=tndex;
                                if (smallest>=smallest_ever){
                                    smallest_ever=smallest;
                                    smallest_ever_2=kndex;
                                }
                            }
                            else
                            {
                                smallest=kndex;
                                if (smallest>=smallest_ever){
                                    smallest_ever=smallest;
                                    smallest_ever_2=tndex;
                                }
                            }
                        }//end flag check
    
    
            }//end for first num
    
            printf("%d %d",smallest_ever,smallest_ever_2);
        }//end for main number
        return 0;
    }//end main
    Last edited by transgalactic2; 12-07-2008 at 03:31 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because your print is inside the for loop, rather than after all your calculations are complete?

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i put it in the main loop because i was told to choose all the numbers which are smaller of the inputed number.

    and from these numbers i need to chose the numbers who are a sum of two abundant numbers
    Last edited by transgalactic2; 12-07-2008 at 03:49 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes, but choosing != printing. You do need to compute numbers in a loop. You don't need to print numbers in a loop.

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed If to smallest<=smallest_ever

    Code:
    #include <stdio.h>
    
    
    #include <stdio.h>
    
    int  main(){
        int num;
        int index,tndex,kndex,jndex;
        int flag_1,flag_2;
        int sum;
        int smallest,smallest_ever,smallest_ever_2;
        sum=0;
        flag_1=0;
        flag_2=0;
        smallest=-1;
        smallest_ever=-1;
        smallest_ever_2=-1;
        printf("enter number\n");
        scanf("&#37;d",&num);
    
        for(index=0;index<num;index++){ //start for main number
            for(tndex=0;tndex<num;tndex++){//start for first num
                 kndex=num-tndex;
                           sum=0;
                        for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
                            if (tndex%jndex==0){
                                sum=sum+jndex;
                            }
                        }//end abbondence check for first num
    
                        if (tndex<sum){
                            flag_1=1;
                        }
    
                        sum=0;
                        for(jndex=1;jndex<kndex;jndex++){//start abbondence check for second num (kndex)
                            if (kndex%jndex==0){
                                sum=sum+jndex;
                            }
    
                        }//end abbondence check for first num
                        if (kndex<sum){
                            flag_2=1;
                        }
    
                        if ((flag_2==1)&&(flag_1==1)){  //start flag check
                            if (kndex>=tndex){
                                smallest=tndex;
                                if (smallest<=smallest_ever){
                                    smallest_ever=smallest;
                                    smallest_ever_2=kndex;
                                }
                            }
                            else
                            {
                                smallest=kndex;
                                if (smallest<=smallest_ever){
                                    smallest_ever=smallest;
                                    smallest_ever_2=tndex;
                                }
                            }
                        }//end flag check
    
    
            }//end for first num
    
            printf("%d %d",smallest_ever,smallest_ever_2);
        }//end for main number
        return 0;
    }//end main

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need to keep some how the smallest pair for each number
    how to do that

    when i put -1 in each of them
    there wasnt any smaller number
    so it gave me pare of -1


    ??

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I dont know how to builld such a program
    ive done every thing

    i cant see my mistake

    for a number 54 number i get 1 and 53 and copies of 54 0 i cant see
    what do i need to correct?

    Code:
    #include <stdio.h>
    
    
    #include <stdio.h>
    
    int  main(){
        int num;
        int index,tndex,kndex,jndex;
        int flag_1,flag_2;
        int sum;
        int smallest,smallest_ever,smallest_ever_2;
        sum=0;
        flag_1=0;
        flag_2=0;
        smallest=-1;
        smallest_ever=-1;
        smallest_ever_2=-1;
        printf("enter number\n");
        scanf("&#37;d",&num);
    
        for(index=1;index<num;index++){ //start for main number
             tndex=index;
                 kndex=num-tndex;
                           sum=0;
                        for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
                            if (tndex%jndex==0){
                                sum=sum+jndex;
                            }
                        }//end abbondence check for first num
    
                        if (tndex<sum){
                            flag_1=1;
                        }
    
                        sum=0;
                        for(jndex=1;jndex<kndex;jndex++){//start abbondence check for second num (kndex)
                            if (kndex%jndex==0){
                                sum=sum+jndex;
                            }
    
                        }//end abbondence check for first num
                        if (kndex<sum){
                            flag_2=1;
                        }
    
                        if ((flag_2==1)&&(flag_1==1)){  //start flag check
                            if (kndex>=tndex){
                                smallest=tndex;
                                if ((smallest<smallest_ever)&&(smallest>1)){
                                    smallest_ever=smallest;
                                    smallest_ever_2=kndex;
                                }
                            }
                            else
                            {
                                smallest=kndex;
                                if ((smallest<smallest_ever)&&(smallest>1)){
                                    smallest_ever=smallest;
                                    smallest_ever_2=tndex;
                                }
                            }
                        }//end flag check
    
    
    
      printf("%d %d",smallest_ever,smallest_ever_2);
    
    
        }//end for main number
      return 0;
    }//end main

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to reset your flags inside the loop, otherwise everything is abundant.

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I did your change
    and i added a code where i put the printing line inside the flag check if
    and i get the correct output 12 42

    but several time

    and i dont know how to keep the smallest ever value
    ??
    Last edited by transgalactic2; 12-07-2008 at 05:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 08:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM