Thread: confused with array

  1. #1
    cman
    Guest

    confused with array

    hi all, i need help with this problem, i seem to get compile errors as well as logic errors in my duplicate function, any help would be appreciated..


    Code:
    /* Use a single-subscripted array to solve the following problem
    ** Read in 20 numbers, each of which is between 10 and 100, inclusive.
    ** As each number is readm print it only if it is not a duplicate
    ** of a number already read. Provide for the "worst case" in which
    ** all 20 numbers are different. Use the smallest possible array to solve
    ** this problem.
    */
    
    #include <stdio.h>
    #define SIZE 20
    
    int duplicate( int num[] );
    
    int main()
    {
    	int i, numbers[ SIZE ];
    
    	printf( "Enter 20 numbers between 10 and 100: " );
    	
    	for ( i = 0; i <= SIZE - 1 && scanf( "%d", &numbers[i] ) == 1; i++ )
    		duplicate( numbers[ i ] );
    
    }
    
    int duplicate( int num[] )
    {
    	int i, hold;
    	
    	for ( i = 0; i <= SIZE - 1; i++ )
    		if ( num[i] == num[i+1] ) {
    			hold = num[i];
    		else 
    			hold = num[i+1];
    	}
    	printf( "%3d\n", num[ i ] );
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    try
    make the array of size[20]
    then increment
    (i=0;i<20;1++)
    then if num from scanf=i
    i=hold
    else
    printf

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Originally posted by ddavidson
    try
    make the array of size[20]
    then increment
    (i=0;i<20;1++)
    then if num from scanf=i
    i=hold
    else
    printf
    (i=0;i<20;1++)?
    Maybe i=0; i<20; i++

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    you have defined duplicate as returning int yet you return nothing, so change int to void. also, the for loop within the duplicate function is dereferencing 0 through SIZE of the num array, but you've only initialized 0 through i (from the loop in main()).

    what you should do is pass the current index (i) to duplicate() and compare all values below the current index with the value dereferenced by the current index.

    example:
    Code:
    for(i = 0; i < cur; i++)
       if(num[i] == num[cur])
          /*handle duplicate*/

  5. #5
    cman
    Guest
    hi, thanks for your thoughts. ive tried implementing it a few different ways but i dont seem to be getting anywhere. My logic is that if ( array[0] == array[1] ) then return the array[0] and recursively use the function duplicate on the rest of the array.


    for ( i = 0; i <= SIZE - 1 && scanf( "%d", &numbers[] ) == 1; i++ )
    is what ive done here correct? will this store all the numbers on input into an array called numbers? or have i done something wrong.

    Thanks!

    Code:
    #include <stdio.h>
    #define SIZE 20
    
    void duplicate( int [] );
    
    int main()
    {
    	int i, numbers[ SIZE ];
    
    	printf( "Enter 20 numbers between 10 and 100: " );
    	
    	for ( i = 0; i <= SIZE - 1 && scanf( "%d", &numbers[] ) == 1; i++ )
    		duplicate( numbers );
    
    	printf( "%3d\n", num[ i ] );
    }
    
    void duplicate( int num[] )
    {
    	int i, hold;
    	
    	for ( i = 0; i <= SIZE - 1; i++ ) {
    		if ( num[i] == num[i+1] ) {
    			return num[i];
    			duplicate( num[i+1] );
    		}
    		else
    			duplicate( num[i+1] );		
    	
    	
    	}
    }

  6. #6
    Unreg1
    Guest
    >>Use the smallest possible array
    In this case, the smallest array would be an array of unsigned char, size 12, totalling 12 bytes (I'm assuming a byte has 8 bits here). In your code you have 20 int's which is 20*sizeof(int) in size.

    You'd use the unsigned char array as a bit map, where a bit set to 1 denotes a number has been used, 0 not used.

    This maybe a bit advanced, but is something you might want to consider for next time.

    Anyway, in your current code:
    >for ( i = 0; i <= SIZE - 1; i++ ) {
    > if ( num[i] == num[i+1] ) {
    This is wrong, as when you get to the last loop, you'll be comparing the last element (i) with memory that is outside of the array bounds (i+1)

    There's no need to use recursion either. Have a think about this:

    - Declare an array of ints, size 20
    - Create a var called NumInArray and set it to 0
    - When you've read an int from the user, pass it to the dup function which does the following:
    - Loops from array[0] to array[NumInArray-1] checking each element against the value from the user. Obviously when working with the first number, no comparison occurs.
    - If a match is found, print "This is a dup" and the return to main.
    - If no match found, set array[NumInArray] to the new number, and increment NumInArray by 1.

    This isn't the most efficient way of doing things, but should get the job done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Array help
    By deedlit in forum C Programming
    Replies: 4
    Last Post: 11-05-2003, 10:55 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM