Click here to Skip to main content
15,898,987 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Can someone help me write code to satisfy the following algorithm:

input: MeasuredValue
HighLimit
LowLimit

output: PASS/ FAIL boolean based on the following logic

If the measured value is <= Highlimit and >= Low Limit
return PASS
else return FAIL.

I would like to implement this in C# in a class so it is reusable.

Thanks,

Sophronis Mantoles
Posted

1 solution

This is very basic and you already have the algorithm. How complex and/or robust you want to make it is up to you.

Personally I'd use a struct/class like this:
C#
public struct MeasurementBoundsF
{
    public static readonly MeasurementBoundsF Empty = new MeasurementBoundsF();
    private float highLimit;
    private float lowLimit;
    public MeasurementBoundsF(float lowLimit, float highLimit)
    {
        if (highLimit > lowLimit)
        {
            this.lowLimit = lowLimit;
            this.highLimit = highLimit;
        }
        else
        {
            this.lowLimit = highLimit;
            this.highLimit = lowLimit;
        }
    }
    public float HighLimit
    {
        get { return highLimit; }
    }
    public float LowLimit
    {
        get { return lowLimit; }
    }
    public PassFail GetPassFail(float measuredValue)
    {
        return (measuredValue <= highLimit) && (measuredValue >= lowLimit) ? PassFail.Pass : PassFail.Fail;
    }
}
I've used an enum instead of a bool so I can extend the possible results in future:
C#
public enum PassFail
{
    Fail = 0,
    Pass = 1
}
In use you could do something like this:
C#
float measuredValue;
float highLimit;
float lowLimit;

Console.Write("Measured value? ");
if (float.TryParse(Console.ReadLine(), out measuredValue))
{
    Console.Write("High limit? ");
    if (float.TryParse(Console.ReadLine(), out highLimit))
    {
        Console.Write("Low limit? ");
        if (float.TryParse(Console.ReadLine(), out lowLimit))
        {
            MeasurementBoundsF measurementBoundsF = new MeasurementBoundsF(lowLimit, highLimit);
            Console.WriteLine(measurementBoundsF.GetPassFail(measuredValue));
        }
        else
            Console.WriteLine("Low limit was invalid");
    }
    else
        Console.WriteLine("High limit was invalid");
}
else
    Console.WriteLine("Measured value was invalid");
Console.ReadKey();
If you don't understand it then I suggest you purchase a book or two and study and/or study some online beginners tutorials.

If this is for homework (which I suspect), I would strongly suggest you don't copy and paste the above as your tutor may ask you to explain it, and could quite possibly visit this site (many do) and will know you have cheated.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900