An algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.

I have written the following code but it is not giving any output neither does it gives any error. It just takes the inputs and returns to the code without any output!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void repzero(int **matrix, int nor, int noc);

void main()
{       clrscr();
	int **matrix;
	int nor,noc,i,j;
	cout<<endl<<"Enter the number of rows: ";
	cin>>nor;
	cout<<endl<<"Enter the number of columns: ";
	cin>>noc;
	cout<<endl<<"Enter the elements of the matrix: "<<endl;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cin>>matrix[i][j];
		}
	}
	cout<<endl<<"Original matrix : "<<endl;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cout<<matrix[i][j]<<"	";
		}
		cout<<endl;
	}
	repzero(matrix, nor, noc);
	cout<<endl<<"Resulting matrix : "<<endl;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cout<<matrix[i][j]<<"	";
		}
		cout<<endl;
	}
	getch();
}

void repzero(int **matrix, int nor, int noc)
{	int *r,*c,ri=0,ci=0,i,j;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	if(matrix[i][j]==0)
			{	r[ri]=i;
				c[ci]=i;
				ri++; ci++;
			}
		}
	}
	int rl=(sizeof r)/(sizeof r[0]);
	int cl=(sizeof c)/(sizeof c[0]);
	for(i=0; i<rl;i++)
	{	for(j=0; j<noc; j++)
		{	matrix[r[i]][j]=0;
		}
	}
	for(i=0; i<cl;i++)
	{	for(j=0; j<nor; j++)
		{	matrix[j][c[i]]=0;
		}
	}
}




Kindly help me with the bug!!
Last edited on
You are not allocating any memory for the matrix.
I also think this is wrong int rl=(sizeof r)/(sizeof r[0]);
That code is the same as writing int rl=sizeof(int*)/sizeof(int);
@peter87

but when i am running the part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void main()
{       clrscr();
	int **matrix;
	int nor,noc,i,j;
	cout<<endl<<"Enter the number of rows: ";
	cin>>nor;
	cout<<endl<<"Enter the number of columns: ";
	cin>>noc;
	cout<<endl<<"Enter the elements of the matrix: "<<endl;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cin>>matrix[i][j];
		}
	}
	cout<<endl<<"Original matrix : "<<endl;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cout<<matrix[i][j]<<"	";
		}
		cout<<endl;
	}
	
	getch();
}



it works !!
That would mean that memory gets allocated to the array!!
@peter87

I also think this is wrong int rl=(sizeof r)/(sizeof r[0]);
That code is the same as writing int rl=sizeof(int*)/sizeof(int);


Can you please suggest another way of calculating the length of the integer array!!
Last edited on
No, you are just unlucky that your incorrect program happens to "work". matrix is a pointer but you never make it point to any anything so you don't know what will happen.
Can you please suggest another way of calculating the length of the integer array!

You can't because r is a pointer. You have to keep track of the length yourself (or use a container like vector that keeps track of the size for you).

EDIT: isn't ri and ci what you want for the values of rl and cl? You have the same problem with r that you have with matrix. Its't not pointing to an array.
Last edited on
@Peter87


No, you are just unlucky that your incorrect program happens to "work". matrix is a pointer but you never make it point to any anything so you don't know what will happen.


How do I allocate memory to the array if I dont know what length it is going to be!!
Kindly suggest a way!!
@peter87

Ohk i'll try the vector method and then get back !!

Thanks a lot! :)
@peter87
EDIT: isn't ri and ci what you want for the values of rl and cl? You have the same problem with r that you have with matrix. Its't not pointing to an array.



You are right!! My bad!!

Thanks for pointing it out!!
If the size is known at compile time you can allocate the array like this int matrix[M][N]; where M and N is compile time constants.

If the size is not known at compile time you can use operator new to allocate the array.
1
2
3
4
5
6
int **matrix;
matrix = new int*[M];
for (int i = 0; i < M; i++)
{
	matrix[i] = new int[N];
}

There might be other ways that are better but this allows you to use the same syntax as you already are doing in the rest of the code.
@peter87

I have changed the above code to the following and it worked!!

thanks 4 the suggestion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void repzero(int **matrix, int nor, int noc);

void main()
{       clrscr();
	int **matrix;
	int nor,noc,i,j;
	cout<<endl<<"Enter the number of rows: ";
	cin>>nor;
	cout<<endl<<"Enter the number of columns: ";
	cin>>noc;
	matrix = new int* [nor];
	for(i=0; i<nor; i++)
	{	matrix[i]= new int[noc];
	}
	cout<<endl<<"Enter the elements of the matrix: "<<endl;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cin>>matrix[i][j];
		}
	}
	cout<<endl<<"Original matrix : "<<endl;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cout<<matrix[i][j]<<"	";
		}
		cout<<endl;
	}
	repzero(matrix, nor, noc);
	cout<<endl<<"Resulting matrix : "<<endl;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cout<<matrix[i][j]<<"	";
		}
		cout<<endl;
	}
	getch();
}

void repzero(int **matrix, int nor, int noc)
{	int r[10],c[10],ri=0,ci=0,i,j;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	if(matrix[i][j]==0)
			{	r[ri]=i;
				c[ci]=i;
				ri++; ci++;
			}
		}
	}
	
	for(i=0; i<ri;i++)
	{	for(j=0; j<noc; j++)
		{	matrix[r[i]][j]=0;
		}
	}
	for(i=0; i<ci;i++)
	{	for(j=0; j<nor; j++)
		{	matrix[j][c[i]]=0;
		}
	}
}



But can you plz suggest the most efficient way to do the same!!
Thanks
what u can do is do the following....i think u shud use the expression *(*(matrix+i)+j) instead of matrix[i][j] and introduce the variable dec...and use an additional if-else....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void main()
{       clrscr();
	int **matrix,
	int nor,noc,i,j;int dec;
	cout<<endl<<"Enter the number of rows: ";
	cin>>nor;
	cout<<endl<<"Enter the number of columns: ";
	cin>>noc;
	cout<<endl<<"Enter the elements of the matrix: "<<endl;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cin>>matrix[i][j];
		}
	}
	cout<<endl<<"Original matrix : "<<endl;
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{if(*(*(matrix+i)+j)==0)
		{dec=0;
		cout<<matrix[i][j]<<"	";
		}
		else
		{
		cout<<matrix[i][j]<<"	";
		}
		}
		cout<<endl;
	}
cout<<"The New Matrix is:";
if (dec==0)
{	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{  *(*(matrix+i)+j)=0;
		}
	}
for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cout<<matrix[i][j];
		}
	}
}
else
{
	for(i=0; i<nor;i++)
	{	for(j=0; j<noc; j++)
		{	cout<<*(*(matrix+i)+j);
		}
	}
}
	getch();
}


hope this helps.........jyoti
Last edited on
@peter87
Thanks a lot!!
Will try this 4 sure!!
@peter87
what u can do is do the following....i think u shud use the expression *(*(matrix+i)+j) instead of matrix[i][j] and introduce the variable dec...and use an additional if-else....


Can you please explain the logic behind this solution??
It wasn't me that said that and I don't know what he is on about. *(*(matrix+i)+j) will do the same as matrix[i][j] so I don't see any reason to write that way.
@peter 87
oops....my bad!!

@mj709

Kindly explain the logic there coz i cudnt get what difference would dat make in this case!!
@peter and jyoti ur correct............. the notation has nothing to do with the results desired, but just for convenience at understanding.......wen we write m[i][j] we mean we are referring a 2d array..................and wen we say *(*(m+i)+j) we are using a 2d pointer to an array....................so we dont get confused..........................only for convenience sake...........i mentioned that :)
Topic archived. No new replies allowed.