Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C Code loop and int help

  1. Jan 24, 2009 #1
    Hey

    I am having some issues with my C code.

    What it should do is calculate the output speed when a gear combination is used. It loops to find all combinations for 4 gears from 1 to 42, but can be changed.

    2 things i cant get working

    - IF STATEMENT - i want it to only show combintations that fall within a 10% range of the target output speed

    - LOOP - Keeps printing 0;s as the answer?

    Any help is appreciated.

    here is the code

    Code (Text):

    #include <stdio.h>
    #include <iostream>
    #include <time.h>



     

    int main()
    {
       
        //
        clock_t start, end;
        int q = 0;

        start = clock();

        for(q = 0; q < 99999999; q++);
        //
       
       
        // Count calculations
        int count = 0;
       
        // Gears
        int n1, n2, n3, n4;

        //Minimum teeth
        int tmin = 1;
       
        //Maximum teeth
        int tmax = 42;
       
        // Input speed
        int N1 = 9700;
       
        //Target Output speed
        int N4_Target = 1950;
       
        //Actual Output speed
        int N4;
       
        //To calculate
       
        int r1, r2;
       
        // Gear n1
        for (n1=tmin;n1<=tmax;n1++)
        {
            //gear n2
            for(n2=tmin;n2<=tmax;n2++)
            {
                 //gear n3
                 for(n3=tmin;n3<=tmax;n3++)
                 {
                      //gear n4            
                      for(n4=tmin;n4<=tmax;n4++)
                      {
                           // RATIO 1
                           
                           r1 = n2/n1;
                           
                           // RATIO 2
                           
                           r2 = n4/n3;
                           
                           // OUTPUT SPEED
                           
                           N4 = (n1*n3)/(n2*n4)*N1;
                           
                           // Lowest possible output speed
                           
                           int low_error = 0;
                           
                           low_error = (N4_Target/100)*90;
                           
                           // Highest possible output speed
                           
                           int high_error = 0;
                           
                           high_error = (N4_Target/100)*110;
                           
                           //printf("Low = %i, High = %i\n",low_error, high_error);
                           
                          if ( N4 <= high_error)
                          {
                     
                                  printf("(%i * %i) / (%i * %i ) * %i = %i\n",n1,n2,n3,n4,N1,N4);
                          }
                          else
                          {
                               
                           
                          }

                           
                             count++;
                                       
                                       
                      }
                                 
                 }
                             
                             
                 
            }
           
        }
       
        printf("%i\n\n\n",count);
       
       
        //
         end = clock();
         printf("Oeff.. %.2f sec's\n", (double)(end - start) / (double)CLOCKS_PER_SEC);
         //

       
       system("pause");
       
       
       return 0;  
       
    }


     
     
  2. jcsd
  3. Jan 24, 2009 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    I have my doubts about a gear with 0 teeth :P
    But anyway...

    You are only checking for the upper bound, you are not looking whether N4 >= lower_bound, are you?

    Can you be more specific as to which loop you are referring to? e.g. put a comment line where the 0's are being printed.

    Can you explain the use of that line?
    Also, I have my doubt about declaring
    Code (Text):
    int high_error = 0;
    inside the loops, I would suggest declaring it outside the loop:
    Code (Text):
    int high_error;
    for(...) {
       ...
       high_error = (N4_Target / 100) * 110;
       ...
    }
     
    or even not declare it at all:
    Code (Text):

    for(...) {
      ...
      if (N4 <=  (N4_Target * 1.10)) {
        ...
     }
     ...
    }
     
    Finally, what is the use of keeping the "count" variable... isn't it immediately clear that you have (tmax - tmin)4 combinations?
     
  4. Jan 24, 2009 #3
    Right, thanks for that, all taken and will try streamline the code and repost if i get the same error, thanks.
     
  5. Jan 24, 2009 #4
    Here is my second attempt.

    First let me shed some light on what i am trying to achieve.


    I want to calculate this forumula


    ((n1 * n3) / (n2*n4)) * N1

    Example would be

    ((12 * 40) /( 30 * 20)) * 9700 = 7760


    Doing this, i think i need to change int to a float as some of the divisions are 0.? numbers, and thus an int would just give 0 right?

    I want to create an IF that says, if N4 is less than 10% higher than the target, and is more than 10 lower than the target?

    like

    if (N4 <= N4_Target * 1.10) and (N4 >= N4_Target * 0.9)

    the error can be 10% either way of the target, hope this protrays this?



    Code (Text):

    #include <stdio.h>
     
    int main()
    {

        int n1, n2, n3, n4; // Gears
        int tmin = 12; //Minimum teeth  
        int tmax = 42; //Maximum teeth
        int N1 = 9700; // Input speed
        int N4_Target = 1950; //Target Output speed
        int N4; //Actual Output speed
     
        // Gear n1
        for (n1=tmin;n1<=tmax;n1++)
        {
            //gear n2
            for(n2=tmin;n2<=tmax;n2++)
            {
                 //gear n3
                 for(n3=tmin;n3<=tmax;n3++)
                 {
                      //gear n4            
                      for(n4=tmin;n4<=tmax;n4++)
                      {
                           
                           // OUTPUT SPEED
                           
                           N4 = (n1*n3)/(n2*n4)*N1;
     
                          if ( N4 <= N4_Target * 1.10)
                          {
                     
                                  printf("(%i * %i) / (%i * %i ) * %i = %i \n",n1,n2,n3,n4,N1,N4);
                          }
                               
                      }
                                 
                 }
     
            }
     
        }
       
       system("pause");
           
    }

     
     
  6. Jan 24, 2009 #5
    I have made the code so it runs with hard coded values.

    Now i want to make a loop so i can get all combinations for n1,n2,n3,n4 from 12 to 42.

    ie

    12*12 / 12*12 * N1

    to

    42*42 / 42*42 * N1

    Think notation is

    12..42*12..42 / 12..42*13..42 * N1

    thus calculating all possible numbers, next step would be to use an if to make sure than N4 is only output if its within 10% either way of the target speed.

    Am i making sense? if no i will try to elaberate more.

    Thanks again for all help

    Code (Text):


    #include <stdio.h>

    int main()
    {
        float n1=12,n2=40,n3=20,n4=30;
        float N1 = 9700;
        float N4;

        N4 = ((n1*n3)/(n2*n4))*N1;
       
        printf("(%2.0f x %2.0f) / (%2.0f x %2.0f) x %2.0f = %2.0f rpm\n\n",n1,n2,n3,n4,N1,N4);
       
        system("pause");
       
    }


     
    and this is the code i have made to calculate every combination

    Code (Text):

    #include <stdio.h>

    int main()
    {
       
        int i, j, k, l;
        int min = 12, max = 42;
       
        for(i=min;i<=max;i++)
        {
            for(j=min;j<=max;j++)
            {                  
                for(k=min;k<=max;k++)
                {
                    for(l=min;l<=max;l++)
                    {
                        printf("%i * %i * %i *%i\n",i,j,k,l);
                    }
                }                      
            }                                  
        }
       
        printf("%i\n",i);  
        system("pause");
     
    }

     
    Now i just need to mash them together, but it all goes wrong
     
    Last edited: Jan 24, 2009
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C Code loop and int help
  1. C++ Code Help (Replies: 3)

Loading...