2 array match

This is a discussion on 2 array match within the C++ Programming forums, part of the General Programming Boards category; hi,i'm still a beginner in C++, below is a sample program n its output, i would like to know how ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    Unhappy 2 array match

    hi,i'm still a beginner in C++, below is a sample program n its output, i would like to know how do I limit the match recorded to only once? Refering 2 the 2nd line output it found a match at array1 element 1 and array2 element2. But it finds the same match again in the 3rd line output at array2 element2.
    When a match is found, the scanning shouldn't repeat the matcher array1 and the matched at array2, it should proceed for the new next consequent array's element.
    Thanx to anyone who would be able to shed some light in it.thanx alot.

    #include <stdio.h>
    #define MAX 5
    int main(void)
    {
    int i, j, array1[5], array2[5];

    array1[0] = '1';
    array1[1] = '0';
    array1[2] = '0';
    array1[3] = '7';


    array2[0] = '1';
    array2[1] = '5';
    array2[2] = '0';
    array2[3] = '2';


    for (i=0;i<MAX-1;i++) {
    for(j=0;j<MAX-1;j++) {
    if (array1[i] == array2[j])
    {
    printf("we found a match at array1 element %d and array2 element %d \n", i, j);

    }
    }
    }

    return 0;

    }

    Output screen:
    we found a match at array1 element 0 and array2 element 0
    we found a match at array1 element 1 and array2 element 2
    we found a match at array1 element 2 and array2 element 2

    thanx again

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    im not sure i understand your question but try this...

    Code:
    for (i=0;i<MAX-1;i++) {
      for(j=0;j<MAX-1;j++) {
        if (array1[i] == array2[j])
        {
           printf("we found a match at array1     element %d and array2 element %d \n", i,  j);
          break;
         }
       }
    }

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    thank u, for the help...how do i make only the adjacent array rows only to match

    array1[0] = '1'; array2[0] = '1';
    array1[1] = '0'; array2[1] = '5';
    array1[2] = '0'; array2[2] = '0';
    array1[3] = '7'; array2[3] = '2';

    i only want it to match once,and its adjacent rows...thank you for the help to be rendered.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If I understand correctly:
    Code:
    for (i=0;i<MAX-1;i++) 
    {
        if (array1[i] == array2[i]) 
       {
          printf("we found a match at array1 element %d and array2 element %d \n", i, i);
       }
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    thanx jawib,,it does wat i want coz both arrays 1 & 2 were declared previously...wat if i only had somethin like below, where the arrays re stored in a image buffer, i had to specify them, if like this how do i find the match for the adjacent array rows.thanx 2 al for the help. Here for every i & j / x & y there is a value called pixelv. So here sinz both i & x re fixed, only j & y varies. how do i find a match once for every pixelv at every adjacent j & y?

    MbufGet2d(MilImage, 0L, 0L, IMAGE_WIDTH, IMAGE_HEIGHT, &pixelv )

    for(j=254;j<401;j++)

    {
    for(y=144;y<291;y++)

    {

    x=200; i=270;

    if(pixelv[i][j]==pixelv[x][y])


    {
    fprintf(fptr,"\t%d %d %d %d\n",j ,y ,pixelv[i][j] ,pixelv[x][y]);
    break;
    }
    }
    }

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    MbufGet2d(MilImage, 0L, 0L, IMAGE_WIDTH, IMAGE_HEIGHT, &pixelv )

    for(j=254;j<401;j++)

    {
    for(y=144;y<291;y++)

    {

    x=200; i=270;

    if(pixelv[i][j]==pixelv[x][y])


    {
    fprintf(fptr,"\t%d %d %d %d\n",j ,y ,pixelv[i][j] ,pixelv[x][y]);
    break;
    }
    }
    }

    How do i make the adjacent match between j & y for the same pixelv value...if both the adjacent value for j & y has the same value of pixelv than it should print out per the requirement.
    Hope someone could shed some light here..thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  2. Array of strings and memory allocation
    By maluyk in forum C Programming
    Replies: 7
    Last Post: 01-26-2009, 11:52 AM
  3. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 10:21 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21