2D Arrays

Marcos

New member
Joined
Mar 31, 2022
Messages
2
Programming Experience
Beginner
Hey guys. I´m trying to develop a code for my engineering classes and having some truble with arrays.

I´m using a 2D array as a matrix and need to find the value vx and vy.
I have a previous value "lambda" and I need to compare this value with the first column( 0) . I´m looking for a value in the first column that is equal or higher than "lambda", when I find it I´ll assume that my "vx" and my "vy" are the values in the same line but in the next column. Column 1 for "vx" and Column 2 for "vy". My "else" code is working perfectly, but I can´t solve this "while" loop. Could someone help me with that ?

Example: My "lambda" is equal to 1.46. I´ll lsearch in the first column for a value equal or higher. So I´ll find RA [10,0] = 1.50 and then my "vx" will be RA[10,1] = 3.33 and my "vy" will be RA[10,2] = 2.50

Thanks in advance.



C#:
double [,] RA = new double[21, 3];

                RA[0, 0] = 1.00; RA[0, 1] = 2.50; RA[0, 2] = 2.50;
                RA[1, 0] = 1.05; RA[1, 1] = 2.62; RA[1, 2] = 2.50;
                RA[2, 0] = 1.10; RA[2, 1] = 2.73; RA[2, 2] = 2.50;
                RA[3, 0] = 1.15; RA[3, 1] = 2.83; RA[3, 2] = 2.50;
                RA[4, 0] = 1.20; RA[4, 1] = 2.92; RA[4, 2] = 2.50;
                RA[5, 0] = 1.25; RA[5, 1] = 3.00; RA[5, 2] = 2.50;
                RA[6, 0] = 1.30; RA[6, 1] = 3.08; RA[6, 2] = 2.50;
                RA[7, 0] = 1.35; RA[7, 1] = 3.15; RA[7, 2] = 2.50;
                RA[8, 0] = 1.40; RA[8, 1] = 3.21; RA[8, 2] = 2.50;
                RA[9, 0] = 1.45; RA[9, 1] = 3.28; RA[9, 2] = 2.50;
                RA[10, 0] = 1.50; RA[10, 1] = 3.33; RA[10, 2] = 2.50;
                RA[11, 0] = 1.55; RA[11, 1] = 3.39; RA[11, 2] = 2.50;
                RA[12, 0] = 1.60; RA[12, 1] = 3.44; RA[12, 2] = 2.50;
                RA[13, 0] = 1.65; RA[13, 1] = 3.48; RA[13, 2] = 2.50;
                RA[14, 0] = 1.70; RA[14, 1] = 3.53; RA[14, 2] = 2.50;
                RA[15, 0] = 1.75; RA[15, 1] = 3.57; RA[15, 2] = 2.50;
                RA[16, 0] = 1.80; RA[16, 1] = 3.61; RA[16, 2] = 2.50;
                RA[17, 0] = 1.85; RA[17, 1] = 3.65; RA[17, 2] = 2.50;
                RA[18, 0] = 1.90; RA[18, 1] = 3.68; RA[18, 2] = 2.50;
                RA[19, 0] = 1.95; RA[19, 1] = 3.72; RA[19, 2] = 2.50;
                RA[20, 0] = 2.00; RA[20, 1] = 3.75; RA[20, 2] = 2.50;

             
                if(lambda<2)
                {
                    int i = 0;

                    while (lambda <= RA[i,0])
                    {
                        vx = RA[i, 1];
                        vy = RA[i, 2];

                        i++;
                    }

                    Vx = vx * (ct * lx / 1000);             // Here my code do not recognize vx (local variable not attributed)
                    tb_vx.Text = Vx.ToString();
                    Vy = vy * (ct * lx / 1000);             //Here my code do not recognize vy (local variable not attributed)
                    tb_vy.Text = Vy.ToString();
                }
                else
                {
                    vx = 5;vy = 2.5;
                    Vx = vx * (ct * lx / 1000);
                    tb_vx.Text = Vx.ToString();
                    Vy = vy * (ct * lx / 1000);
                    tb_vy.Text = Vy.ToString();
                }
 
Last edited by a moderator:
Not related to your problem, but hopefully this help make your code more readable:
C#:
double[,] RA =
{
    { 1.00, 2.50, 2.50 },
    { 1.05, 2.62, 2.50 },
    { 1.10, 2.73, 2.50 },
    { 1.15, 2.83, 2.50 },
    { 1.20, 2.92, 2.50 },
    { 1.25, 3.00, 2.50 },
    { 1.30, 3.08, 2.50 },
    { 1.35, 3.15, 2.50 },
    { 1.40, 3.21, 2.50 },
    { 1.45, 3.28, 2.50 },
    { 1.50, 3.33, 2.50 },
    { 1.55, 3.39, 2.50 },
    { 1.60, 3.44, 2.50 },
    { 1.65, 3.48, 2.50 },
    { 1.70, 3.53, 2.50 },
    { 1.75, 3.57, 2.50 },
    { 1.80, 3.61, 2.50 },
    { 1.85, 3.65, 2.50 },
    { 1.90, 3.68, 2.50 },
    { 1.95, 3.72, 2.50 },
    { 2.00, 3.75, 2.50 }
};
 
I´ll lsearch in the first column for a value equal or higher.
Unfortunately, you'll never match the RA[20, 0] = 2.00; when lambda == 2.00 because line 26 will be false, and therefore the code execution will jump into your else block.
 

Latest posts

Back
Top Bottom