Optimizing C Code: Help with Loops and If Statements for Gear Combinations

In summary, the code is trying to find combinations of gears to reach the target output speed, but is having problems.
  • #1
herpamad
78
0
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 can't 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:
#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;  
    
}
 
Technology news on Phys.org
  • #2
I have my doubts about a gear with 0 teeth :P
But anyway...

herpamad said:
- IF STATEMENT - i want it to only show combintations that fall within a 10% range of the target output speed
You are only checking for the upper bound, you are not looking whether N4 >= lower_bound, are you?

- LOOP - Keeps printing 0;s as the answer?
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.

Code:
    for(q = 0; q < 99999999; q++);
Can you explain the use of that line?
Also, I have my doubt about declaring
Code:
int high_error = 0;
inside the loops, I would suggest declaring it outside the loop:
Code:
int high_error;
for(...) {
   ...
   high_error = (N4_Target / 100) * 110;
   ...
}
or even not declare it at all:
Code:
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?
 
  • #3
Right, thanks for that, all taken and will try streamline the code and repost if i get the same error, thanks.
 
  • #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:
#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");
       
}
 
  • #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:
#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:
#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:

Related to Optimizing C Code: Help with Loops and If Statements for Gear Combinations

What is a loop in C code and how does it work?

A loop in C code is a programming construct that allows a certain block of code to be repeated multiple times until a certain condition is met. It works by executing the code inside the loop, then checking the condition. If the condition is still true, the code is executed again. This process continues until the condition becomes false, at which point the loop ends.

What is the purpose of using loops in C code?

The purpose of using loops in C code is to reduce the amount of code that needs to be written and to make the code more efficient. Instead of writing the same code multiple times, a loop allows us to execute it repeatedly with different values or conditions.

What are the different types of loops available in C code?

There are three types of loops available in C code: for loop, while loop, and do-while loop. Each of these loops has its own syntax and use cases, but they all serve the same purpose of repeating a block of code.

What is the difference between a for loop and a while loop in C code?

The main difference between a for loop and a while loop in C code is the way they control the iteration. A for loop uses a counter variable to control the number of iterations, while a while loop uses a condition to determine when to stop the loop.

How do I declare and use an integer variable in C code?

To declare an integer variable in C code, you can use the int keyword followed by the variable name. For example, int num; will declare an integer variable named num. To use the variable, you can assign a value to it using the assignment operator (=) and use it in your code.

Similar threads

  • Programming and Computer Science
Replies
1
Views
997
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top