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

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I got it to print the smallest pair for the input number.

    but i need also to do this proccess for every number smaller then the input to 1.

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

  2. #17
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to change this code so it will
    do the same process for every number smaller then the input until 1

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    how to change this code so it will
    do the same process for every number smaller then the input until 1
    Could you turn it into a function and then call the function in some loop with i--?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #19
    Registered User
    Join Date
    Jun 2011
    Posts
    4
    Hi, transgalactic2. Could you please help me with this task? its similar to yours:

    In the interval from 1 to the given number N, your task is to find the number which has the most divisors. In case there is more than one solution, find the smallest one.

    INPUT:
    From the standard input read the number N ( 0 < N < 2^31 ).

    OUTPUT:
    The standard output should contain two numbers (one in each line). The first line should contain the (smallest) number which has the most divisors, and the second line should contain the number of divisors.

  5. #20
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @Dule: Read the forum guidelines. Pay particular attention to #3, where it says don't post on threads that are more than 2 weeks old, like this one that is 2.5 years old!. And besides, you're trying to talk to transgalactic2. Notice how his user name is crossed out and it says "Banned" underneath. He ain't here no more. He's been given the boot. You need to start your own thread.

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