- #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
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;
}