c++ passing array to functions
Whenever I work on c++ project, I alway get confused of passing an array to fuction.I thought that it would be nice to have reference of passing array to function.
There are several ways to work on this issue.
I just have made only some of them, but they always do the job!!
One thing I and you have to keep in mind is that whenever you pass the array, it is call by referece.
Which means that if you change any element of array in the funtion, the changes are remained even the array is out of th funtion.
----------------------------------------------------------------------------------------------------------------------------
The following is practice code. If you have suggestion or correction, please feel free to leave comment.
#include <iostream>
#include <functional> // i am using mac so i need this to use namespace std
using namespace std;
// 1d array functions
void printArray( int arr[], int len);
void add1( int arr[], int len);
void printArray2( int *array, int len);
// 2d array function
// for the array parameter, you can leave blank for the first one
void print2Darray(int array[][3], int len);
// 3d array function
// do you see the pattern?? you can alway make the first one blank
void print3Darray(int array[][2][2], int len);
int main()
{
int length = 5;
// 1d array example
// initialize array to 1
int array [5] = {1,2,3,4,5};
// this function's array parameter is array
cout << "the parameter of array is array" << endl;
printArray( array, length);
// this function prove that any function send array as parameter, is call by reference
// which means that if you change stuff in array in function, the changes are remaind even the array is out of that function
// when you pass the array to the function, keep it in your mind.
add1( array, length);
// this function's array parameter is pointer
cout << "the parameter of array is pointer; \n due to function add1 all elements are incremented by one" << endl;
printArray( array, length);
// this function's array parameter is array
cout << "the parameter of array is array\n due to function add1 all elements are incremented by one" << endl;
printArray( array, length);
// 2d array example
int array2d [3][3] = {{1,2,3},{4,5,6},{7,8,9}};
length=3;
// this function's array parameter is array
// i could not use pointer to pass the array
// if you know how, please let me know
cout << "2d example" << endl;
print2Darray(array2d,length);
// 3d array example
int array3d [2][2][2]={};
length =2;
cout << "3d example" << endl;
print3Darray(array3d,length);
}
void printArray ( int arr[], int len)
{
for (int i=0; i<len; i++){
cout << arr[i] << endl;
}
}
void add1( int arr[], int len)
{
for (int i=0; i<len; i++){
arr[i] += 1;
}
}
void printArray2 ( int *arr, int len)
{
for (int i=0; i<len; i++){
cout << arr[i] << endl;
}
}
void print2Darray(int array[][3], int len)
{
for(int i=0; i<len; i++){
cout << "row "<< i << endl;
for(int j=0; j<len; j++){
cout << array[i][j] << " ";
}
cout << endl;
}
}
void print3Darray(int array[][2][2], int len)
{
for(int i=0; i<len; i++){
cout << "x "<< i << endl;
for(int j=0; j<len; j++){
cout << "\ty " << j << endl;
for(int k=0; k<len; k++){
cout << "\t\tz " << k << endl;
}
}
cout << endl;
}
}
No comments:
Post a Comment