[英]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 ) ;
調用它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;
}
“我要申報這樣的東西嗎?”
是。 這就是您聲明一個函數的方法,該函數采用由六個int
組成的數組。
如果不知道大小,則有兩種解決方法:使用std::vector
(或者在數組數組的情況下,使用向量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;
}
返回行*列平均值的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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.