C: Segmentation fault in my code
I wrote a code that performs an algorithm to the matrix according to the following rules:
"You will be given a matrix A of size M N which shows who read which book in the book club.
Each row corresponds to a book club member and each column corresponds to a specic book in
the library. The cell at the ith row and the jth column of the matrix is 1 if the person indexed
by i has read the book indexed by j, and it is 0 otherwise.
The coordinator of the book club decided to use an algorithm to nd the group of members
and subset of books. The problem is to nd the largest submatrix whose entries are all 1. The
order of the rows and the order of the columns have no specic meaning and can be rearranged
when nding this submatrix. The coordinator decides to use an algorithm which is ecient and
easy to implement but does not guarantee to nd the largest submatrix.
The algorithm is as follows:
1. If the matrix A contains all 1s, report A as the result and end the algorithm
2. Else nd a row R in A which contains the maximum number of 1s such that the number
of 1s is less than N. In case of equality choose the row with the smaller row label (See
below for a description of the row/column labels). Let the number of 1s in row R be k,
0 < k < N.
3. Put row R as the rst row and arrange the columns of A so that the rst k columns are 1
in the rst row and the rest of the columns are 0
4. Arrange the rest of the rows as follows in the following order:
(a) Put the rows which contain all 0 in the last N 􀀀 k columns, rst. Let the number of
such rows be p.
(b) Put the rows which contain at least one 1 in the rst k columns and at least one 1 in
the last N 􀀀 k columns, next. Let the number of such rows be q.
(c) Finally, put the rows which contain all 0s in the rst k columns, as the last set of
rows. Let the number of such rows be r.
5. Dene two submatrices U and V as follows:
(a) U is the submatrix dened by the rst 1 + p + q rows and the rst k columns.
(b) V is the submatrix dened by the last q + r rows and all the N columns.
Note that U and V overlaps.
6. Count the number of 1s in U and V . If U contains more 1s than V then set A = U and go
to Step 1, else set A = V and go to Step 1. Set M and N accordingly in the new A."


    #include <stdio.h>



    void algorithm(int matrix[1000][1000]);



    int M, N, OUTROWS[1000], OUTCOLS[1000], rowsU[1000], colsU[1000], rowsV[1000];




    void check(int matrix[1000][1000])
    {
	int i, j, ch;
	i = 0;
	ch = 0;
	while (i < M)
	{
		j = 0;
		while (j < N)
		{
			if (matrix[i][j] == 0)
			{
				ch = 1;
				break;
			}
			j = j + 1;
		}
		i = i + 1;
	}
	if (ch == 0)
	{
		printf ("%d\n", M);
		i = 0;
		while (i < M)
		{
			OUTROWS[i] = OUTROWS[i] + 1;
			i = i + 1;
		}
		i = 0;
		while (i < N)
		{
			OUTCOLS[i] = OUTCOLS[i] + 1;
			i = i + 1;
		}
		i = 0;
		while (i < M)
		{
			printf ("%d\n", OUTROWS[i]);
			i = i + 1;
		}
		printf ("%d\n", N);
		i = 0;
		while (i < N)
		{
			printf ("%d\n", OUTCOLS[i]);
			i = i + 1;
		}
	}
	else
	{
		algorithm (matrix);
	}
    }




    void algorithm(int matrix[1000][1000])
    {
	int i, j, l, onesU, onesV, colsR[1000], k, p, q, r, colarrangedmatrix[1000][1000],    arrangedmatrix[1000][1000], U[1000][1000], V[1000][1000];




	/*First step*/

	k = 0;
	i = 0;
	while (i < M)
	{
		onesU = 0;
		j = 0;
		while (j < N)
		{
			if (matrix[i][j] == 1);
			{
				onesU = onesU + 1;
			}
			j = j + 1;
		}
		if (onesU > k && onesU != N)
		{
			k = onesU;
			rowsU[0] = i;
		}
		i = i + 1;
	}
	i = 0;
	while (i < M)
	{
		j = 0;
		l = 0;
		while (j < N)
		{
			if (matrix[(rowsU[0])][j] == 1)
			{
				colarrangedmatrix[i][l] = matrix[i][j];
				colsR[l] = j;
				l = l + 1;
			}
			j = j + 1;
		}
		i = i + 1;
	}
	i = 0;
	while (i < M)
	{
		j = 0;
		l = k;
		while (j < N)
		{
			if (matrix[(rowsU[0])][j] == 0)
			{
				colarrangedmatrix[i][l] = matrix[i][j];
				colsR[l] = j;
				l = l + 1;
			}
			j = j + 1;
		}
		i = i + 1;
	}
	i = 0;
	while (i < M)
	{
		j = 0;
		while (j < N)
		{
			printf ("%d ", colarrangedmatrix[i][j]);
			j = j + 1;
		}
		printf ("\n");
		i = i + 1;
	}
	printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n");
	i = 0;
	while (j < N)
	{
		arrangedmatrix[0][j] = colarrangedmatrix[0][j];
		j = j + 1;
	}



	/*Second step*/

	p = 0;
	i = 0;
	while (i < M)
	{
		if (i == rowsU[0])
		{
			i = i + 1;
		}
		else
		{
			j = 0;
			while (j < N - k)
			{
				if (colarrangedmatrix[i][N-1-j] == 1)
				{
					break;
				}
				if (j == (N - k - 1))
				{
					l = 0;
					while (l < N)
					{
						arrangedmatrix[p+1][l] = colarrangedmatrix[i][l];
						rowsU[p+1] = i;
						l = l + 1;
					}
					p = p + 1;
				}
				j = j + 1;
			}
			i = i + 1;
		}
	}



	/*Third step*/

	q = 0;
	i = 0;
	while (i < M)
	{
		j = 0;
		while (j < p + 1)
		{
			if (i == rowsU[j])
			{
				break;
			}
			j = j + 1;
		}
		if (j == p)
		{
			j = 0;
			while (j < k)
			{
				if (colarrangedmatrix[i][j] == 1)
				{
					l = 0;
					while (l < N)
					{
						arrangedmatrix[q+p+1][l] = colarrangedmatrix[i][l];
						rowsU[p+q+1] = i;
						rowsV[q] = i;
						l = l + 1;
					}
					q = q + 1;
				}
				j = j + 1;
			}
		}
		i = i + 1;
	}





	/*Fourth step*/
	r = 0;
	i = 0;
	while (i < M)
	{
		j = 0;
		while (j < p+q+1)
		{
			if (i == rowsU[j])
			{
				break;
			}
			j = j + 1;
		}
		if (j == p + q)
		{
			l = 0;
			while (l < N)
			{
				arrangedmatrix[p+q+r+1][l] = colarrangedmatrix[i][l];
				rowsV[q+r] = i; 
				l = l + 1;
			}
			r = r + 1;
		}
		i = i + 1;
	}
	i = 0;
	while (i < k)
	{
		colsU[i] = colsR[i];
		i = i + 1;
	}
	i = 0;
	while (i < p+q+1)
	{
		j = 0;
		while (j < k)
		{
			U[i][j] = arrangedmatrix[i][j];
			j = j + 1;
		}
		i = i + 1;
	}
	i = M - q - r;
	l = 0;
	while (i < M)
	{
		j = 0;
		while (j < N)
		{
			V[l][j] = arrangedmatrix[i][j];
			j = j + 1;
		}
		i = i + 1;
		l = l + 1;
	}
	onesU = 0;
	i = 0;
	while (i < p+q+1)
	{
		j = 0;
		while (j < k)
		{
			if (U[i][j] == 1)
			{
				onesU = onesU + 1;
			}
			j = j + 1;
		}
		i = i + 1;
	}
	onesV = 0;
	i = 0;
	while (i < q+r)
	{
		j = 0;
		while (j < N)
		{
			if (V[i][j] == 1)
			{
				onesV = onesV + 1;
			}
			j = j + 1;
		}
		i = i + 1;
	}
	i = 0;
	while (i < p+q+1)
	{
		j = 0;
		while (j < k)
		{
			printf ("%d ", U[i][j]);
			j = j + 1;
		}
		printf ("\n");
		i = i + 1;
	}
	printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
	while (i < q+r)
	{
		j = 0;
		while (j < N)
		{
			printf ("%d ", U[i][j]);
			j = j + 1;
		}
		printf ("\n");
		i = i + 1;
	}
	printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
	if (onesU > onesV)
	{
		i = 0;
		M = p + q + 1;
		N = k;
		while (i < p+q+1)
		{
			OUTROWS[i] = rowsU[i];
			i = i + 1;
		}
		i = 0;
		while (i < k)
		{
			OUTCOLS[i] = colsU[i];
			i = i + 1;
		}
		i = 0;
		while (i < M)
		{
			j = 0;
			while (j < N)
			{
				printf ("%d ", U[i][j]);
				j = j + 1;
			}
			printf ("\n");
			i = i + 1;
		}
		printf ("\n\n\n\n\n\n\n\n\n\n");
		check(U);
	}
	else
	{
		i = 0;
		M = q + r;
		while (i < q+r)
		{
			OUTROWS[i] = rowsV[i];
			i = i + 1;
		}
		i = 0;
		while (i < N)
		{
			OUTCOLS[i] = colsR[i];
			i = i + 1;
		}
		i = 0;
		while (i < M)
		{
			j = 0;
			while (j < N)
			{
				printf ("%d ", V[i][j]);
				j = j + 1;
			}
			printf ("\n");
			i = i + 1;
		}
		printf ("\n\n\n\n\n\n\n\n\n\n");
		check(V);
	}
    }




    int main()
    {
	int i, j, A[1000][1000];
	fscanf (stdin, "%d %d", &M, &N);
	i = 0;
	while (i < M)
	{
		j = 0;
		while (j < N)
		{
			fscanf (stdin, "%d", &A[i][j]);
			j = j + 1;
		}
		i = i + 1;
	}
	i = 0;
	while (i < M)
	{
		OUTROWS[i] = i;
		i = i + 1;
	}
	i = 0;
	while (i < N)
	{
		OUTCOLS[i] = i;
		i = i + 1;
	}
	check (A);
	return 0;
    }




And I have segmentation fault. I used printf to find where the segmentation fault occurs, but it was hopeless. Sorry if this is a noob question, but I can't find where is the error. Thanks in advance
C: Segmentation fault in my code
===

I wrote a code that performs an algorithm to the matrix according to the following rules:
"You will be given a matrix A of size M N which shows who read which book in the book club.
Each row corresponds to a book club member and each column corresponds to a specic book in
the library. The cell at the ith row and the jth column of the matrix is 1 if the person indexed
by i has read the book indexed by j, and it is 0 otherwise.
The coordinator of the book club decided to use an algorithm to nd the group of members
and subset of books. The problem is to nd the largest submatrix whose entries are all 1. The
order of the rows and the order of the columns have no specic meaning and can be rearranged
when nding this submatrix. The coordinator decides to use an algorithm which is ecient and
easy to implement but does not guarantee to nd the largest submatrix.
The algorithm is as follows:
1. If the matrix A contains all 1s, report A as the result and end the algorithm
2. Else nd a row R in A which contains the maximum number of 1s such that the number
of 1s is less than N. In case of equality choose the row with the smaller row label (See
below for a description of the row/column labels). Let the number of 1s in row R be k,
0 < k < N.
3. Put row R as the rst row and arrange the columns of A so that the rst k columns are 1
in the rst row and the rest of the columns are 0
4. Arrange the rest of the rows as follows in the following order:
(a) Put the rows which contain all 0 in the last N 􀀀 k columns, rst. Let the number of
such rows be p.
(b) Put the rows which contain at least one 1 in the rst k columns and at least one 1 in
the last N 􀀀 k columns, next. Let the number of such rows be q.
(c) Finally, put the rows which contain all 0s in the rst k columns, as the last set of
rows. Let the number of such rows be r.
5. Dene two submatrices U and V as follows:
(a) U is the submatrix dened by the rst 1 + p + q rows and the rst k columns.
(b) V is the submatrix dened by the last q + r rows and all the N columns.
Note that U and V overlaps.
6. Count the number of 1s in U and V . If U contains more 1s than V then set A = U and go
to Step 1, else set A = V and go to Step 1. Set M and N accordingly in the new A."


    #include <stdio.h>



    void algorithm(int matrix[1000][1000]);



    int M, N, OUTROWS[1000], OUTCOLS[1000], rowsU[1000], colsU[1000], rowsV[1000];




    void check(int matrix[1000][1000])
    {
	int i, j, ch;
	i = 0;
	ch = 0;
	while (i < M)
	{
		j = 0;
		while (j < N)
		{
			if (matrix[i][j] == 0)
			{
				ch = 1;
				break;
			}
			j = j + 1;
		}
		i = i + 1;
	}
	if (ch == 0)
	{
		printf ("%d\n", M);
		i = 0;
		while (i < M)
		{
			OUTROWS[i] = OUTROWS[i] + 1;
			i = i + 1;
		}
		i = 0;
		while (i < N)
		{
			OUTCOLS[i] = OUTCOLS[i] + 1;
			i = i + 1;
		}
		i = 0;
		while (i < M)
		{
			printf ("%d\n", OUTROWS[i]);
			i = i + 1;
		}
		printf ("%d\n", N);
		i = 0;
		while (i < N)
		{
			printf ("%d\n", OUTCOLS[i]);
			i = i + 1;
		}
	}
	else
	{
		algorithm (matrix);
	}
    }




    void algorithm(int matrix[1000][1000])
    {
	int i, j, l, onesU, onesV, colsR[1000], k, p, q, r, colarrangedmatrix[1000][1000],    arrangedmatrix[1000][1000], U[1000][1000], V[1000][1000];




	/*First step*/

	k = 0;
	i = 0;
	while (i < M)
	{
		onesU = 0;
		j = 0;
		while (j < N)
		{
			if (matrix[i][j] == 1);
			{
				onesU = onesU + 1;
			}
			j = j + 1;
		}
		if (onesU > k && onesU != N)
		{
			k = onesU;
			rowsU[0] = i;
		}
		i = i + 1;
	}
	i = 0;
	while (i < M)
	{
		j = 0;
		l = 0;
		while (j < N)
		{
			if (matrix[(rowsU[0])][j] == 1)
			{
				colarrangedmatrix[i][l] = matrix[i][j];
				colsR[l] = j;
				l = l + 1;
			}
			j = j + 1;
		}
		i = i + 1;
	}
	i = 0;
	while (i < M)
	{
		j = 0;
		l = k;
		while (j < N)
		{
			if (matrix[(rowsU[0])][j] == 0)
			{
				colarrangedmatrix[i][l] = matrix[i][j];
				colsR[l] = j;
				l = l + 1;
			}
			j = j + 1;
		}
		i = i + 1;
	}
	i = 0;
	while (i < M)
	{
		j = 0;
		while (j < N)
		{
			printf ("%d ", colarrangedmatrix[i][j]);
			j = j + 1;
		}
		printf ("\n");
		i = i + 1;
	}
	printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n");
	i = 0;
	while (j < N)
	{
		arrangedmatrix[0][j] = colarrangedmatrix[0][j];
		j = j + 1;
	}



	/*Second step*/

	p = 0;
	i = 0;
	while (i < M)
	{
		if (i == rowsU[0])
		{
			i = i + 1;
		}
		else
		{
			j = 0;
			while (j < N - k)
			{
				if (colarrangedmatrix[i][N-1-j] == 1)
				{
					break;
				}
				if (j == (N - k - 1))
				{
					l = 0;
					while (l < N)
					{
						arrangedmatrix[p+1][l] = colarrangedmatrix[i][l];
						rowsU[p+1] = i;
						l = l + 1;
					}
					p = p + 1;
				}
				j = j + 1;
			}
			i = i + 1;
		}
	}



	/*Third step*/

	q = 0;
	i = 0;
	while (i < M)
	{
		j = 0;
		while (j < p + 1)
		{
			if (i == rowsU[j])
			{
				break;
			}
			j = j + 1;
		}
		if (j == p)
		{
			j = 0;
			while (j < k)
			{
				if (colarrangedmatrix[i][j] == 1)
				{
					l = 0;
					while (l < N)
					{
						arrangedmatrix[q+p+1][l] = colarrangedmatrix[i][l];
						rowsU[p+q+1] = i;
						rowsV[q] = i;
						l = l + 1;
					}
					q = q + 1;
				}
				j = j + 1;
			}
		}
		i = i + 1;
	}





	/*Fourth step*/
	r = 0;
	i = 0;
	while (i < M)
	{
		j = 0;
		while (j < p+q+1)
		{
			if (i == rowsU[j])
			{
				break;
			}
			j = j + 1;
		}
		if (j == p + q)
		{
			l = 0;
			while (l < N)
			{
				arrangedmatrix[p+q+r+1][l] = colarrangedmatrix[i][l];
				rowsV[q+r] = i; 
				l = l + 1;
			}
			r = r + 1;
		}
		i = i + 1;
	}
	i = 0;
	while (i < k)
	{
		colsU[i] = colsR[i];
		i = i + 1;
	}
	i = 0;
	while (i < p+q+1)
	{
		j = 0;
		while (j < k)
		{
			U[i][j] = arrangedmatrix[i][j];
			j = j + 1;
		}
		i = i + 1;
	}
	i = M - q - r;
	l = 0;
	while (i < M)
	{
		j = 0;
		while (j < N)
		{
			V[l][j] = arrangedmatrix[i][j];
			j = j + 1;
		}
		i = i + 1;
		l = l + 1;
	}
	onesU = 0;
	i = 0;
	while (i < p+q+1)
	{
		j = 0;
		while (j < k)
		{
			if (U[i][j] == 1)
			{
				onesU = onesU + 1;
			}
			j = j + 1;
		}
		i = i + 1;
	}
	onesV = 0;
	i = 0;
	while (i < q+r)
	{
		j = 0;
		while (j < N)
		{
			if (V[i][j] == 1)
			{
				onesV = onesV + 1;
			}
			j = j + 1;
		}
		i = i + 1;
	}
	i = 0;
	while (i < p+q+1)
	{
		j = 0;
		while (j < k)
		{
			printf ("%d ", U[i][j]);
			j = j + 1;
		}
		printf ("\n");
		i = i + 1;
	}
	printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
	while (i < q+r)
	{
		j = 0;
		while (j < N)
		{
			printf ("%d ", U[i][j]);
			j = j + 1;
		}
		printf ("\n");
		i = i + 1;
	}
	printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
	if (onesU > onesV)
	{
		i = 0;
		M = p + q + 1;
		N = k;
		while (i < p+q+1)
		{
			OUTROWS[i] = rowsU[i];
			i = i + 1;
		}
		i = 0;
		while (i < k)
		{
			OUTCOLS[i] = colsU[i];
			i = i + 1;
		}
		i = 0;
		while (i < M)
		{
			j = 0;
			while (j < N)
			{
				printf ("%d ", U[i][j]);
				j = j + 1;
			}
			printf ("\n");
			i = i + 1;
		}
		printf ("\n\n\n\n\n\n\n\n\n\n");
		check(U);
	}
	else
	{
		i = 0;
		M = q + r;
		while (i < q+r)
		{
			OUTROWS[i] = rowsV[i];
			i = i + 1;
		}
		i = 0;
		while (i < N)
		{
			OUTCOLS[i] = colsR[i];
			i = i + 1;
		}
		i = 0;
		while (i < M)
		{
			j = 0;
			while (j < N)
			{
				printf ("%d ", V[i][j]);
				j = j + 1;
			}
			printf ("\n");
			i = i + 1;
		}
		printf ("\n\n\n\n\n\n\n\n\n\n");
		check(V);
	}
    }




    int main()
    {
	int i, j, A[1000][1000];
	fscanf (stdin, "%d %d", &M, &N);
	i = 0;
	while (i < M)
	{
		j = 0;
		while (j < N)
		{
			fscanf (stdin, "%d", &A[i][j]);
			j = j + 1;
		}
		i = i + 1;
	}
	i = 0;
	while (i < M)
	{
		OUTROWS[i] = i;
		i = i + 1;
	}
	i = 0;
	while (i < N)
	{
		OUTCOLS[i] = i;
		i = i + 1;
	}
	check (A);
	return 0;
    }




And I have segmentation fault. I used printf to find where the segmentation fault occurs, but it was hopeless. Sorry if this is a noob question, but I can't find where is the error. Thanks in advance