# Thread: comparing a number to an array

1. ## comparing a number to an array

I am trying to see if the entered number is unique (if it hasnt been entered already) and i dont seem to be able to get it right.
What am I doing wrong?
Code:
```#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

#define SIZE 5

//prototype
int isValid(input);
int isUnique(input);

main(){

int values [SIZE]={0};
int count;
int i;
int valid;
int result;
int validSoFar;
int unique;
int uniqueSoFar;
count=0;
result=0;
i=0;
validSoFar=0;
uniqueSoFar=0;

for (i=0; i<SIZE; i++){
printf("Enter an integer (50-100):\n");
scanf("%i",&values[i]);
valid =isValid(values[i]);
unique=isUnique(values[i]);

if (valid ==FALSE){
i--;}

validSoFar+=valid;
uniqueSoFar+=unique;

printf("so far valid %i\n",validSoFar);
printf("Unique so far %i\n",uniqueSoFar);
}

system("pause");
}

int isValid(int input){
int result;
int i;
if (input>=50 && input <=100)
result= TRUE;

else
result= FALSE;
return result;
}

int isUnique (int inputData[], int elements){
int result;
int i;
if (elements==inputData)
result=FALSE;
else
result=TRUE;

return result;
}```

2. (1) Your function says it requires an integer array and an integer, but you pass it just an integer.

(2) This:
Code:
`if (elements==inputData)`
can't possibly mean anything, since elements is an integer and inputData is an array.

3. what i trid to do there is have the function take the input number which i called element and I am trying to see if that number is equal to any number in the array.
How can i fix this? This is my first time taking c and I am having a real hard time with it.
thanks for help

4. i tried to fix it but i am stuck. pls help
Code:
```#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

#define SIZE 5

//prototype
int isValid(input);
int isUnique(input);

main(){

int values [SIZE]={0};
int count;
int i;
int valid;
int result;
int validSoFar;
int unique;
int uniqueSoFar;
int number;
count=0;
result=0;
i=0;
validSoFar=0;
uniqueSoFar=0;

for (i=0; i<SIZE; i++){
printf("Enter an integer (50-100):\n");
scanf("&#37;i",&values[i]);
number=values[i];
valid =isValid(values,number);

unique=isUnique(values[i]);

//if (valid ==FALSE){
//	i--;}

validSoFar+=valid;
uniqueSoFar+=unique;

printf("so far valid %i\n",validSoFar);
printf("Unique so far %i\n",uniqueSoFar);
}

system("pause");
}

int isValid(int input){
int result;
int i;
if (input>=50 && input <=100)
result= TRUE;

else
result= FALSE;
return result;
}

int isUnique (int values[], int number){
int result;
int i;
for(i=0; i<10;i++){
if (number==values[i])
result=FALSE;
else
result=TRUE;}

return result;
}```

5. Originally Posted by goran00
what i trid to do there is have the function take the input number which i called element and I am trying to see if that number is equal to any number in the array.
I think if you had actually tried to do that, you would have gotten closer -- for one thing, you would actually be comparing element to actual numbers in the array, instead of ... whatever it is you've got there now. You might also be passing the array into the function instead of not, which makes it hard to do anything with it.

And as a matter of design, this design is doomed to failure because -- you put the number straight into the array when you read it in. So you're going to have to keep a temporary variable around to put the number in when it is read in; only if it passes the Valid and Unique tests should you put it in the array.

6. but i thought that once i am in the array is when the uniquesness is tested, not before.
thats what i was trying to do.
how would i go about that
basically i am trying to input the number and the array into the function and have the function figure out if it is unique or not.
how would i do that

7. Originally Posted by goran00
but i thought that once i am in the array is when the uniquesness is tested, not before.
thats what i was trying to do.
how would i go about that
basically i am trying to input the number and the array into the function and have the function figure out if it is unique or not.
how would i do that
Originally Posted by goran00
what i trid to do there is have the function take the input number which i called element and I am trying to see if that number is equal to any number in the array.
That's how you test a number for uniqueness -- you just have to actually do it. You know, check whether element != inputdata[i] for every i. You can't compare a number to an array all at once, and you have to be careful that if you find an equality you keep hold of it -- any equality, anywhere, and your number's not unique.

But you can't put the number in the array first, or else you will find a match, and so you will have no uniqueness ever.

8. i understand that but how would i go about doing that? I have absolutely no idea. I have been stuck on the same thing for two weeks and I dont seem to go anywehre

9. here is what i did with the uniqueness function, but there seems to be a problem on the first line. what is it? it looks right to me
Code:
```int isUnique(int inputData[],int SIZE,int number)
{
int result;
for(i=0; i<SIZE;i++){
if (number=values[i])
result=FALSE;
else
result=TRUE;
return result;}

}```

10. is there anyone here that can help me with this?

11. 1. The variable named i was not declared.

2. number=values[i] is wrong (hint: you want to compare, not assign).

3. Your loop logic is wrong. Remember, the number is unique if and only if after going through the loop, you did not find a match. The moment you find a match, you know that the number is not unique.

12. (1) Are you passing in three things to your function? No? Maybe you should, then.

(2) You don't want your return statement inside your for loop -- it will return (hence breaking the function) after the first go-round.

(3) And you still need to make sure that a result=TRUE later doesn't overwrite a result=FALSE earlier.

13. i am having a hard time understanding the whole seperate function thing and I do understand the things that I need to fix, but I don't know how to fix.
how do i compare a number to values in the array?
how do i make the loop stop when it finds a match?
how do i return the result?
i had no class in how to do this kind of thing so i have no idea what i am doing. I have a simple example (square root of a number) and that is the only thing that I know about function.
can anyone help me any more
thank you

14. how do i compare a number to values in the array?
In this case it would be: number == values[i]

how do i make the loop stop when it finds a match?
You can break from the loop, return from the function, or base the loop condition on a flag (i.e., some boolean variable) that is set when a match is found.

how do i return the result?
With a return statement, like what you have done.

15. number == values[i]
- i have tried doing that but it gives me error "="
You can break from the loop, return from the function, or base the loop condition on a flag (i.e., some boolean variable) that is set when a match is found.
- i have no idea how to do that