Basically I am working on my assignment and i need to make my code read a number then check to see if the number is in an array I created. This is the code at the moment starting from where i create the array
Console.WriteLine("Press enter to see question 7");
Console.ReadKey();
int MaxValue;
int RandomNumber;
Random MyRandom;
MaxValue = 20;
MyRandom = new Random();
RandomNumber = MyRandom.Next(MaxValue);
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 enter to see question 8");
Console.ReadKey();
Console.WriteLine("The lowest number in the array is {0}", firstArray.Min());
//9
int j;
for (j = 0; j < 20; j++)
{
if (linear_search(j, 7, firstArray))
System.Console.WriteLine(j + "if found in the array");
else
System.Console.WriteLine(j + "is not found in the array");
}
The problem I have at the moment is it says "the name "linear_search" does not exist in the current context". Also I know there are probably problems with the code anyway, since i'm not really sure how to use linear searches and can't find anywhere that explains it properly. I know the code at the moment isn't asking for input, and is just looking for 7 in the code, but that's just for while i make it work.
Loading
VulpesPosted Jan 23, 2013, 9:54 AM
If the item is found you can return its index in the collection. If it's not found you can return some special number (-1 is usually used, as indices start at 0) to the calling code.
Most of your program is fine. I've filled in some code for the linear_search method and also added a few lines to obtain the number to search for in the array:
using System;
using System.Linq;
class Test
{
static void Main()
{
Console.WriteLine("Press enter to see question 7");
Console.ReadKey();
int MaxValue;
int RandomNumber;
Random MyRandom;
MaxValue = 20;
MyRandom = new Random();
RandomNumber = MyRandom.Next(MaxValue);
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 enter to see question 8");
Console.ReadKey();
Console.WriteLine("The lowest number in the array is {0}", firstArray.Min());
//9
Console.WriteLine("Press enter 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);
}
Console.WriteLine("Press any key to end program");
Console.ReadKey();
}
static int linear_search(int search, int[] arr)
{
int index = -1;
for(int i = 0; i < arr.Length; i++)
{
if(arr[i] == search)
{
index = i;
break;
}
}
return index;
}
}
VulpesPosted Jan 23, 2013, 10:54 AM
Jamie DalleyPosted Jan 23, 2013, 10:51 AM