开发者

searching in n 2-dimensional arrays

Hi I need the following logic to implement on an n arrays(of 2-dimensional) .Here I have considered 3 arrays of one dimensional

#inclu开发者_开发技巧de<stdio.h>
main()
{
    int a[4]={2,1,4,7},b[4]={3,-3,-8,0},c[4]={-1,-4,-7,6},sum,i,j,k,val=0;
    for(i=0;i<4;i++) {
        for(j=0;j<4;j++) {
            for(k=0;k<4;k++) {
                sum = a[i]+b[j]+c[k];
                if(sum == val)
                printf("%d  %d  %d\n", a[i], b[j], c[k]);
            }
        }
    }

}

Output: 2 -8 6 ; 1 3 -4 ; 1 0 -1 ; 4 3 -7 ; 4 -3 -1 ; 4 0 -4 ; 7 -3 -4 ; 7 0 -7 ;


Please see C syntax in Wikipedia for syntax information.

In practice, you need to use int array[3][4] = ... to create an array with 3 rows and 4 columns. Later in code replace the accesses to current a, b and c arrays with a fixed row index for each case.

Rest of the implementation is left as an exercise as this sounds like homework to me.


Nice problem :-)
I will not post my full solution because the problem appears to be homework. Just a few pointers ...

I solved it with recursion: the simplification process I used was finding a sum of target in n arrays is the same as finding a sum of target - ONE_ELEMENT in n-1 arrays.

Example using your 3 arrays and a target of zero

find 3 elements with sum 0           in {2, 1, 4, 7}, {3, -3, -8, 0}, {-1, -4, -7, 6}
find 2 elements with sum 0 - 2 (-2)  in {3, -3, -8, 0}, {-1, -4, -7, 6}
find 1 elements with sum -2 - 3 (-5) in {-1, -4, -7, 6}              NOT FOUND
find 1 elements with sum -2 - -3 (1) in {-1, -4, -7, 6}              NOT FOUND
find 1 elements with sum -2 - -8 (6) in {-1, -4, -7, 6}              YAY! FOUND
...

To make it work easily, I had create a data structure for the arrays and to come up with a way to pass information between the several invocations of the recursive function (I used another array allocated in the helper recursive setup function).

The structure for the arrays was

struct sizedarray {
  int *data;
  size_t nelems;
};

and the prototypes for the recursive and helper functions was

findtarget(int target, struct sizedarray *arrays, size_t narrays);
findtarget_recursive(int target, struct sizedarray *arrays, size_t narrays, size_t level, int *saved);

Edited to add a working solution

#include <stdio.h>
#include <stdlib.h>

/* struct to hold arrays with varying sizes */
struct sizedarray {
  int *data;
  size_t nelems;
};

void findtarget_recursive(int target,
                         struct sizedarray *arrays,
                         size_t narrays,
                         size_t level,
                         int *saved) {
  size_t k, j;
  struct sizedarray *curarray = arrays + level;

  /* if no arrays left to search return */
  if (level == narrays) {
    return;
  }
  /* if only 1 arrays do not recurse */
  if (level + 1 == narrays) {
    for (k = 0; k < curarray->nelems; k++) {
      if (curarray->data[k] == target) {
        /* print saved elements from previous arrays */
        for (j = 0; j < level; j++) {
          printf("%d ", saved[j]);
        }
        /* print current element from current array */
        printf("%d\n", curarray->data[k]);
      }
    }
    return;
  } else {
    /* when 2 or more arrays left, recurse */
    for (k = 0; k < curarray->nelems; k++) {
      saved[level] = curarray->data[k];
      findtarget_recursive(target - curarray->data[k],
                           arrays,
                           narrays,
                           level + 1,
                           saved);
    }
  }
}

int findtarget(int target, struct sizedarray *arrays, size_t narrays) {
  int *saved = NULL;
  saved = malloc(narrays * sizeof *saved);
  /* assume it worked, needs something when it fails */
  if (saved) {
    findtarget_recursive(target, arrays, narrays, 0, saved);
    free(saved);
  }
  return 0;
}

int main(void) {
  int a0[] = {2, 1, 4, 7};
  int a1[] = {3, -3, -8, 0};
  int a2[] = {-1, -4, -7, 6};
  int a3[] = {1, 5, 6, 7};
  int a4[] = {-10, -4, -1, 3, 8};
  int a5[] = {17, 18, 19, 20, 21, 22, 23, 24, 25};
  struct sizedarray arrays[6];
  int target = 0;

  arrays[0].data = a0; arrays[0].nelems = sizeof a0/sizeof *a0;
  arrays[1].data = a1; arrays[1].nelems = sizeof a1/sizeof *a1;
  arrays[2].data = a2; arrays[2].nelems = sizeof a2/sizeof *a2;

  findtarget(target, arrays, 3);

  arrays[3].data = a3; arrays[3].nelems = sizeof a3/sizeof *a3;
  arrays[4].data = a4; arrays[4].nelems = sizeof a4/sizeof *a4;
  arrays[5].data = a5; arrays[5].nelems = sizeof a5/sizeof *a5;

  puts("\n\nwith 6 arrays ...");
  findtarget(target, arrays, 6);

  return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜