【问题标题】:Function to calculate average of Sum of all elements in sub-matrix [closed]计算子矩阵中所有元素总和平均值的函数[关闭]
【发布时间】:2021-06-30 01:28:16
【问题描述】:

我想做一个带三个参数的函数

  1. 二维阵列。 (int array[rows][cols])
  2. 一个整数。 (int n)
  3. 数组[i][j]的地址。

意味着我的函数原型应该是这样的double Sub_avg(int arr[rows][cols], int n, int *arr[i][j])


功能说明

  • 第一个参数是二维数组,表示将要处理的数据。

  • 这里n代表n x n矩阵表示需要对所有元素取平均值的方阵的维数。

  • 最后一个参数int arr[i][j]是子矩阵第一个元素的地址。


示例输入

  • 例如让我们采用 3x3 矩阵,
  • 我要计算子矩阵

输出

  • 那么该函数将返回 8.25,即 (8 + 7 + 9 + 9)/4。

  1. 是否有此类计算的库函数或模板?
  2. 我主要关心的是如何在运行时传递二维数组,因为用户要输入数组的维度。

【问题讨论】:

  • 函数需要ij,而不是int *a[l][j](这是什么?)
  • C++ 有可能吗? -- 如果答案是“否”,那难道不会让 C++ 成为有史以来最弱的计算机语言之一吗?当然,这在 C++ 中是可能的。
  • 请看一下my solution你的任务。
  • 已编辑。 @PaulMcKenzie
  • int *a[i][j] 是子数组第一个元素的地址。

标签: c++ arrays pointers matrix submatrix


【解决方案1】:

通常在 C++ 中,您将使用 std::vectorstd::array 来存储数组并传递它们。但是,如果您真的需要使用普通的旧数组和指针,那么接下来我将提供您的任务解决方案。此外,在传递普通数组时,您可能会使用模板魔法,但我认为您想要一些非常简单的东西。

我对您的函数接口进行了最低限度的修改,使其足以使用普通数组和指针来解决任务。您应该将数组传递为int const *,因为在 C/C++ 中,如果没有模板魔法,您将无法传递 int arr[rows][cols],您还必须传递 rowscols,因为函数不知道数组的维度,而是在传递指向子数组的指针时,您应该在数组中传递 sub_rowsub_col 位置。

另外我决定实现Sub_avg2()函数,它非常接近你函数的接口,但它更高级和复杂,因为它使用模板。也因为模板,这意味着你的函数的主体应该只放在你的库的标题.h 文件中,主体应该在编译时作为源代码可用。但额外的好处是,这个函数会在编译时自动完成传递数组维度的工作。

您还可以注意到,我在代码中进行了额外的越界检查,并在出现错误时返回 0。

Try it online!

#include <iostream>

static double Sub_avg(int const * arr, int rows, int cols, int n, int sub_row, int sub_col) {
    if (!arr || rows < 0 || cols < 0 || n < 0 || sub_row < 0 || sub_col < 0 || sub_row + n > rows || sub_col + n > cols)
        return 0; // Just out of bounds checking. Return error.
    double sum = 0;
    for (size_t i = 0; i < n; ++i)
        for (size_t j = 0; j < n; ++j)
            sum += arr[(sub_row + i) * cols + sub_col + j];
    return sum / double(n * n);
}

template <int rows, int cols>
static double Sub_avg2(int const (&arr)[rows][cols], int n, int const * sarr) {
    int sub_row = (sarr - &arr[0][0]) / cols, sub_col = (sarr - &arr[0][0]) % cols;
    if (!arr || rows < 0 || cols < 0 || n < 0 || sub_row < 0 || sub_col < 0 || sub_row + n > rows || sub_col + n > cols)
        return 0; // Just out of bounds checking. Return error.
    double sum = 0;
    for (size_t i = 0; i < n; ++i)
        for (size_t j = 0; j < n; ++j)
            sum += arr[sub_row + i][sub_col + j];
    return sum / double(n * n);
}

int main() {
    int const rows = 3, cols = 3, n = 2, sub_row = 1, sub_col = 1;
    int arr[rows][cols] = {{3, 5, 6}, {5, 8, 7}, {5, 9, 9}};
    std::cout << Sub_avg((int*)arr, rows, cols, n, sub_row, sub_col) << std::endl;
    std::cout << Sub_avg2(arr, n, &arr[sub_row][sub_col]) << std::endl;
}

输出:

8.25
8.25

【讨论】:

  • 你能再举一些模板的例子吗?渴望了解新事物,谢谢。
  • @setller 互联网上有很多关于模板的资源,只需在 Google 中搜索 c++ templates。例如一个简单的教程is here
  • @setller 另外,如果我的回答是正确的和/或对您有帮助,请不要忘记接受和/或投票。要接受答案,请转到答案的开头,在它的左边有一个复选标记,在同一个地方有向上箭头的地方也是向上投票。您可以同时接受和支持投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 2015-04-13
  • 2011-04-11
相关资源
最近更新 更多