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

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

源程序

#define N 5

typedef int fix_matrixNN; //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++)

Aij=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<Bkl)

{

p.m=Bkl;

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++)

Aii=val;

}

赋初值后,矩阵

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

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

相关推荐
何以解忧,唯有..1 小时前
预订酒店问题
算法
No8g攻城狮1 小时前
【算法】什么是 DFS 与 BFS 及他们的区别
算法·深度优先·宽度优先
zww89491111 小时前
家政派单系统实战指南:从需求分析到智能调度算法落地
算法·需求分析
手写码匠1 小时前
华为云Flexus+DeepSeek征文|Dify 多 Agent 评测实战:用 DeepSeek-R1 当裁判,打造多智能体系统的自动化质量保障体系
人工智能·深度学习·算法·aigc
Uncommon.2 小时前
线性代数与向量
pytorch·python·线性代数·机器学习
ZC跨境爬虫2 小时前
LeetCode 88. 合并两个有序数组(双指针详解 + Java Python 实现)
java·python·算法·leetcode
Nil2082 小时前
leetcode 238除了自身以外数组的乘积
数据结构·算法·leetcode
土司大王2 小时前
LeetCode hot100——最长连续序列
数据结构·算法·leetcode
caimouse2 小时前
ReactOS 图形系统分析(17):区域填充 — EngPaint / IntEngPaint(paint.c)
c语言·开发语言·算法
生态学者2 小时前
Ecological Indicators | 机场鸟调的新方法:用 AWM-PC1 读懂干扰梯度下的鸟类群落
人工智能·算法·r语言·微信公众平台