Jamie Dalley

Jamie Dalley

  • NA
  • 16
  • 5.5k

Why doesn't this array work properly?

Jan 30 2013 7:05 AM
Basically this bit of code should generate a random array and then ask for the user to input a number, then check if the number is in the array and say where it is, otherwise it should say the number isn't in the array. For some reason sometimes it says the number isn't in the array when it is. The code is for my uni assignment so thats why there are other things too. thanks in advance, here is the code:

     int[] firstArray = new int[10];
            for (int i = 0; i < 10; i++)
            {
                RandomNumber = MyRandom.Next(MaxValue);
                firstArray[i] = RandomNumber;
            }
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Item {0} in the array is {1}", i, firstArray[i]);
            }
            //8
            Console.WriteLine("Press any key to see question 8");
            Console.ReadKey();
            Console.WriteLine("The lowest number in the array is {0}", firstArray.Min());
            //9
            Console.WriteLine("Press any key to see question 9");
            Console.ReadKey();
            int j;
            Console.Write("Enter number to search for : ");
            j = int.Parse(Console.ReadLine());
            int index = linear_search(j, firstArray);
            if (index > 0)
            {
                Console.WriteLine("{0} is found at index {1} in the array", j, index);
            }
            else
            {
                Console.WriteLine("{0} is not found in the array", j);
            }

Answers (2)