Show toolbar

2013年7月10日 星期三

Passing array to a function

標題:使用一維陣列傳入函式的方式進行一維陣列的處理
VC++ (main.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "stdafx.h"
#include <iostream>
using namespace std;
// 一維陣列, 乘值
float* multiply(float arr[], int m ,float num)
{
int i = 0;
for(i=0;i<m;i++)
{
arr[i] *= num;
}
return arr;
}
int _tmain(int argc, _TCHAR* argv[])
{
const int m = 5;
int i = 0, j = 0;
float arrA[m] = {1, 2, 3, 4, 5};
float *arrB = new float[m]; //宣告一動態陣列
arrB= multiply(arrA, m, 5); //傳入一維陣列
for(i=0;i<m;i++) //印出一維陣列資料
{
cout << arrB[i] << endl;
}
return 0;
}

說明:
直接向函式傳入一維陣列進行處理,再存入另一個陣列輸出。

沒有留言:

張貼留言