Monday, March 7, 2011

C#, If Statement, Min and Max

Good evening; I am writing a bit of code that solves the following equation.

X is size of device Y is quantity of device A is the denominator Z is the total diversified value

(X * Y)/A = Z

Here is the part I don't know how to accomplish. The value of A is found by the amount of Y. If Y is between 3 and 6 than A = .7, if Y is between 6 and 9 than A = .6; and so on.

What function should I use to accomplish the above? Any help is greatly appreciated.

Regards,

Greg Rutledge

From stackoverflow
  • You can use if and the comparison operators (<=). Homework?

    What do you mean by "and so on"? If the cases are regular then maybe you can use a formula instead of a bunch of if statements.

    if(Y <= 3.0)      A = ...; 
    else if(Y <= 6.0) A = 0.7; 
    else if(Y <= 9.0) A = 0.6;
    ...
    
    tejas_grande : No, its a calculator for some of the guys at work to use as a reference. It is meant to figure the diversified value of group mounted devices in an electrical panel. I know it looks like homework and I am sorry about that.
    tejas_grande : The and so on is because there are two more criteria that will produce a different denominator.
  • There are 3 approaches to this, IMO:

    1) Formula calculation. Thus, you want to know what A is given Y which if you have enough data, e.g. taking your .7 for 3<=Y<6, .6 for 6

    A = .8-(Y/3)/10.0;
    

    You may need to use a cast or truncate function on the Y/3 part if Y isn't a multiple of 3, or you could do this to take out the fractional part: (Y-(Y % 3))/3

    2) Use a while loop structure to take out the 3's of Y, note that the statements in the while are abbreviated which may make it a bit unclear:

    int Holder = Y, A=.8;
    while (Holder > 0)
    {
        A-= .1;
        Holder-= 3;
    }
    

    3) If/elseif. If Y is bounded then there is a brute force assignment strategy you could use:

     If Y<3 
        A=.8
     Else if Y < 6 
        A=.7
     Else if Y < 9 
        A=.6
    

    etc.

    This is in order of what I'd consider for solving such a problem.

  • Assuming A starts at 0.8 and decreases by 0.1 with every increase by 3 of Y:

    int temp = Y / 3;
    float A = 0.8f - (temp / 10f);
    
  • Here is an update to my question. I implemented the strategy suggested above. I'm not sure if I am executing it correctly.

    if ((cb5_1.Checked)&&(cb5_2.Checked)&&(cb5_3.Checked))
      {//if the first three text boxes are checked calculate based on the following. 
       decimal a, b, c, z;
       decimal aa, bb, cc, zz;
       a = decimal.Parse(cbx5_1a.Text);
       b = decimal.Parse(cbx5_2a.Text);
       c = decimal.Parse(cbx5_3a.Text);
       aa = decimal.Parse(cbx5_1q.Text);
       bb = decimal.Parse(cbx5_2q.Text);
       cc = decimal.Parse(cbx5_3q.Text);
       z = (aa+bb+cc);
    
       if (z = > 3)
       {
        zz = ((a*aa)+(b*bb)+(c*cc))*.7m;
       }
       else if (z = > 6)
       {
        zz = ((a*aa)+(b*bb)+(c*cc))*.6m;
       }
       else if(z = > 9)
       {
        zz = ((a*aa)+(b*bb)+(c*cc))*.5m;
       }
       tb5_atotal.Text = Math.Round(z,2).ToString();
    
    Aistina : Could you edit this so the variable names match with the naming you used in the first post? This is kinda confusing.
    tejas_grande : Sorry I was simplifying the equation. A, B, C, are X in the first post, and AA, BB, CC are Y in the first post.
    tejas_grande : Z is A in the first post and ZZ is Z in the first post
    Thomas G. Mayfield : General programming food for thought: What happens if you need to change the formula `((a*aa)+(b*bb)+(c*cc))`? What happens if you forget to change one of those 3 places?
    Timothy Carter : You want <= not >= because right now you could not ever reach any of your else if's. If z is greater than 6, say 7, it would never get to that else if because it would be greater than 3 to begin with.
    Juliet : Ouch! I hope you're not writing code like in production. For a start, give your variables meaningful names. Additionally, notice how you have "zz = ..." copy/pasted 3 times, but the only difference is the final constant -- you can factor out this common line of code.
    tejas_grande : Princess; can you give an example of what would be considered meaningful. I am new to programming; I bet you noticed. Can you provide me an example of creating meaningful names?
  • Ok, taking the code that you just posted, I think this is what you're looking for:

    if ((cb5_1.Checked)&&(cb5_2.Checked)&&(cb5_3.Checked))
    {
        //if the first three text boxes are checked calculate based on the following. 
        decimal a, b, c, d, z;
        decimal aa, bb, cc, zz;
        a = decimal.Parse(cbx5_1a.Text);
        b = decimal.Parse(cbx5_2a.Text);
        c = decimal.Parse(cbx5_3a.Text);
        aa = decimal.Parse(cbx5_1q.Text);
        bb = decimal.Parse(cbx5_2q.Text);
        cc = decimal.Parse(cbx5_3q.Text);
        z = (aa+bb+cc);
    
        d = 0.8m - ((z / 3) / 10m);
    
        zz = ((a*aa)+(b*bb)+(c*cc))*d;
    
        tb5_atotal.Text = Math.Round(z,2).ToString();
    
  • If the values are predefined in a list, and not created with a function.

    Basically each object has a minValue, maxValue, value, minLink and maxLink.

    Follow links until you find the target value or a null pointer.

    if Y <= maxValue then 
        if Y >= minValue then
            return value
        else
            follow minLink
    else 
        follow maxLink
    

0 comments:

Post a Comment