-
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++;
}
}
-
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
*/
-
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 );
}
-
npins is actually the size
typo..
-
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;
}
:confused:
-
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
-
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.
-
I'm confused, are you trying to increment (or amend) the pointer itself, or the data it points to?
-
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.
-
>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.