需要传递2D数组才能找到行c ++的总和

时间:2017-10-07 20:37:46

标签: c++ multidimensional-array

所以我需要创建一个函数来查找2D数组中的行总和。数组是固定的,矩阵[5] [5],用户输入25个整数。

我知道如何使用以下代码查找行的总和:

//for sake of ease lets say user inputs numbers 1-25
for (r = 0; r < 5; r++)
{
  for (c = 0; c < 5; c++)
  {
     sum = sum + matrix[r][c]
  }
  cout << "\n" << sum;
  sum = 0;
}


//the code above will display the sum of each row as follows:
15
40
65
90
115

我想将每行的总计显示为

Row 1:
Sum =

Row 2:
Sum =

etc...

如何将数组传递给函数以查找每一行的总和,以及如何将各行总和分开显示为我想要的?

我已经阅读了一篇关于在c ++初学者书中将多维数组传递给4次函数的章节,我已经阅读并在网上查看了很多不同的论坛,也许是因为我已经主持了很长时间了。没有看到答案,但我让自己头疼。我真的只想了解如何通过它。我试图修改一个数组到一个函数的传递,以找到数组中所有整数的总和,但我无法让它为我需要的东西工作。

ETA(10/7/2017 1535 PCT): 所以我尝试以下尝试将我的2D数组传递给函数并计算总和...

void total(int matrix[][5], int n, int m)
    {     // I am getting an error here though that states "expected a ';' "
        for (r = 0; r < n; r++)
        {
            int sum = 0;
            for (c = 0; c < m; c++)
                sum += matrix[r][c];

        }
        cout << "Row " << r << " = " << sum << endl;
    }

这甚至是如何用2D数组创建函数的?

ETA(10/7/2017 2100 PCT)

所以我想我想出了如何传递数组,但我似乎无法让它做正确的数学运算,这意味着这并不总结行......

#include "stdafx.h"
#include "iostream"
using namespace std;

int total( const int [][5], int, int);

int main()
{
    int c, r, matrix[5][5];

    cout << "Please input any 25 numbers you'd like, seperated by a space, then press enter:" << endl;

    for (r = 0; r < 5; r++)
    {
        for (c = 0; c < 5; c++)
        {
            cin >> matrix[r][c];
        }
    }
    getchar();

    cout << endl << "Matrix: " << endl;
    for (r = 0; r < 5; r++)
    {
        cout << endl;
        for (c = 0; c < 5; c++)
        {
            cout << matrix[r][c] << "\t";
        }
        cout << endl;
    }

    cout << "Please press the enter key to get the sums of each row << endl;
    getchar();

    cout << "Sum = " << total << endl; //this displays "Sum = 013513F2"
    system("PAUSE");
}


int total(const int matrix[][5], int R, int C)
{
    int sum = 0;

    for (int r = 0; r < R; r++)
    {
        for (int c = 0; c < C; c++)
        {
            sum = sum + matrix[r][c];
        }
    }
    return sum;
}

2 个答案:

答案 0 :(得分:1)

通过引用使用语法type (&name)[numElements]可以传递任何维度的数组。或者通过指针,您可以用&替换*。下面是一个编译的基本示例,它通过引用传递数组pass2Darray函数。或者,您可以简单地使用大小为[5 * 5]的常规数组,以确保它完全连续。由于2D数组本身不是C ++中存在的东西。然后,由于您使用的是矩阵,因此您可以按[row * i + col][col * j + row]行格式访问专栏。

#include <iostream>

// Reference to multiArray
// int (&someName)[num][num]

// Pointer to multiArray
// int (*someName)[num][num]

void pass2Darray(int (&passed)[1][1]) {
    std::cout << passed[0][0];
}

int main() {

    int arr[1][1] = { {1} };
    pass2Darray(arr);

    return 0;
}

答案 1 :(得分:0)

为了帮助未来的读者,这就是我想出的。进行了大量的研究和一百万次不同的试验和错误,但现在是:

int math(int a[5]) //The function the array has been passed to
{
    //Declaring the variables in the function 
    int sum = 0;
    double average = 0;
    int min = 0;
    int max = 0;

    min = a[0]; //setting the minimum value to compare to

    for (int C = 0; C < 5; C++) //Creates the loop to go through the row elements 
    {
        sum = sum + a[C];   // calculates the sum of each row
        if (a[C] < min) min = a[C];  //assigns the element of lowest value from row
        if (a[C] > max) max = a[C];  //assigns the element of highest value from row
    }
    average = sum / 5;  //calculates the average of each row

        cout << "Sum = " << sum << endl;     //Outputs sum
        cout << "Average = " << average << endl;   //Outputs average
        cout << "Min = " << min << endl;  //Outputs min
        cout << "Max = " << max << endl;  //Oututs max
        cout << endl;
        return 0; //return value for function
}

向下调用该函数的行并显示我正在寻找的输出:

for (r = 0; r < 5; r++) //sets up row loop for display
{
    cout << "Row " << r+1 << ":" << endl;
    math(matrix[r]); //displays calculations done in math function
    cout << endl;
}

希望这可以帮助有人在路上......