用二维数组设置矩阵元素并求最大值元素的位置

程序用二维数组A[5][5]定义5×5矩阵,用for循环给矩阵赋初值。然后,调用函数确定矩阵的最大值元素并确定其位置。最后,调用另外一函数,给矩阵对角线元素赋值。

源程序

#define N 5

typedef int fix_matrix[N][N]; //fix_matrix表示N×N的二维整型数组

struct composition

{

int m;

int ro;

int co;

}matr;

void fix_set_diag(fix_matrix A,int val);

struct composition max_matrixelement(fix_matrix B,struct composition);

void main()

{

int i,j,val;

fix_matrix A;

matr.m=0;matr.ro=0;matr.co=0;

for(i=0;i<N;i++) //给矩阵A赋初值

for(j=0;j<N;j++)

A[i][j]=i+j;

matr=max_matrixelement(A,matr);

val=254;

fix_set_diag(A,val);

}

/***查找矩阵A的最大元素值,并确定其所在行和列*/

struct composition max_matrixelement(fix_matrix B,struct composition p)

{

int k,l;

for(k=0;k<N;k++)

for(l=0;l<N;l++)

if(p.m<B[k][l])

{

p.m=B[k][l];

p.ro=k+1;

p.co=l+1;

}

return p;

}

/***用val设置矩阵A对角线元素***/

void fix_set_diag(fix_matrix A,int val)

{

int i;

for(i=0;i<N;i++)

A[i][i]=val;

}

赋初值后,矩阵

调用函数max_matrixelement(fix_matrix B,struct composition p)后,得矩阵A的最大值为8,位置为(5,5)。

调用函数fix_set_diag(fix_matrix A,int val)后,

相关推荐
你撅嘴真丑15 小时前
第九章-数字三角形
算法
uesowys15 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder15 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮15 小时前
AI 视觉连载1:像素
算法
智驱力人工智能16 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥16 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风16 小时前
代码随想录第十五天
数据结构·算法·leetcode
3GPP仿真实验室16 小时前
【MATLAB源码】CORDIC-QR :基于Cordic硬件级矩阵QR分解
开发语言·matlab·矩阵
XX風17 小时前
8.1 PFH&&FPFH
图像处理·算法
NEXT0617 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法