# How to simplify these conditions ? (engine model c program)

This is a discussion on How to simplify these conditions ? (engine model c program) within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> #include <stdlib.h> float enginerpm, torq; torque() { if (enginerpm <1500) { torq = (0.03*(enginerpm - ...

1. ## How to simplify these conditions ? (engine model c program)

Code:
```#include <stdio.h>
#include <math.h>
#include <stdlib.h>

float enginerpm, torq;

torque()
{

if (enginerpm <1500)
{
torq = (0.03*(enginerpm - 1000)) + 160;
}
else
{
if (enginerpm <2000)
{
torq = (0.02*(enginerpm - 1500)) + 175;
}
else
{
if (enginerpm <2500)
{
torq = (0.02*(enginerpm - 2000)) + 185;
}
else
{
if (enginerpm <3000)
{
torq = (0.01*(enginerpm - 2500)) + 195;
}
else
{
if (enginerpm <3500)
{
torq = (0.01*(enginerpm - 3000)) + 200;
}
else
{
if (enginerpm <4000)
{
torq = (0.01*(enginerpm - 3500)) + 205;
}
else
{
if (enginerpm <4500)
{
torq = (0.01*(enginerpm - 4000)) + 210;
}
else
{
torq = 215;
}
}
}
}
}
}
}

}```

2. Like
Code:
```if ( ) {
} else if ( ) {
} else if ( ) {
} else if ( ) {
} else {
}```

3. Wrong section, methinks. There is a section for C, but this is the section for C++.

4. Moved

5. Grab a pen and paper and work out just what's happening there, and you can save yourself a bunch of those if checks.

Quzah.

6. Any way in which I can remove some 'if' statements???

7. Well you could plot torque vs. rpm on a graph using your function (it should be 7 points connected by straight lines).

Parametric equation - Wikipedia, the free encyclopedia
Then define a parametric equation for a curve which best fits those points.

8. Originally Posted by Salem
Well you could plot torque vs. rpm on a graph using your function (it should be 7 points connected by straight lines).

Parametric equation - Wikipedia, the free encyclopedia
Then define a parametric equation for a curve which best fits those points.
I don't know whether to laugh or cry after reading that. I might do a little of both.

Quzah.

9. Code:
```float torque(float enginerpm)
{
struct
{
int rpm_limit;
double multiplier;
} torque_list[] =
{
{1500, 0.03, 160 },
{2000, 0.02, 175 },
{2500, 0.02, 185 },
{3000, 0.01, 195 },
{3500, 0.01, 200 },
{4000, 0.01, 205 },
{4500, 0.01, 210 },
{INT_MAX, 0.0, 215 }
};

int i;

for (i = 0; torque_list[i].rpm_limit < enginerpm; i++)
{
}

return (torque_list[i].multiplier * (enginerpm - (torque_list[i].rpm_limit - 500))) + torque_list[i].adder;
}```

10. Is this compiled in c im getting errors when I try to compile in c.
INT_MAX undeclared
initialiser element is not compatible at load time
ISO 90 forbids mixed declaration and code

11. You need #include <limits.h> for INT_MAX

12. Originally Posted by Jason Singh
Is this compiled in c im getting errors when I try to compile in c.
INT_MAX undeclared
initialiser element is not compatible at load time
ISO 90 forbids mixed declaration and code
Did you #include <limits.h>?

13. Now I get get error
[Linker error] undefined reference to `WinMain@16'
returned 1 exit status

14. Im using
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>

15. > [Linker error] undefined reference to `WinMain@16'
Your IDE thinks you're creating a GUI program (which begins at WinMain), but you seem to be writing a console program, which begins at main.

Page 1 of 2 12 Last