向函数传递二维数组
C++ Passing 2d arrays to functions
要理解计算数组的平均值,我们需要在下面声明一个函数。逐行循环读取
double getAverage(int arr[], int size)
{
int i, sum = 0;
double avg;
for (i = 0; i < size; ++i){
sum += arr[i];
}
avg = double(sum) / size;
return avg;
}
在之后,将该值调用给main函数。
#include <iostream>
using namespace std;
// function declaration:
double getAverage(int arr[], int size);
int main ()
{
// an int array with 5 elements.
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
// pass pointer to the array as an argument.
avg = getAverage( balance, 5 ) ;
// output the returned value
cout << "Average value is: " << avg << endl;
return 0;
}
我的问题是,如果我想计算row*col的平均值呢?我要申报这样的东西吗?设row和col的大小为arr[3][6]
double getAverage(int arr[][6], int noOfrows, int noOfcol)
{
float sum=0, average;
for (int i = 0 ; i < noOfrows ; i++) {
for (int j = 0; j<noOfcol; j++) {
sum = sum + arr[i][j];
}
}
average = (float)sum / (float)(noOfcol*noOfrows);
cout << " " << average;
return average;
}
这是我的代码
int main()
{
int sales[3][6] = {{1000, 800, 780, 450, 600, 1200},
{800, 900, 500, 760, 890, 1000},
{450, 560, 570, 890, 600, 1100}};
int avg;
int choice;//menu choice
const int computeAverage_choice = 1,
computeTotal_choice = 2,
listMaxMin_choice = 3,
Exit_choice = 4;
do
{
//displayMenu(); // Show Welcome screen
choice = displayMenu();
while (choice < 1 || choice > 4)
{
cout << "Please enter a valid menu choice: " ;
cin >> choice;
}
//If user does not want to quit, proceed.
if (choice != Exit_choice)
{
switch (choice)
{
case computeAverage_choice:
avg = computeAverage(sales, 3, 6);
cout<<"The averge:" << avg;
break;
case computeTotal_choice:
//reserves
break;
case listMaxMin_choice:
//reserves
break;
}
}
} while (choice != Exit_choice);
return 0;
}
如果你的函数是这样声明的:double getAverage(int arr[][6], int noOfrows, int noOfcol)
但是你试图用avg = getAverage( balance, 5 ) ;
[只有2个参数]来调用它,你的编译器应该返回一个错误。
请将呼叫调整为avg = getAverage( balance, 5 */num or rows*/ , 6 */num of cols*/) ;
#include <iostream>
using namespace std;
double getAverage(int arr[][6], int noOfrows, int noOfcol)
{
float sum=0, average;
for (int i = 0 ; i < noOfrows ; i++) {
for (int j = 0; j<noOfcol; j++) {
sum = sum + arr[i][j];
}
}
average = (float)sum / (float)(noOfcol*noOfrows);
cout << " " << average;
return average;
}
int main()
{
int a[2][6] = {{1,2,3,4,5,6},{2,3,4,5,6,7}};
getAverage(a, 2, 6); // OK
getAverage( a, 5 ) ; // compile error
return 0;
}
"我要声明这样的东西吗?"
是的。这就是如何声明一个函数,该函数接受包含6个int
的数组的数组。
如果大小未知,有两种解决方法:使用std::vector
(或者对于数组的数组,使用向量的向量)。或者您可以使用模板为数组的行和列:
template<size_t noOfrows, size_t noOfcol>
int getAverage(int const (&arr)[noOfrows][noOfcol])
{
...
}
这样你只需要以数组作为参数调用它,编译器将推断出行数和列数:
int balance1[4][7] = { ... };
int average1 = getAverage(balance1);
int balance2[2][5] = { ... };
int average2 = getAverage(balance2);
无论数组或子数组的大小,
我不太明白你的问题。如果您担心声明行和列并将其传递给函数,您可以通过将数组作为参数传递或将指向数组的指针作为参数传递来实现。我已经在#define中定义了行和色的长度。
定义#include <iostream>
using namespace std;
#define NO_OF_ROWS 2
#define NO_OF_COLS 3
主要功能int main ()
{
// an int array with 5 elements
int balance[5] = {1000, 2, 3, 17, 50};
int ar[2][3] = {{5,5,5},{10,10,10}};
double avg, avgRC;
// pass pointer to the array as an argument.
avg = getAverage( balance, 5 ) ;
avgRC = getAvg(ar);
// output the returned value
cout << "Average value is: " << avg << endl;
cout << "Average of row*col: " << avgRC << endl;
return 0;
}
返回row*col平均值的GetAvg函数
double getAvg(int arr[][NO_OF_COLS])
{
float sum=0, average;
for (int i = 0 ; i < NO_OF_ROWS ; i++) {
for (int j = 0; j<NO_OF_COLS; j++) {
sum = sum + arr[i][j];
}
}
average = (float)sum / (float)(NO_OF_ROWS*NO_OF_COLS);
cout << " " << average;
return average;
}
由于int sales[3][6]
与int sales[3 * 6]
具有相同的布局,您可以重用以前的代码并简单地调用
const double avg = getAverage(&sales[0][0], 3 * 6);
如果你想传递数组,最好的方法是通过引用传递(使用非直观的语法),并让模板推断大小:
template <std::size_t R, std::size_t C>
double getAverage(const int (&a)[R][C]) {
return std::accumulate(&a[0][0], &a[0][0] + R * C, 0.) / (R * C);
}
相关文章:
- 在函数中传递二维数组
- C++函数中推广二维数组
- 如何将 50 行和 50 列的二维数组编码为一个为元素随机分配星号的函数?
- 如何将二维数组类型字符(字符串)作为函数参数传递?
- 如何在 c++ 中初始化构造函数中的二维数组
- 编写一个可以使二维数组平坦的函数
- 二维数组传入没有全局定义 (M x N) 的函数
- 如何将二维数组作为类中的私有变量,然后在构造函数中设置它
- 如何在函数中将二维数组作为参数传递,并在 C++ 中返回二维数组
- 类的二维数组和C++中的构造函数调用
- 将二维数组传递到只需要一维数组的函数中(C++)
- 接受来自函数的二维数组(int 类型)并将其返回到主函数
- 二维数组的递归函数
- 如何制作返回C 中二维数组的函数
- 通过指针将二维数组传递给 c++ 中的函数
- C++:传递二维数组以执行函数
- 如何在C++中将动态二维数组传递给函数
- 将二维数组从数据文件传递给函数并输出内容
- 将二维数组传递给函数会产生奇怪的错误
- 二维数组 - 函数