Thread: index array

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    115

    index array

    hi guys i have this array problem.

    say i have an array called index which stores two numbers 1 and 4

    i want to scan through an array of say 6 values but i dont want to hit the array[1] and array[4]

    how can i do this?

    i tried:
    assuming index has values 1 and 4, val has two values.

    Code:
    for ( i = 0; i<size-1); i++)
    for(j=0;j<val-1;j++)
    while(i!=index[j])
    for(k=i;k<size-1;k++){
    if(foo[i] == bar[k]) {
    incre++;
    }
    }
    there are only 10 people in the world, those who know binary and those who dont

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Something like this?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i, j, index[] = {1,4}, array[] = {10,20,30,40,50,60};
        for(i = 0; i < sizeof(array)/sizeof(*array); ++i)
        {
            for(j = 0; j < sizeof(index)/sizeof(*index); ++j)
            {
                if(i == index[j])
                {
                    goto skip;
                }
            }
            printf("array[%d] = %d\n", i, array[i]);
        skip:
            ;
        }
        return 0;
    }
    
    /* my output
    array[0] = 10
    array[2] = 30
    array[3] = 40
    array[5] = 60
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    115

    Thumbs up

    thanks for the reply.

    is there any other way i can do this without using that goto statement? im not familiar with that expression. i tried another way below but i still cannot skip the index array.

    i tried breaking out of the loop but then i have a problem with the first for loop.
    thanks again

    Code:
    void foo_bar( int A[], int B[], int size, int *nx, int *ny )
    {
    	int i, j, k, n=0, index[SIZE], val=0;
    
    	*ny = 0;
    	*nx = 0;
    
    	for ( i=0,j=0; i<=npins-1; i++,j++ ) {
    		if ( A[i] == B[j] ) {
    			(*ny)++;
    			val=*ny;
    			index[n]=i;
    			n++;
    		}
    	}
    
    	for ( i=0; i<=size-1; i++ ) {
    		for ( j=0; j<val-1; j++ ) {
    			if ( i == index[j] ) {
    				i++;
    				break;
    			}
    		}
    
    		for (k=i; k<=npins-1;k++) {
    			if ( A[i] == B[k] )
    				(*nx)++;
    		}
    	}
    
    	printf( "%d %d\n", *ny, *nx );
    }
    there are only 10 people in the world, those who know binary and those who dont

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    npins is actually the size

    typo..
    there are only 10 people in the world, those who know binary and those who dont

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    i have a problem when i run foo_bar2 and it calls foo_bar how come the pointers *nx, *ny never change? it always equals zero when i first initialise it. what have i done wrong? I dont want it to print out the result in foo_bar, i want to pass the pointers in from foo_bar into foo_bar2!

    Code:
    void foo_bar( int A[], int B[], int size, int *nx, int *ny ) 
    {
    	int i,j;
    	int ctr1 = 0;
    	int ctr2 = 0;	
    	
    	for (i=0;i<size;i++)	{
    		if (A[i]==B[i]) {
    		ctr1++;
    		}
    	}
    	
                    for (j=0;j<size;j++) {
    	if ( A[i] != B[j] ) {
    	ctr2++;
    	}
    }
    	*nx=ctr1;
    	*ny=ctr2;
    }
    
    
    void foo_bar2( int B[], int A[], int size, int *nx, int *ny ) {
    	foo_bar( A, B, size, nx, ny );
    	printf( "\t%d nx, %d ny\n", *nx, *ny );
    	return;
    }
    there are only 10 people in the world, those who know binary and those who dont

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    After correcting this one typo (I guess that's what it is):

    >>if ( A[i] != B[j] ) {
    should be (?)
    >>if ( A[j] != B[j] ) {

    I use this code:
    Code:
      int a1[] = {1, 2, 3};
      int a2[] = {1, 2, 3};
      int i = 0, j = 0;
      foo_bar2(a1, a2, 3, &i, &j);
    And get output:
    >> 3 nx, 0 ny
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    sorry thats not it. this code is just an example of my full program, sort its a little vague.
    Im just having problems passing the pointer value into another function, it does not seem to want to accept it.

    where i have:

    *nx = crt1;
    *ny = crt2;

    i tried nx=&crt1;
    i get weird numbers

    i also tried make *nx=0, then i did (*nx)++, so just incrementing the pointer value.
    there are only 10 people in the world, those who know binary and those who dont

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm confused, are you trying to increment (or amend) the pointer itself, or the data it points to?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    all i basically want to do is have two counters, and i want to assign two pointers, one to each counter, and then pass it forward to a new function.
    there are only 10 people in the world, those who know binary and those who dont

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >is there any other way i can do this without using that goto statement?

    I would do it like this.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i, j, index[] = {1,4}, array[] = {10,20,30,40,50,60};
        for ( i = 0; i < sizeof(array)/sizeof(*array); ++i )
        {
            for ( j = 0; j < sizeof(index)/sizeof(*index); ++j )
            {
                if ( i == index[j] )
                {
                    break;
                }
            }
            if ( j == sizeof(index)/sizeof(*index) )
            {
                printf("array[%d] = %d\n", i, array[i]);
            }
        }
        return 0;
    }
    A functionized version might be like this.
    Code:
    #include <stdio.h>
    
    /*
     * Copy from 'array' to 'result' except for value of 'i' that match any of
     * the values in 'index'.
     */
    int foo(const int *array, int asize, const int *index, int isize, int *result)
    {
        int i, j, rsize = 0;
        for ( i = 0; i < asize; ++i )
        {
            for ( j = 0; j < isize; ++j )
            {
                if ( i == index[j] )
                {
                    break;
                }
            }
            if ( j == isize )
            {
                result[rsize++] = array[i];
            }
        }
        return rsize;
    }
    
    /*
     * Print the 'result' array.
     */
    void bar(const int *array, int asize, const int *index, int isize, int *result)
    {
        int i, rsize = foo(array, asize, index, isize, result);
        for ( i = 0; i < rsize; ++i )
        {
            printf("result[%d] = %d\n", i, result[i]);
        }
    }
    
    int main(void)
    {
        int index[] = {1,4}, array[] = {10,20,30,40,50,60};
        int result[ sizeof(array)/sizeof(*array) ];
        bar(array, sizeof(array)/sizeof(*array),
            index, sizeof(index)/sizeof(*index),
            result);
        return 0;
    }
    
    /* my output
    array[0] = 10
    array[2] = 30
    array[3] = 40
    array[5] = 60
    */
    Going through the other responses, I don't think I quite understand what you want to do (I think I need more caffeine to start my day). So this is here for whatever it may be worth.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] Sorting an array and preserving the original index
    By frodo_jedi in forum C Programming
    Replies: 10
    Last Post: 04-06-2009, 06:51 AM
  2. Reversing character array without accessing thro' index.
    By Roaring_Tiger in forum C Programming
    Replies: 8
    Last Post: 08-28-2004, 10:52 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM