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
AI Thread Summary
The discussion revolves around troubleshooting a C code that calculates output speed based on gear combinations. The user is facing two main issues: implementing an IF statement to filter combinations within a 10% range of a target output speed and resolving a loop that consistently prints zeros as output.Initial feedback highlights that the IF statement only checks the upper bound, neglecting the lower bound condition. Suggestions include modifying the condition to ensure N4 falls within both the upper and lower limits. Additionally, there are concerns about the use of integer types for calculations, which can lead to incorrect results due to integer division. Changing to float types is recommended for more accurate calculations.The user also seeks to generate all combinations of gear values from 12 to 42 and ensure that the output speed is printed only if it meets the specified criteria. The final code attempts to integrate these elements but still encounters issues. The discussion emphasizes the importance of correctly structuring the loops and conditions to achieve the desired output.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top