简体   繁体   中英

How to find the value in an array?

How to to find the value in an array, below i have given the code so far what i done.

 double finVal = 300;
    string[] dbvals ={"8^10","16^18","24^34","32^63","40^116","48^215","56^397","64^733","72^1354","80^2500"};
    
    for (int l = 0; l < dbvals.Length() ; l++)
    {
     string[] lc = dbvals[l].Split('^');
      double xl = Convert.ToDouble(lc[0]);
      double yl = Convert.ToDouble(lc[1]);
            if(y1>finVal)
            -- i want to find the value between 48 to 56
            --how to do it
    }

The following should get what you are after, dotnetfiddle sample

private static void BetweenTwoNumbers()
{
    double firstAssertion = 300; 

    string[] doubleValues =
    {
        "8^10", "16^18", "24^34", 
        "32^63", "40^116", "48^215", 
        "56^397", "64^733", "72^1354", 
        "80^2500"
    };

    for (int index = 0; index < doubleValues.Length; index++)
    {
        var parts = doubleValues[index].Split('^');
        var part1 = Convert.ToDouble(parts[0]);
        var part2 = Convert.ToDouble(parts[1]);


        if (part2 > firstAssertion && part1 is >= 48 and <= 56)
        {
            Console.WriteLine($"[{part1}], [{part2}]");
        }

    }

}

Verify the code:

I didn't want the answer as 48 or 56. the answer is between them. Verify the following code, this solves my purpose.

Link: https://dotnetfiddle.net/M2TDWT

using System;

public class Program
{
    public static void Main()
    {
        double finVal = 300;
        double oldval1 = 0;
        double oldval2 = 0;
        double ansval = 0;
        string[] dbvals = {"8^10", "16^18", "24^34", "32^63", "40^116", "48^215", "56^397", "64^733", "72^1354", "80^2500"};
        for (int index = 0; index < dbvals.Length; index++)
        {
            var parts = dbvals[index].Split('^');
            var part1 = Convert.ToDouble(parts[0]);
            var part2 = Convert.ToDouble(parts[1]);
            if (part2 > finVal)
            {
                ansval= Math.Round( (Convert.ToDouble(8)/Convert.ToDouble(part2-oldval2))*(finVal-oldval2));
                Console.WriteLine("The Given Find Value = "+ finVal);
                Console.WriteLine("The current first Array Element = "+ part1);
                Console.WriteLine("The current second Array Element = "+ part2);
                Console.WriteLine("The previous first Array Element = "+ oldval1);
                Console.WriteLine("The previous second Array Element = "+ oldval2);
                Console.WriteLine("The Difference between first Array Element = "+ (part1-oldval1));
                Console.WriteLine("The Difference between second Array Element = "+ (part2-oldval2));
                Console.WriteLine("The Difference between findvalue and previous second array = "+ (finVal-oldval2));
                Console.WriteLine("The Calculation : (firstarraydiff)/(secondarraydiff)*(givenfindval-previoussecondarrayvalue)");
                Console.WriteLine("The result (previoussecondarrayvalue+calculatedvalue) = " + (oldval1+ansval));
                Console.WriteLine("The Finded Value "+ (ansval));
                break;
            }
            else
            {
                oldval1 = part1;
                oldval2 = part2;
                
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM