-6

This requires splitting the array which contains age and name of the person, and then performing search operation on the name variable and finally print the age if the name is present in the array.

So, far I have only created a 2D array but don't know how to split and then perform the search

String[,] arr = { { "aakif", "25" }, {"ali", "31"} , {"ben","35"}, {"hassnain" ,"45" } };
19
  • In this case of situation you're better off with a dictionary (key,value) Commented Oct 6, 2019 at 18:34
  • I have to use a 2D array, its part of an assignment task. Commented Oct 6, 2019 at 18:35
  • @Melchia I'm getting the feeling that ali_25 wants to learn how to do a binary search, and not just use a pre-packaged solution like Dictionary. Commented Oct 6, 2019 at 18:36
  • 3
    Let's see what you tried. Especially where you keep track of the start and end of the range that you are searching. At some point you will calculate the midpoint between start and end and then update either start or end and assign it the value of midpoint to continue searching in either the first half or second half of the range. Commented Oct 6, 2019 at 18:37
  • 1
    And asking here is not the right course of action, there are other sites better suited for these kinds of questions. Commented Oct 6, 2019 at 18:54

3 Answers 3

1
using System;

namespace ConsoleApp
{

  static partial class Program
  {
    static void Main(string[] args)
    {
      Test();
      Console.WriteLine();
      Console.WriteLine("End.");
      Console.ReadKey();
    }
    static void Test()
    {
      string[,] arr = 
      { 
        { "aakif", "25" }, 
        { "ali", "31" }, 
        { "ben", "35" }, 
        { "hassnain", "45" }
      };
      string search = "ali";
      string age = arr.GetAge(search);
      if ( age != null )
        Console.WriteLine($"{search} age = {age}");
      else
        Console.WriteLine($"{search} not found");
    }
    static string GetAge(this string[,] array, string name)
    {
      for ( int index = array.GetLowerBound(0); index <= array.GetUpperBound(0); index++ )
        if ( array[index, array.GetLowerBound(1)] == name )
          return array[index, array.GetUpperBound(1)];
      return null;
    }
  }

}

Output:

ali age = 31
Sign up to request clarification or add additional context in comments.

Comments

0

I do disagree with the way you store your input but you can achieve your search with the following:

String[,] arr = new string[2,4];
arr[0, 0] = "saif";
arr[0, 1] = "25";
arr[0, 2] = "ali";
arr[0, 3] = "17";
arr[1, 0] = "aakif";
arr[1, 1] = "11";
arr[1, 2] = "hassnain";
arr[1, 3] = "50";

int index = -1;
int jindex = -1;
for ( int i =0 ; i <arr.GetLength(0) ; i++) {
    for ( int j =0 ; j <arr.GetLength(1) ; j++) {
        if (arr[i,j]== "ali")   {
            index = i;
            jindex = j;
            break;
        }

    }
}

if ( index != -1) {
    Console.WriteLine(arr[index,jindex] + " " + arr[index,jindex +1 ]);
}
else Console.WriteLine("Not Found");

1 Comment

thanks a lot. I have corrected the input storage way.
0

The first index of the index [0,0], [1,0] if the name you are looking for. [Index, 1] and print the age value.

string[,] arr = { { "aakif", "25" }, { "ali", "31" }, { "ben", "35" }, { "hassnain", "45" } };
        string search = "ali";
        for (int i = 0; i < arr.GetLength(0); i++)
        {
            if (arr[i,0].ToString() == search)
            {
                Console.WriteLine(arr[i, 0] + " " + arr[i, 1] + " years old");
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.