Thread: What is an efficient way to REPLACE repeated elements within a 2D (10x10) int array

  1. #1
    court
    Join Date
    Feb 2014
    Location
    north myrtle beach, sc
    Posts
    11

    What is an efficient way to REPLACE repeated elements within a 2D (10x10) int array

    Thank you for your attention. I have generated a 10x10 integer matrix, by way of a 2 dimensional array, the elements are randomly generated and lie on 1 <= z <= 5. I am in need of an efficient method of setting all adjacent "duplicates" (repeating elements) of length 3 or greater to the integer six (6). The source for the brute method follows for clarity.
    Code:
     
     46         for(row=0;row<10;row++)
     47         {
     48         for(col=0;col<10;col++)
     49         {
     50         board[row][col]=rand()%4+1; //filling matrix here
     51         }}
     52         print_board(board, row, col);
     53 
     54         printf("Switch: ROW COLUMN\n ");
     55         scanf("%hu %hu", &x1, &y1);
     56         printf("With: ROW COLUMN\n");
     57         scanf("%hu %hu", &x2, &y2);
     58         swap(board, x1, y1,x2, y2);  stdrd for loop no good b/c of
     59                                        //below <-this->below    
     60         if(board[0][0]==board[0][1] && board[0][1]==board[0][2] && board[0][2]==board[0][3])
     61         {
     62                 board[0][0]=6;
     63                 board[0][1]=6;  
     64                 board[0][2]=6;
     65                 board[0][3]=6;   // brute force would require many 
                                                   // more to complete
     66         }
     67 
     68         print_board(board, row, col);
     69 
     70
    I know there must exist a much more elegant approach than listing out all permutations. If you have seen this and recall the technique I would very much appreciate your assistance. C-Monster

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by court View Post
    I know there must exist a much more elegant approach than listing out all permutations. If you have seen this and recall the technique I would very much appreciate your assistance. C-Monster
    Googling longest repeated sequence turned up some potential results, but they're fairly complex and I'm quite sure it's not worth it for a 10x10 matrix. You're probably better off with the brute force method. You're looking at 10 * 10 == 100 iterations at most, so that's really not that much, especially given how fast modern computers are.

    A standard for loop should work just fine for going through the rows of the matrix. For checking each row for repeats, I would use a while loop (inside the for loop).

    Note, work this out with paper and pencil, paying close attention to every little step you take, every little piece of data you use or keep track of (e.g. where the start and end of a repeated sequence is, how far apart those indices are, etc). That will form the basis of your algorithm. Write out the pseudo code, then turn that into real code, little by little (5-10 lines at a time), compiling and testing as you go.

    EDIT:
    You may want a while loop inside a while loop (all inside the for loop) for finding repeats, but it can still be done in 100 iterations total for a 10x10, if you're just a little bit clever. Note, those while loops would replace the awkward if statement in your code.
    Last edited by anduril462; 03-20-2014 at 06:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-21-2012, 11:48 PM
  2. Repeated printing of a specific elements-sparse matrix addition?
    By black_stallion in forum C Programming
    Replies: 0
    Last Post: 11-05-2011, 03:32 AM
  3. Elements repeated thrice except one
    By anirban in forum Tech Board
    Replies: 35
    Last Post: 04-09-2011, 10:30 PM
  4. Replace elements in vector
    By franse in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2008, 12:46 PM
  5. Replace elements in string
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 09-05-2005, 04:04 PM