在C ++中查找矩阵的最大值,最小值和平均值 [英] Find maximum, minimum, average of a matrix in C++

查看:364
本文介绍了在C ++中查找矩阵的最大值,最小值和平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C ++中找到给定矩阵( matrix [i] [j] )中的最大值,最小值和平均值。

How do I find the maximum, minimum and average values in a given matrix (matrix[i][j]) in C++. The type is unsigned long double.

推荐答案

这里没有什么聪明的东西(只有伪代码,

There's nothing clever to be done here (pseudocode only, since this smells like HW):

for each entry in the matrix:
    add the entry to a running sum
    compare the entry to a running min
        if it's smaller, it's the new running min
    compare the entry to a running max
        if it's larger, it's the new running max
average is the sum divided by the number of entries

您可以微调优化该循环,使其效率更高或更低,没有什么你能够以算法做得更聪明。您需要查看所有 i * j 条目。无论如何。

You can micro-optimize that loop to make it more or less efficient, but there's nothing you can do algorithmically to be more clever. You need to look at all i*j entries no matter what.

这篇关于在C ++中查找矩阵的最大值,最小值和平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆