two dimensional array blues.

This is a discussion on two dimensional array blues. within the C Programming forums, part of the General Programming Boards category; can anyone demonstrate a way to compare a 2d array and check whether each number is different. Code: int twodarray[4][2] ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220

    two dimensional array blues.

    can anyone demonstrate a way to compare a 2d array and check whether each number is different.

    Code:
      int twodarray[4][2] = {
                         { 1, 2 },
                         { 3, 4 },
                         { 5, 6 }
                        };
     if(theyrealldifferent(twodarray)
     {
      printf("they're all different");
     }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    cmon brian its only a couple of for loops and an if statement
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Yeah,
    I tried this:
    Code:
    int checkthem(int input[4][2])
    {
     int i = 0;
     int j = 0;
     int k = 0;
     int l = 0;
    
     while(1)
     {
      if(input[i][j] == input[k][l])
      {
       return 0;
      }
      else
      {
       if(j == 0 && i == 0)
       {
        j = 1;
       }
       else if(j == 1 && i == 0)
       {
        j = 0;
        i = 1;
       }
       else if(i == 1 && j == 0)
       {
        j = 1;
       }
       else if(i == 1 && j == 1)
       {
        i = 0;
        j = 0;
        if(l == 0 && k == 0)
        {
         l = 1;
        }
        else if(l == 1 && k == 0)
        {
         l = 0;
         k++;
        }
        else if(k == 1 && l == 0)
        {
         l++;
        }
        else if(l == 1 && k == 1)
        {
         return 1;
        }
       }
      }
     }
    }
    doesn't work.
    I suck.

  4. #4
    Unregistered
    Guest

    hmm...

    a couple of suggestions...

    1) if it ain't mission critical, would it be adviseable to first convert the 2d array into a single dimensional array and, then using just 2 for loops and 1 if-else statement, get the work done?

    2) 2d array would involve a plethora of comparisions which could be done using the following code with a couple of modifications/additions:

    for(i=0;i<4;i++)
    {
    for(j=0;j<2;j++)
    {
    if(array[i][j]==array[i+1][j]
    printf(“Matching nos.!”);
    else
    {
    break;
    }
    }

    I'd rather go route 1....cheers!

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    First off, realise that the problem is really the same as that of a single-dimensional array. A 2d array is just a 1d array where the compiler does some math for you.

    This issue is very closely related to sorting, and most of the time, you only worry about having duplicate elements when it's an array you want to sort.

    However, if you don't want to sort the array, I suggest using a bubble-sort-like algorithm...
    1) Write a function that bubble-sorts the array.

    2) Replace the code that looks like this...
    Code:
    if (x < y)
    {
     swap (x, y);
    }
    With code that looks like this....
    Code:
    if (x == y)
    {
     return TRUE;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-10-2008, 12:53 AM
  2. How To pass 2 dimensional array of strings to a function
    By chottachatri in forum C Programming
    Replies: 15
    Last Post: 01-25-2008, 02:20 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM

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