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

  • Thread starter Thread starter herpamad
  • Start date Start date
  • Tags Tags
    C code Code Loop
Click For Summary

Discussion Overview

The discussion revolves around optimizing C code for calculating output speeds based on various gear combinations. Participants are addressing issues related to loops and conditional statements, specifically focusing on ensuring that the output speeds fall within a specified range of a target speed.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant is struggling with an if statement that should filter combinations based on a 10% range of the target output speed.
  • Another participant points out that the current implementation only checks for the upper bound of the output speed and suggests also checking the lower bound.
  • Concerns are raised about the presence of zero values in the output, with a request for clarification on which loop is producing these values.
  • Suggestions are made to declare certain variables outside of loops to improve code efficiency and clarity.
  • A participant proposes changing integer types to floats to handle divisions that may result in non-integer values.
  • There is a discussion about the necessity of a count variable, with one participant questioning its purpose given the predictable number of combinations.
  • Another participant expresses a desire to ensure that the output is only displayed if it falls within a 10% range of the target speed, providing a specific conditional structure for this check.
  • One participant shares their progress in creating a loop to calculate all combinations of gear teeth and seeks confirmation on their understanding of the task.

Areas of Agreement / Disagreement

Participants express various concerns and suggestions, but there is no consensus on the best approach to resolve the issues raised. Multiple competing views on how to implement the logic and structure of the code remain present.

Contextual Notes

Participants mention potential issues with variable types and the handling of zero values in calculations, but these concerns are not fully resolved. The discussion reflects ongoing exploration of coding strategies without definitive conclusions.

herpamad
Messages
78
Reaction score
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
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?
 
Right, thanks for that, all taken and will try streamline the code and repost if i get the same error, thanks.
 
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");
       
}
 
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:

Similar threads

  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 46 ·
2
Replies
46
Views
9K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K