Created
April 22, 2012 05:08
-
-
Save goctave/2455165 to your computer and use it in GitHub Desktop.
CareerCup_1.7@1point3acres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************* | |
Write an algorithm such that if an element in an MxN matrix is 0, | |
its entire row and column is set to 0 | |
**********************************************************************/ | |
#include <iostream> | |
using namespace std; | |
const int M = 4, N = 4; | |
int main() | |
{ | |
bool row[M] = {false}, col[N] = {false}; | |
int matrix[M][N] = {1, 2, 3, 4, | |
5, 6, 7, 0, | |
9, 1, 4, 6, | |
2, 4, 8, 3 | |
}; | |
for(int i = 0; i < M; i++) | |
{ | |
for(int j = 0; j < N; j++) | |
{ | |
if(matrix[i][j] == 0) | |
{ | |
row[i] = true; | |
col[j] = true; | |
} | |
} | |
} | |
for(int i = 0; i < M; i++) | |
{ | |
if(row[i] == true) | |
{ | |
for(int j = 0; j < N; j++) | |
matrix[i][j] = 0; | |
} | |
} | |
for(int j = 0; j < N; j++) | |
{ | |
if(col[j] == true) | |
for(int i = 0; i < M; i++) | |
matrix[i][j] = 0; | |
} | |
for(int i = 0; i < M; i++) | |
{ | |
for(int j = 0; j < N; j++) | |
cout << matrix[i][j] << " "; | |
cout << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment