# Thread: The simplest of probelms..any ideas ?

1. ## The simplest of probelms..any ideas ?

Ok ...now I am a complete rookie to programming...I am doing a college project.No you dont have to do it for me....I so love coding ever since I started that I would do it myself even if its gonna take me 12 hrs today...

I however need some help...in the project...I am in need of comparing an array to an Integer.

for example an array b = [407] to an integer variable xyz

So how do I convert the above array elements into one integer as in "int a = 407" that way i figures i could then just compare 407 to xyz using library functions.(I do not know what XYZ is untill user inputs it)

If there is any other way to do this...pls free to enlighten me....

Greatly appreciate any help

2. Your confusion probably is related to the fact you don't understand the problem. Comparing an array to an integer is rather abstract. To some people it could mean you just want to see if the integer is an element of said array. Is this the case? Then you need a simple loop to check each element.

Remember, an array's elements are simply the type that the array is declared with. That means if you have an array declared like this:

Code:
`int array[100];`
That means array[0] is just an int, literally, like any other int.

3. Thanks MacGyver.....i think I am having difficulties in expressing what my problem is..

I am not attempting to compare an array to an integer like one compares one integer with another integer....

Let me try and explain again....

1)I have an integer array with 3 integer elements in it.Namely [4 0 7]

2)Later in my code i have an integer variable xyz = 751

3)Now i need to see if the array elements all put together...i.e the number 407 is equal to 751 or not.If i were to store user input (i.e the no 407) in an integer variable say "int a" then i wouldnt have problems comparing "int a" to "int xyz" But that I cannot do...the user input has to be stored in an array and then I need to compare the number in array to the variable XYZ...

4)Its like the 0th element ,the 1st elment and the 2nd element of my array (in this case 407 respectively) together is a number 407...now compare this to any other variable

Thanks again to all for any assistance

4. You have two options.

1. Do it the math way of dividing by 10, 100, etc. on the int in question and disect it digit by digit, comparing it with the array.
2. Convert the int to a string via sprintf(), snprintf(), itoa(), or some other means, and then converting (by subtracting '0' from the char) and comparing each character to each digit of the array.

I would go with the last option if you want to be able to handle this gracefully and yet compare an arbitrary number of digits. If you know the digits will be fixed at 3, let's say, then the first option might be more appealing since you can hardcode the division.

5. You can have a simple loop like this one to transform the array into the integer you are looking for this way:

Code:
```for all elements of the array starting at 0
new_integer *= 10
new_integer += element i of array```
Then you can compare new_integer with xyz.

6. hi!
i was given a similar problem at my college and
the method i followed was as follows
( It is the opposite Macgyver's 1st method multiplying instead of dividing,
similar to desolation's method)
Use the following loop

Code:
```integer=0;
for(i=0;i<n;i++) //n is the number of elements in the array 'a'
{
a[i]*=pow(10,n-1-i);
integer+=a[i];
}```
Compare integer with your required value

7. wow thanks guys.....I have come to simply love coding beacause of the various ways in which a single problem can be solved...

Once again thanks for the replies..