-3

I need to implement a Search button that will compare the value from my TextBox with the values in the arrays and if they match, it returns the index, otherwise it will return -1. I implemented the random arrays of double and int already as well as all the interface and buttons. It is a must to use IComparable and CompareTo() but I have no idea on how to implement it. I tried implementing a Search method, but it is not working, I don't know how to call it in the SearchButton_Click event handler.

That's what I have so far:

WPF APP

public partial class MainWindow : Window 
{
    int[] numb = new int[6];
    double[] numb2 = new double[6];

    public MainWindow () 
    {
        InitializeComponent ();
    }

    //Create Int
    private void Button_Click (object sender, RoutedEventArgs e) 
    {
        resultsBox.Items.Clear ();
        resultsBox.Items.Add ("Index Value\n");
        Random rnd = new Random ();
        for (int i = 0; i < 6; i++) {
            numb[i] = rnd.Next (0, 999);
            string m = i.ToString () + "\t" + numb[i].ToString ();
            resultsBox.Items.Add (m);
        }
    }

    //Create Double
    private void CreateDouble_Click (object sender, RoutedEventArgs e) 
    {
        resultsBox.Items.Clear ();
        resultsBox.Items.Add ("Index Value\n");
        Random rnd = new Random ();
        for (int i = 0; i < 6; i++) {
            numb2[i] = Math.Round (rnd.NextDouble () * (999), 2);
            string m = i.ToString () + "\t" + numb2[i].ToString ();
            resultsBox.Items.Add (m);
        }
    }

    //Search
    private void SearchButton_Click (object sender, RoutedEventArgs e) { }

    static int Search<T> (T[] dataArray, T searchKey) where T : IComparable<T> 
    {
        //Iterate through the array.
        for (int iter = 0; iter < dataArray.Length; iter++) {
            //Check if the element is present in the array.
            if (dataArray[iter].CompareTo (searchKey) == 0) {
                //Return the index if the element is present in the array.
                return iter;
            }
        }
        //Otherwise return the index -1.
        return -1;
    }
}

Thank you!

BionicCode
  • 1
  • 4
  • 28
  • 44
  • It's unclear in what you wana search? int and double already implements `IComparable` and `IComparable` ... also there is alrady `Array.IndexOf` defined – Selvin Mar 30 '20 at 20:48
  • I want to type something in the search box and search in the arrays of double or int, if what is inside the search box matches the numbers that are being displayed, I have to change the label content to the index of the arrays, otherwise it has to return -1. – Victor Dallarosa Mar 30 '20 at 20:56
  • Write a generic method, Search, that searches an array using linear search algorithm. Method Search should compare the search key with each element in its array parameter until the search key is found or until the end of the array is searched. If the search key is found, return its location in the array; otherwise return -1. Write a WPF app that inputs and searches an int array and a double array. Provide buttons that the user can click to randomly generate int and double values. – Victor Dallarosa Mar 30 '20 at 20:59
  • Display the generated values so that the user knows what values he/she can search for [Hint: use (T: IComparable) in the where clause for method Search so that you can use method CompareTo() to compare the search key to the elements in the array] – Victor Dallarosa Mar 30 '20 at 20:59

1 Answers1

0

First you have to give the TextBox a name, so that you can reference it in your code-behind, in order to retrieve the search key. Then on button clicked, you have to check the numeric type of the search key, to identify the search target array. Then pass both arguments to the Search() method:

MainWindow.xaml

<Window>
  ...

  <TextBox x:Name="SearchInputBox` />
  ...
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window 
{
  private readonly int arraySize = 6;
  private int[] IntegerValues { get; }
  private double[] DoubleValues { get; }

  public MainWindow() 
  {
    InitializeComponent();
    this.IntegerValues = new int[arraySize];
    this.DoubleValues = new double[arraySize];
  }

  // Create Int
  private void Button_Click(object sender, RoutedEventArgs e) 
  {
    resultsBox.Items.Clear();
    resultsBox.Items.Add("Index Value\n");

    Random rnd = new Random();
    for (int index = 0; index < arraySize; index++) 
    {
      this.IntegerValues[index] = rnd.Next(0, 999);
      string item = index.ToString() + "\t" + this.IntegerValues[index].ToString();
      resultsBox.Items.Add(item);
    }
  }

  // Create Double
  private void CreateDouble_Click(object sender, RoutedEventArgs e) 
  {
    resultsBox.Items.Clear();
    resultsBox.Items.Add("Index Value\n");

    Random rnd = new Random();
    for (int index = 0; index < arraySize; index++) 
    {
      this.DoubleValues[index] = Math.Round(rnd.NextDouble() * 999, 2);
      string item = index.ToString() + "\t" + this.DoubleValues[index].ToString();
      resultsBox.Items.Add(item);
    }
  }

  // Handle search button clicked
  private void SearchButton_Click(object sender, RoutedEventArgs e) 
  {
    string numericString = this.SearchInputBox.Text;

    int resultIndex = -1;
    if (int.TryParse(numericString, out int integerSearchPredicate)
    {
      resultIndex = MainWindow.Search(this.IntegerValues, integerSearchPredicate);
    }
    else if (double.TryParse(numericString, out int doubleSearchPredicate)
    {
      resultIndex = MainWindow.Search(this.DoubleValues, doubleSearchPredicate);
    }
  }

  static int Search<T>(T[] dataArray, T searchKey) where T : IComparable<T> 
  {
    // Iterate over the array.
    for (int index = 0; index < dataArray.Length; index++) 
    {
      // Check if the element is present in the array.
      if (dataArray[index].CompareTo(searchKey) == 0) 
      {
        //Return the index if the element is present in the array.
        return index;
      }
    }

    // Otherwise return the index -1.
    return -1;
  }
}
BionicCode
  • 1
  • 4
  • 28
  • 44