#include <stdio.h>
#include <string.h>
#include <iostream>
#define M 4
#define N 5
using namespace std;
void printMatrix(int ** numbers,int rows,int columns){
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
std::cout<<numbers[i][j]<<"\t";
std::cout<<std::endl;
}
std::cout<<std::endl;
}
int ** generateMatrix(int rows,int columns)
{
int **numbers=new int*[rows];
int num = 0 ;
for(int i=0;i<rows;i++)
{
numbers[i]=new int[columns];
for(int j=0;j<columns;j++)
{
numbers[i][j]=num;
num++;
}
}
numbers[2][2]=0;
return numbers;
}
void setZeroes(int** matrix, int matrixSize, int matrixColSize)
{
int m = matrixSize;
int n = matrixColSize;
//printf("m = %d , n = %d .\n",m,n );
int row[m], col[n];
memset(row, 0, sizeof(row));
memset(col, 0, sizeof(col));
int i = 0;
int j = 0;
for (i = 0; i < m; i++)
{
for ( j = 0; j < n; j++)
{
// printf("i = %d , j = %d ,matrix[i][j] = %d .\n",i,j,matrix[i][j] );
if (!matrix[i][j])
{
row[i] = col[j] = 1;
}
}
}
for ( i = 0; i < m; i++)
{
for ( j = 0; j < n; j++)
{
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
}
int main()
{
int **numbers=generateMatrix(M,N);
cout << "Before Matrix is set zeros ***********************************\n" << endl;
printMatrix(numbers,M,N);
setZeroes(numbers,M,N);
cout << "After Matrix is set zeros ***********************************\n" << endl;
printMatrix(numbers,M,N);
return 0;
}
这个代码是四不像,非常抱歉,我会改成纯正的C语言风格的代码的。