Thread: Uniqueness

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

    Uniqueness

    i am trying to figure out if the number entered are unique (they havent been entered yet).
    this is what i have so far
    where do i go from here
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TRUE 1
    #define FALSE 0
    
    #define SIZE 5
    
    //prototype
    int isValid(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);
    	
    		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 elements){
    	int result;
    	int i;
    	for (i=0; i<5; i++){
    		if (elements==values[i])
    			result=FALSE;
    		else
    			result=TRUE;
    	}
    	return result;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Ask a specific question perhaps?

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i put in the prototype and now i have the following.
    when the program runs and i put the same number in twice it still says that its unique.
    how do i fix that
    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("&#37;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 values[], int elements){
    	int result;
    	int i;
    	for (i=0; i<5; i++){
    		if (elements==values)
    			result=FALSE;
    		else
    			result=TRUE;
    	}
    	return result;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This program shouldn't compile, at least not without a pile of warnings:
    Code:
    if (elements==values)
    doesn't do what you think it does; and in fact nothing in your loop depends on i so I would bet this loop gets optimized away.

    But even after that, your function should be called CompareToZero, since that's what it does. Trace it out on paper, and you should get an idea of what your function does and what it's supposed to do.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Consider your for loop --- what is the value of "i" when you call the isUnique() function?

    And in your isUnique() function, you are not checking the entire values array are you (if elements == values)? Don't you want a subscript.?

    Take another look at how you pass arrays to a function. That may help you clear this up.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    Unhandled exception at 0x00413156 in Assignment 6.exe: 0xC0000005: Access violation reading location 0x00000043.
    what does that mean
    this is the error i keep getting and here is the updated code
    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("&#37;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 values[], int elements){
    	int result;
    	int i;
    	for (i=0; i<SIZE; i++){
    		if (elements==values[i])
    			result=FALSE;
    		else
    			result=TRUE;
    	}
    	return result;
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This code could not compile.
    Compiling: C:\Documents and Settings\Andrew\Desktop\temp.c
    C:\Documents and Settings\Andrew\Desktop\temp.c:10: error: `input' was not declared in this scope
    C:\Documents and Settings\Andrew\Desktop\temp.c:11: error: `input' was not declared in this scope
    C:\Documents and Settings\Andrew\Desktop\temp.c:13: error: ISO C++ forbids declaration of `main' with no type
    C:\Documents and Settings\Andrew\Desktop\temp.c: In function `int main()':
    C:\Documents and Settings\Andrew\Desktop\temp.c:32: error: `isValid' cannot be used as a function
    C:\Documents and Settings\Andrew\Desktop\temp.c:33: error: `isUnique' cannot be used as a function
    C:\Documents and Settings\Andrew\Desktop\temp.c: In function `int isValid(int)':
    C:\Documents and Settings\Andrew\Desktop\temp.c:49: error: `int isValid(int)' redeclared as different kind of symbol
    C:\Documents and Settings\Andrew\Desktop\temp.c:10: error: previous declaration of `int isValid'
    C:\Documents and Settings\Andrew\Desktop\temp.c:10: error: previous non-function declaration `int isValid'
    C:\Documents and Settings\Andrew\Desktop\temp.c:49: error: conflicts with function declaration `int isValid(int)'
    C:\Documents and Settings\Andrew\Desktop\temp.c:51: warning: unused variable 'i'
    C:\Documents and Settings\Andrew\Desktop\temp.c: In function `int isUnique(int*, int)':
    C:\Documents and Settings\Andrew\Desktop\temp.c:60: error: `int isUnique(int*, int)' redeclared as different kind of symbol
    C:\Documents and Settings\Andrew\Desktop\temp.c:11: error: previous declaration of `int isUnique'
    C:\Documents and Settings\Andrew\Desktop\temp.c:11: error: previous non-function declaration `int isUnique'
    C:\Documents and Settings\Andrew\Desktop\temp.c:60: error: conflicts with function declaration `int isUnique(int*, int)'
    Process terminated with status 1 (0 minutes, 1 seconds)
    13 errors, 1 warnings
    Without knowing what your code actually is, I can't say how you've walked off the end of your array.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i did a couple of changes but it didnt help.
    here is the new code
    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("&#37;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;
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're fiddling. Stop. Walk around the building three times. Write a flowchart/pseudocode, as you prefer. When you're done with that, then you can look at your code again.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read the errors. Some of them are very descriptive.
    And there is plenty documentation of compile errors, as well. Have a look at some documentation. It will all become a lot clearer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >int isValid(input);
    >int isUnique(input);

    The compiler needs to know what "input" is... as far as it knows it is garbage. A function prototpye ideally should read:

    Code:
    int isValid ( /*data type here*/);
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. check for uniqueness in arrya
    By ronenk in forum C Programming
    Replies: 3
    Last Post: 10-01-2004, 01:37 AM
  2. Array Check for uniqueness...Need Help FAST!!
    By 67stangman in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2002, 12:17 PM