Like Tree2Likes

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. #1
    Registered User
    Join Date
    May 2011
    Posts
    7

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Like
    Code:
    if ( ) {
    } else if ( ) {
    } else if ( ) {
    } else if ( ) {
    } else {
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Wrong section, methinks. There is a section for C, but this is the section for C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Moved
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    7
    Any way in which I can remove some 'if' statements???

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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.
    quzah likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem View Post
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Code:
    float torque(float enginerpm)
    {
       struct
       {
          int rpm_limit;
          double multiplier;
          int adder;
       } 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;
    }
    Salem likes this.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    7
    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. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    You need #include <limits.h> for INT_MAX
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by Jason Singh View Post
    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>?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    7
    Now I get get error
    [Linker error] undefined reference to `WinMain@16'
    returned 1 exit status

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    7
    Im using
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <limits.h>

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > [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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone help me simplify this?
    By Shackdaddy836 in forum C Programming
    Replies: 1
    Last Post: 10-07-2011, 02:56 PM
  2. Write a program to model a simple calculator
    By Annihilate in forum C Programming
    Replies: 5
    Last Post: 11-22-2010, 12:14 PM
  3. Simplify Fraction Help
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2009, 01:17 PM
  4. Search Engine C++ program
    By Jaidan in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2007, 01:29 AM
  5. simplify this?
    By markg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 08:09 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21