Search number in a 3-dim array

In the following example, the user will declare and intialize a 4,3,2 array and search a  number in that array:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,,] myArray= {
                              {
                                  {1,2}, 
                                  {3,4}, 
                                  {5,6}
                               },
                              {
                                  {7,8 },
                                  {9,10 },
                                  {11,12 }
                              },
                              {
                                  {13,14 },
                                  {15,16 },
                                  {17,18 }
                              },
                              {
                                  {19,20 },
                                  {21,22 },
                                  {23,24 }
                              }
                            };

            //   Console.Write("{0},{1},{2}", myArray.GetLength(0), myArray.GetLength(1), myArray.GetLength(2));
            Console.Write("Enter a number to search in array: ");
            int num=Convert.ToInt32(Console.ReadLine());
            for(int i=0;i<myArray.GetLength(0); i++)
            {
                for(int j=0; j<myArray.GetLength(1); j++)
                {
                    for(int k=0; k<myArray.GetLength(2); k++)
                    {
                        if(myArray[i,j,k] == num)
                        {
                            Console.WriteLine("Number found at {0},{1},{2}", i, j, k);
                            break;
                        }

                    }
                }
            }

        }
    }
}

 

Add a Comment

Your email address will not be published. Required fields are marked *