简体   繁体   中英

Searching Algorithm C#

I have problem i want to use bool function to create searching algorithm this is my code

private bool IsEqual(int data1, int[] arr2){
     bool find=false;

        foreach(int data2 in arr2){
           if(data1==data2){
              find=true;
              break;
              }
     }

     return find;
}

and this is i call the function

int data1=2;
int[] arr={1,2,3,4,5};
if(IsEqual(data1, arr)){
    console.writeline("Find in index");
 }

how can i get the index of the array if the number is find?

Note:

  • I must use bool function

  • I can't add parameter in bool function

  • i can't add keyword in parameter like ref int data1

  • only change bool function

  • I can't add other function

You can return index . Return -1 if not found (instead of false)

You have to maintain count yourself, or switch to use a for(int i=... loop:

int i = 0;
foreach(int data2 in arr2) {
    if( data2 == data1 ) return i;
    i++;
}
return -1;

C# has a notion of ref arguments: https://msdn.microsoft.com/en-us/library/14akc2c7.aspx .

private bool IsEqual(ref int data1, int[] arr2) {
    bool find=false;
    for(int i = 0; i < arr2.Length; i++) {
        if(data1==arr2[i]) {
            find=true;
            data1 = i;
            break;
        }
    }
    if(!find) {
        data1 = -1;
    }
    return find;
}

and this can be called like so

int data1=2;
int[] arr={5,4,3,2,1};
if(IsEqual(ref data1, arr)){
    console.writeline("Find in index");
}
int index = data1;

Use an out parameter combined with a for loop. Play with it here . The out parameter lets you keep return a bool , and the for loop is built for keeping track of the array index.

using System;
public class Program
{
    // this is your method
    // using an out parameter and a for loop
    // ignore the static keyword - it isn't important for the example
    private static bool IsEqual(int data1, int[] arr2, out int index)
    {
        index = -1;
        bool find = false;
        for(int i = 0; i < arr2.Length; i++)
        {
            if (data1 == arr2[i])
            {
                find = true;
                index = i;
                break;
            }
        }
        // you don't need to return the index
        // the out parameter covers that for you
        return find;
    }

    public static void Main()
    {
        // and this is i call the function 
        int data1 = 2;
        int[] arr = { 1, 2, 3, 4, 5 };

        int index;
        if (IsEqual(data1, arr, out index))
        {
            Console.WriteLine("Find in index {0}", index);
        }
    }
}

OK, given your strange constraints, here's some code that finds the index of your int, "using" your IsEqual function:

int data1=2;
int[] arr={1,2,3,4,5};
for (int ix = 0; ix < arr.Length; ix++) {
    if(IsEqual(data1, new int[] { arr[ix] })) {
        Console.WriteLine("Find in index {0}", ix);
        break;
    }
}

It's pretty terrible code, but it does what you want..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM