Reputation: 11
If I enter the number 5, and it was at index 4 it will give me not found
the first 3 times, and then the index will be the same as the number I entered.
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
int myNumber;
int length = A.Length;
Console.WriteLine("enter your number");
myNumber = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < length; i++)
{
if (myNumber == A[i])
{
Console.WriteLine("the numer" + myNumber + "is present in the array at the index" +" "+ A[i]);
}
else
{
Console.WriteLine("the number you entered are not found");
}
Console.ReadKey();
}
Upvotes: 0
Views: 141
Reputation: 111820
The correct program:
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
int myNumber;
int length = A.Length;
Console.WriteLine("enter your number");
myNumber = Convert.ToInt32(Console.ReadLine());
// ADDED
bool found = false;
for (int i = 0; i < length; i++)
{
if (myNumber == A[i])
{
found = true; // ADDED
// On the far right of next row: Fixed A[i] -> i
Console.WriteLine("the numer" + myNumber + "is present in the array at the index" + " " + i);
break;
}
}
// ADDED
if (!found)
{
Console.WriteLine("the number you entered are not found");
}
Console.ReadKey();
I hope/think you can see/comprehend the differences without help.
I'll add that there is a second method to solve the problem:
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
int myNumber;
int length = A.Length;
Console.WriteLine("enter your number");
myNumber = Convert.ToInt32(Console.ReadLine());
// MOVED OUTSIDE FOR
int i = 0;
for (; i < length; i++)
{
if (myNumber == A[i])
{
// On the far right of next row: Fixed A[i] -> i
Console.WriteLine("the numer" + myNumber + "is present in the array at the index" + " " + i);
break;
}
}
// ADDED
if (i == length)
{
Console.WriteLine("the number you entered are not found");
}
Console.ReadKey();
See the difference: found
isn't necessary and we use only the i
variable, that has its "scope" expanded.
Upvotes: 1
Reputation: 9041
The Array class has a nice IndexOf method you can use. It'll return either the index location of the value you are looking for, or it'll return a -1 if the value is not found.
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
Console.WriteLine("enter your number");
int myNumber = Convert.ToInt32(Console.ReadLine());
int indexLocation = Array.IndexOf(A, myNumber);
if (indexLocation > -1)
{
Console.WriteLine("The number {0} was found at index location {1}", myNumber, indexLocation);
}
else
{
Console.WriteLine("The number {0} was not found", myNumber);
}
Upvotes: 5
Reputation: 21766
You want to change your code to something like this. Your first mistake was to print A[i] instead of i, your second mistake was to print the not found message inside the loop. If you move it outside the loop it will be printed only once, same applies to Console.ReadKey()
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
int myNumber;
int length = A.Length;
Console.WriteLine("enter your number");
myNumber = Convert.ToInt32(Console.ReadLine());
bool isFound = false;
for (int i = 0; i < length; i++)
{
if (myNumber == A[i])
{
Console.WriteLine("the numer" + myNumber + "is present in the array at the index" +" "+ i);
isFound = true;
}
}
if (!isFound)
{
Console.WriteLine("the number you entered are not found");
}
Console.ReadKey();
}
}
Upvotes: 0