1

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

8
  • 2
    I don't know if you're doing this for learning or what, but you could just use Array.IndexOf Commented Mar 3, 2015 at 3:41
  • I use this algorithm for my project this is not my real project just examples Commented Mar 3, 2015 at 3:45
  • I'm afraid you haven't given us enough information about the constraints. It sounds like your professor has made the IsEqual method a black box. You can only return bool and you aren't allowed to use an out parameter. What are you allowed to do? Commented Mar 3, 2015 at 4:01
  • that's makes me confused Commented Mar 3, 2015 at 4:05
  • If you have to find the index and must use only the functions provided, and aren't allowed to search for yourself, what about passing in successively larger sub-arrays of the array? When it goes from false to true, you have your index. Commented Mar 3, 2015 at 5:10

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

but my lecturer told me I must return bool not int
After IsEqual returns true, you may call another function that returns int.
1

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;

Comments

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;

3 Comments

but my lecturer told me I can't add parameter in bool function
if you can't add a parameter to the bool function, are you allowed to pass one of the arguments by reference? I've edited the code sample to demonstrate
I must use bool IsEqual(int data1, int[] arr2) I can't add another keyword in parameter function
0

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);
        }
    }
}

1 Comment

but my lecturer told me I can't add parameter in bool function
0

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..

3 Comments

I'm sorry but i only must change the bool function not the main
Well, what does your professor expect you to do with the index then? You can't return it, are you supposed to just print it from inside the function?
You can't. Index is an integer. You are returning a bool. You could save it to a global variable, but I really hope that's not what your professor intended (though if he wrote IsEqual, maybe he does).

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.