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:
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!!