Thread: Searching a sequence in a matrix

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    33

    Searching a sequence in a matrix

    Good day C Gurus

    I want to Write a C program that:
    a. Read a square matrix (5 rows by 5 columns) from the keyboard,
    loading the values by rows, or by column, at will
    b. Find the contiguous sequences of 0s in the rows of the matrix, having
    a length greater or equal to 3, if any
    c. Print the row index of such sequences.
    For example: If the matrix is the following:
    0 0 0 4 5
    1 2 0 4 5
    1 0 0 4 0
    1 2 3 4 5
    1 0 0 0 0
    The sequence of values "0 0 0" is found in the first and in the last row,
    then the program should print:
    The sequence is found in the row 0
    The sequence is found in the row 4

    Note that the third row does not contain the indicated sequence, due to the fact that the three 0s are not contiguous.

    here is my code and it works perfectly well when it comes to searching that sequence on the row level

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define rows 5
    #define columns 5
    
    
    int main()
    
    
    {
        int f;int h\\ f and h are used as counters
        int i,j;
    
    
    int matrice[rows][columns]={0,0,0,4,5,1,2,0,4,5,1,0,0,4,0,1,2,3,4,5,1,0,0,0,0};
    
    
         for(i=0;i<5;i++)
         {
          f=0;
           for(j=0;j<5;j++)
         {
           if( matrice[i][j]== matrice[i][j-1]|| matrice[i][j]==matrice[i][j+1] && (matrice[i][j]==0) )
    
    
                f++;
         }
     if (f>=3)
            {
    
    
               printf("%d",f);
            printf("the sequence is found in row %d\n",i);
            }
    
    
         }//till here is fine
    My problem comes when i try to apply the same approche on the Column level

    Code:
    
    
    Code:
    for(j=0;j<5;j++)
         {
          h=0;
           for(i=0;i<5;i++)
         {
    if( (matrice[i][j]== matrice[i-1][j])|| (matrice[i][j]==matrice[i+1][j]) && (matrice[i][j]==0) )
    
    
                h++;
         }
    
    
            {
    if (h>=3)
               printf("the sequence is found in column %d\n",j);
    
    
            }
    
    
         }


    The last part gave me strange and wrong results

    Any help will be highly appreciated
    Thanks!!


  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your code is not working perfectly well when searching the rows!

    Firstly, the first part of your code is searching the columns, not the rows. The first index is the row index.

    Secondly, it's going outside the bounds of the array.

    Also, we usually use UPPERCASE letter for macros. And the array should be defined like this (note the extra braces around each row) :
    Code:
        int m[ROWS][COLS] = {
            { 0, 0, 0, 4, 5},
            { 1, 2, 0, 4, 5},
            { 1, 0, 0, 4, 0},
            { 1, 2, 3, 4, 5},
            { 1, 0, 0, 0, 0}
        };
    Try fixing that.
    Also fix the indentation and spacing of your code before reposting.
    Last edited by algorism; 02-12-2017 at 05:05 PM.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    33
    Thanks for your prompt answer

    *First i meant that my first code is searching per row the occurence of my 0's sequence.
    *yes it goes outside the bound of the array but still i couldn t come out with a better idea (it still works) (and any improvemet is highly appreciated!!!!)
    *how to change that identation? sorry i'm still a newbe in C....

    Many thanks in advance!!!!

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I don't know what I was thinking with the row comment. Forget that.

    As for going out of bounds, it might seem like it works, but you would need to test with different input matrices (and on different machines with different compilers) to be sure, and since it's going out of bounds, there's no way it would pass that level of testing.

    The way you're testing for zeroes is strange. Why not just (using r and c for row and column indices)
    Code:
    if (m[r][c] == 0 && m[r][c + 1] == 0 && m[r][c + 2] == 0) {
        printf("pattern found in row %d\n", r);
        break; // don't look any farther.
    }
    To not go out of bounds, you would simply limit your column loop to c < COLUMNS - 2.

    To indent/space properly, just space it out so blocks of code line up and code controlled by ifs and fors are indented under them. Look how much of a mess it is. It's all over the place!

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    33
    Thanks Algorithm

    your method work perfectly fine, but its only inconvenience is that its only usable with small numbers of 0's , is there any other approach to look for a big number of occurences within a matrix?and why not count for the size of occurence (like a i have an island of 9 0's)

    thanks a million!!!

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    That's a good question. You only mentioned 3-in-a-row, so I showed you the simplest way.

    More generally, you'd want to count the maximum number of consecutive zeroes in each row. For each row, set counter and max to 0. Loop through all the elements of the row and if you find a zero, increment counter. If you find a non-zero, save the counter as max if it's greater than max, and reset the counter to 0. At the end of that row, max will contain the maximum number of consecutive zeroes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. help with searching a string on a char matrix.
    By Lired in forum C Programming
    Replies: 6
    Last Post: 04-13-2014, 08:05 AM
  3. Searching word(string) in matrix.
    By Aleremilcapo in forum C Programming
    Replies: 7
    Last Post: 09-22-2010, 09:41 PM
  4. converting sequence to matrix
    By Niki in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2010, 07:51 AM
  5. Replies: 7
    Last Post: 11-25-2008, 01:50 AM

Tags for this Thread