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

程序用二维数组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)后,

相关推荐
lsylalalala2 小时前
常见的排序算法1
数据结构·算法·排序算法
想吃火锅10053 小时前
【leetcode】54. 螺旋矩阵
算法·leetcode·矩阵
想吃火锅10053 小时前
【leetcode】300. 最长递增子序列
算法·leetcode·职场和发展
imaol13 小时前
数据结构---队列
java·数据结构·算法
数模竞赛Paid answer3 小时前
2026年电工杯数学建模A题绿电直连型电氢氨园区优化运行求解全过程论文及程序
算法·数学建模·电工杯
疯狂打码的少年3 小时前
【数据结构】串的模式匹配:KMP算法(重点)
数据结构·笔记·算法
晚风醉蝶4 小时前
第零篇-算法全景图
算法
疯狂打码的少年5 小时前
【数据结构】树的基本概念与二叉树定义
java·数据结构·笔记·算法
webmote335 小时前
用 NVIDIA Nemotron 3 Super + .NET 构建有记忆的多轮对话
后端·算法
Keven_115 小时前
算法札记:区分稀疏图与稠密图在经验上的数学计算差异
算法·稠密图·稀疏图