comparing a number to an array

This is a discussion on comparing a number to an array within the C Programming forums, part of the General Programming Boards category; I am trying to see if the entered number is unique (if it hasnt been entered already) and i dont ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    (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. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by goran00 View Post
    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. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by goran00 View Post
    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
    Quote Originally Posted by goran00 View Post
    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. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    is there anyone here that can help me with this?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    (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. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2009, 04:17 PM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. finding the element number in an array
    By tommy69 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 03:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21