需要将2D数组传递给函数,并找到行c++的和

Need to pass 2D array to function and find sum of row c++

本文关键字:的和 c++ 函数 2D 数组      更新时间:2023-10-16

所以我需要创建一个函数来查找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(2017年7月10日1535 PCT):所以我试着用下面的方法,把我的二维数组传给一个函数,然后计算和。。。

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(2017年7月10日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;
}

传递任意维度的数组可以通过使用语法:type (&name)[numElements]by reference来完成。或者通过指针将&替换为*。下面是一个编译的基本示例,它通过引用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;
}

只是为了帮助未来的读者,这就是我想到的。花了很多研究和一百万次不同的尝试和错误,但现在是:

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;
}

希望这能帮助以后的人。。。