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

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

相关推荐
尼尔森系4 小时前
排序与算法:希尔排序
c语言·算法·排序算法
AC使者4 小时前
A. C05.L08.贪心算法入门
算法·贪心算法
冠位观测者4 小时前
【Leetcode 每日一题】624. 数组列表中的最大距离
数据结构·算法·leetcode
yadanuof4 小时前
leetcode hot100 滑动窗口&子串
算法·leetcode
可爱de艺艺5 小时前
Go入门之函数
算法
武乐乐~5 小时前
欢乐力扣:旋转图像
算法·leetcode·职场和发展
a_j585 小时前
算法与数据结构(子集)
数据结构·算法·leetcode
清水加冰6 小时前
【算法精练】背包问题(01背包问题)
c++·算法
sda423423424236 小时前
8.【线性代数】——求解Ax=b
线性代数·ax=b
慢一点会很快7 小时前
FRRouting配置与OSPF介绍,配置,命令,bfd算法:
算法·智能路由器·php·ospf