Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am unable to print set no,rowno,columnno,from user in 3 dimentional array (c sharp)...PLZ HELP

What I have tried:

t[,,] arr1 =
{
{{1,2},
{4,3},
},
{{8,6},
{7,2 }},

{{11,9,},
{91,24 }},

};


Console.WriteLine("ENTER SET NO:");
int val=int.Parse(Console.ReadLine());


for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k=0; k<2;k++)
{
if (val == arr1[i, j, k])
{
Console.WriteLine("value i:" + i);
Console.WriteLine("Value of j:" + j);
Console.WriteLine("Value of k:" + k);

}
Console.Read();
}
}
}


}
}
}
vishnu satpathy 1 sec ago
    ReplyModify the comment. Delete the comment.
I need to show set No , row no and column no, from user
Example- set 1,rw1,col1 and it should show me arrays
Posted
Updated 24-Feb-22 20:14pm
v2
Comments
Patrice T 25-Feb-22 1:29am    
Show what you have done.
vishnu satpathy 25-Feb-22 1:59am    
int[,,] arr1 =
{
{{1,2},
{4,3},
},
{{8,6},
{7,2 }},

{{11,9,},
{91,24 }},

};


Console.WriteLine("ENTER SET NO:");
int val=int.Parse(Console.ReadLine());


for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k=0; k<2;k++)
{
if (val == arr1[i, j, k])
{
Console.WriteLine("value i:" + i);
Console.WriteLine("Value of j:" + j);
Console.WriteLine("Value of k:" + k);

}
Console.Read();
}
}
}


}
}
}
vishnu satpathy 25-Feb-22 2:02am    
I need to show set No , row no and column no, from user
Example- set 1,rw1,col1 and it should show me arrays

1 solution

If I load your code into VS and run it:
C#
int[,,] arr1 ={{{1,2},{4,3},},
               {{8,6},{7,2 }},
               {{11,9,},{91,24 }},};


Console.WriteLine("ENTER SET NO:");
int val = int.Parse(Console.ReadLine());


for (int i = 0; i < 2; i++)
    {
    for (int j = 0; j < 2; j++)
        {
        for (int k = 0; k < 2; k++)
            {
            if (val == arr1[i, j, k])
                {
                Console.WriteLine("value i:" + i);
                Console.WriteLine("Value of j:" + j);
                Console.WriteLine("Value of k:" + k);

                }
            }
        }
    }
(Reformatted for readability only)
I give it "3" and it correctly prints
ENTER SET NO:
3
value i:0
Value of j:1
Value of k:1
If I give it "2", it also correctly prints:
ENTER SET NO:
2
value i:0
Value of j:0
Value of k:1
value i:1
Value of j:1
Value of k:1
So what is the problem? Apart from it's a bit unreliable ...
I'd change it a little so that it's a bit more reliable:
int[,,] arr1 ={{{1, 2}, {4 , 3}},
               {{8, 6}, {7 , 2}},
               {{11,9}, {91,24}}};


Console.WriteLine("ENTER SET NO:");
int val = 3;


for (int i = 0; i < arr1.GetLength(0); i++)
    {
    for (int j = 0; j < arr1.GetLength(1); j++)
        {
        for (int k = 0; k < arr1.GetLength(2); k++)
            {
            if (val == arr1[i, j, k])
                {
                Console.WriteLine("value i:" + i);
                Console.WriteLine("Value of j:" + j);
                Console.WriteLine("Value of k:" + k);

                }
            }
        }
    }
As it's always a bad idea to use "magic numbers" in your code.
 
Share this answer
 
v2
Comments
CPallini 25-Feb-22 2:20am    
5.

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