Thread: check for uniqueness in arrya

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    Check for uniqueness in array

    I Have 2 one dimensional arrays. One array is catalogue no.
    (A) the other is quantity (D). I need to sum up all elements from the same catalogue no.
    This is what I wrote:
    Code:
    #include<stdio.h>
    
    #define N 6
    
    #define K 3
    
    int f(int A[N]);
    
    int  i,k;
    
     
    
    void main()
    
    {
    	int A[N]={4444, 1111, 2222, 1111 ,3333, 4444};  
    
    	int D[N]= {1, 3, 7, 2, 8, 3};
    	int sum[N]={0};
    	
    	for (i=0;i<N;i++)
    	{
    		k=f(A)+1;
    		if (k==0)
    			sum[i]=D[i];
    	else
    		sum[k]+=D[k];
    	}
    
    printf("\nA sub array found in row i = %d, \n", f(A)+1);
    	
    
    //	puts("\nDid not found a sub array");//
    
    	
    for (i=0;i<N;i++)
    	printf("\nsum is = %d, \n", sum[i]);
    
    }
    
    int f(int A[N])
    {
    	int i, j, c=0;
    
    	for (i = 0; i < N ; i++)
    		for (j = 1 ; j < N ; j++)
    
    		 if (A[i] == A[j] )  
    			c = i;
              
    return (c);
    
    }
    It is clear to me my uniqueness algorithm is false. Looked up google & this forum & couldn’t come to conclusion how should I write it.
    Last edited by ronenk; 09-30-2004 at 07:08 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't understand what you're trying to do. What do you mean "uniqueness algorithm"? Are you trying to, given a value, see if it exists more than once in an array?
    Code:
    for x = 0; x < number of elements in the array; x++
        if array[x] is the number to find
            if not found
                found = true
                foundhere = x;
            else
                figure out if you want to stop looking or not because it's already been found
    Is something like that what you had in mind?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    did not exactly understand your code...
    what I need is to check in the array of catalugue numbers (A) if this element exist. If it is not then intialize the value of sum array with the number in corresponding place from array D. If it is exists ad it to the the place where the poriginal was started.
    Hope I’m clearer now…
    Last edited by ronenk; 09-30-2004 at 08:54 AM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    If I did understand correctly, what you want is something along this:

    Code:
    #include <stdio.h>
    
    #define ARRAY_SIZE(array)       (sizeof(array) / sizeof(*array))
    
    int main(void)
    {
            struct {
                    unsigned int number;
                    unsigned int quantity;
            } catalogue[] = {
                    { 4444, 1 },
                    { 1111, 3 },
                    { 2222, 7 },
                    { 1111, 2 },
                    { 3333, 8 },
                    { 4444, 3 }
            };
            unsigned int i, j;
    
    
            for (i = 0; i < ARRAY_SIZE(catalogue); i++)
                    for (j = 0; j < ARRAY_SIZE(catalogue); j++)
                            if (i != j && catalogue[i].number == catalogue[j].number) {
                                    catalogue[i].quantity += catalogue[j].quantity;
                                    catalogue[j].quantity = 0;
                            }
    
            for (i = 0; i < ARRAY_SIZE(catalogue); i++)
                    printf("%d = %d\n", catalogue[i].number, catalogue[i].quantity);
    
            return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM