#include <
stdio.h>
#include <conio.h>
#include <time.h>
#define LEN 100 /*数组长度上限*/
/*用
随机数填充矩阵*/
void randomMatrix (int randMatrix[LEN][LEN],int row, int col, int min, int max) {
int i,j;
srand ((unsigned)time(NULL)); /*用时间做种,每次产生随机数不一样*/
for (i=0; i<row; i++)
for (j=0; j<col; j++)
randMatrix[i][j] = rand() % (max-min+1) + min; /*产生min~max的随机数*/
}
/*max返回矩阵最大值,rowIndex、colIndex返回最大值坐标*/
void maxMatrix (int matrix[LEN][LEN], int row, int col, int *max, int *rowIndex, int *colIndex) {
int i,j;
*max = matrix[0][0];
*rowIndex = *colIndex =0;
for (i=0; i<row; i++) {
for (j=0; j<col; j++) {
if (matrix[i][j]>*max) {
*max = matrix[i][j];
*rowIndex = i;
*colIndex = j;
}
}
}
}
/*打印矩阵内容*/
void printMatrix (int matrix[LEN][LEN], int row, int col) {
int i,j;
for (i=0; i<row; i++) {
for (j=0; j<col; j++) {
printf ("%d\t", matrix[i][j]);
}
putchar ('\n');
}
putchar ('\n');
}
int main (void) {
int matrix[LEN][LEN];
int row, col;
int max;
int rowIndex, colIndex;
/*定义3*3矩阵,并以1~100的随机数填充*/
row = 3;
col = 3;
randomMatrix (matrix, row, col, 1, 100);
/*求矩阵最大值及其坐标*/
maxMatrix (matrix, row, col, &max, &rowIndex, &colIndex);
/*输出结果*/
printMatrix (matrix, row, col);
printf ("矩阵最大值:matrix[%d][%d]=%d", rowIndex, colIndex, max);
putchar ('\n');
getch ();
return 0;
}
运行结果
