需要将2D数组传递给函数,并找到行c++的和
Need to pass 2D array to function and find sum of row c++
所以我需要创建一个函数来查找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;
}
希望这能帮助以后的人。。。
- 为什么虚函数不能是静态的和全局的?
- 获取向量C++中第一个值和最后一个值的和
- 在程序变得低效之前,允许多少新的[]和删除[]分配是否有限制?
- 范围 v3 的和向量
- 如何使用 cmake pack redhat 风格的 rpm,这是主要的和"-devel"?
- 找出三个数,它们是给定数的除数,并且它们的和是最小的
- 矩阵c++的对角线元素的和
- 如何打印第一个和最后一个元素的和,然后打印第二个和倒数第二个元素的总和,依此类推
- 用CUDA求两个数组的和
- 为什么这个新的 [ ] 和删除 [ ] 实现会分解为 12 >整数?
- 比较元组中的和分类元素
- C++中的"和"是什么意思
- 与const成员一起安排新的和班级的分配
- C++,输出从2到20的数字的适当除数的和
- 初始化空字符串的 " " 和 {} 有什么区别?
- 求一个数组中的数字的和
- 我用堆栈找不到前缀号的和
- 需要将2D数组传递给函数,并找到行c++的和
- 放置新的和完美的转发
- 较大矩阵的子矩阵的和