Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing an int value to an array, then display if value matches or does not match

Tags:

c#

I am brand new to programming and was interested in C#. I am studying arrays and have to compare my variable (checkNum) to my array(myNums[10]). I read posts here and several other sites and saw how to compare but getting stuck on how to properly display the comparison as shown in my attempt with the if/else statement below:(I will continue to research, but would appreciate and nudges in the right direction. Not necessarily the answer as I am wanting to learn) :)

Here is my code:

int[] myNums = new int[10];
int checkNum;
Console.WriteLine("Enter 10 numbers:");

for (int i = 0; i < 10; i++)
{
    Console.Write("Number {0}: ", i + 1);
    myNums[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("You entered:");
foreach (int x in myNums)
{
    Console.Write("{0} ", x);
}
Console.ReadLine();

Console.WriteLine("Enter another number:");
checkNum = int.Parse(Console.ReadLine());
bool exists = myNums.Contains(checkNum);

if (checkNum == myNums[10])
{
    Console.WriteLine("Your number {0} is in the Array.", checkNum);
}
else
{
    Console.WriteLine(
        "Your number {0} does not match any number in the Array.",
        checkNum);
}
Console.ReadLine();
like image 983
Darwin Avatar asked Dec 30 '11 21:12

Darwin


People also ask

How do you compare integers in an array?

A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays. equals() to compare two arrays. Actually, there is a list of equals() methods in the Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is the base of all classes in Java).

How do you compare values in an array?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.

How do you check if an array contains the same value?

To check if all values in an array are equal:Use the Array. every() method to iterate over the array. Check if each array element is equal to the first one. The every method only returns true if the condition is met for all array elements.

How do you compare two elements in the same array?

equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.


1 Answers

bool exists = myNums.Contains( checkNum );
if( checkNum == myNums[10] )
{
    Console.WriteLine( "Your number {0} is in the Array.", checkNum );
}
else
{
    Console.WriteLine( "Your number {0} does not match any number in the Array.", checkNum );
}

Should be

bool exists = myNums.Contains( checkNum );
// or simply if(myNums.Contains(checkNum)) as you don't use the variable again
if( exists )
{
    Console.WriteLine( "Your number {0} is in the Array.", checkNum );
}
else
{
    Console.WriteLine( "Your number {0} does not match any number in the Array.", checkNum );
}

You perform the check correctly, but you don't use the result (exists) and simply (attempt to) compare the new number to the last element in the array. Of course, at this point your program just crashes because you have overrun the bounds of your array.

Arrays are 0 indexed, i.e., nums[10] contains indices 0-9.

like image 75
Ed S. Avatar answered Sep 19 '22 13:09

Ed S.