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

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

相关推荐
De-Alf24 分钟前
Megatron-LM学习笔记(6)Megatron Model Attention注意力与MLA
笔记·学习·算法·ai
2401_8414956432 分钟前
【LeetCode刷题】打家劫舍
数据结构·python·算法·leetcode·动态规划·数组·传统dp数组
冰西瓜6001 小时前
STL——vector
数据结构·c++·算法
天呐草莓1 小时前
集成学习 (ensemble learning)
人工智能·python·深度学习·算法·机器学习·数据挖掘·集成学习
努力学算法的蒟蒻1 小时前
day45(12.26)——leetcode面试经典150
算法·leetcode·面试
闻缺陷则喜何志丹1 小时前
【离线查询 前缀和 二分查找 栈】P12271 [蓝桥杯 2024 国 Python B] 括号与字母|普及+
c++·算法·前缀和·蓝桥杯·二分查找··离线查询
夏幻灵2 小时前
为什么要配置环境变量?
笔记·算法
铭哥的编程日记2 小时前
Manacher算法解决所有回文串问题 (覆盖所有题型)
算法
LYFlied2 小时前
【每日算法】LeetCode 300. 最长递增子序列
前端·数据结构·算法·leetcode·职场和发展
ohnoooo92 小时前
251225 算法2 期末练习
算法·动态规划·图论