Right im tring to figure this out.... using the form load event i have created an array which gets populated with 20 random numbers from 0-9 (numbers do repeat). Based on the users input I am now trying to search my array to findthe first occurrence if any of the number that the user entered. what method can i use to search through my array to check it?

any help is apprechiated

int[] yourarray;
            String guess = "4";

            for (int j=0; j < yourarray.Length;j++ )
            {
                if (yourarray[j].ToString()==guess)
                {
                    MessageBox.Show("Guessed!");
                    break;
                }
            }

Are you new to C#? This is a example but I think this more or less shows you how to do it.

Mark as solved please :)

Using a loop is a waste of time. Arrays contain multiple methods for searching built in:
BinarySearch()
Exists()
Find()
FindAll()
FindIndex()
FindLast()
FindLastIndex()
IndexOf()
LastIndexOf()

Comments
These are probably what you want (need to find which one though)

Yes im fairly new but would say i have a basic understanding i have built a program which generates 20 random integers from 0-9 on the form load and when the user types in a number between i need to find the first occurence of that number within the array but all i can do at the mo is print out how many times it appears eg if the number 7 appears 3 times within the 20 random numbers it prints out 777 but i need to display the position its at in the array or an error message saying not found! im close but not quite there

Using a loop is a waste of time. Arrays contain multiple methods for searching built in:
BinarySearch()
Exists()
Find()
FindAll()
FindIndex()
FindLast()
FindLastIndex()
IndexOf()
LastIndexOf()

There are 1000s of efficient more ways but the way I showed him is something standard in programming....

                                          Yes im fairly new but would say i have a basic understanding i have built a program which generates 20 random integers from 0-9 on the form load and when the user types in a number between i need to find the first occurence of that number within the array but all i can do at the mo is print out how many times it appears eg if the number 7 appears 3 times within the 20 random numbers it prints out 777 but i need to display the position its at in the array or an error message saying not found! im close but not quite there

Did you even try what I put? It does what you said; You just have to change a few things (if you want the guess to come from a input box) and add one little thing to the message box.

Edited 4 Years Ago by riahc3

This article has been dead for over six months. Start a new discussion instead.