Saturday, 11 June 2016

Cpp Program of Passing 2D array to Function

Passing multidimensional array to function is almost same as that of passing single dimensional array. In this tutorial i will show you how to pass multidimensional array to function and also in future tutorials i will show you how to pass multidimensional array to function using pointers. So let start.


Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
using namespace std;

void filling(int arr[5][5]);    //Function Prototype
void printing(int arr[5][5]);

int main()
{
int arr[5][5]={};  // Null Array

filling(arr);  //Function Call
printing(arr);

}


void filling(int arr[5][5])
{
//Filling 2D array
cout<<"Filling Data: " <<endl;

for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cin>>arr[i][j];
}
}

}


void printing(int arr[5][5])
{
//Printing 2D array
cout<<"\nPrinting Data: " <<endl;

for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cout<<arr[i][j];
}
}

}



Output:



Cpp Program of Filling and Printing 2D Array

Filling and printing of Multidimensional array is bit tricky than that of one dimensional array. In this tutorial i am going to show you how to fill and print multidimensional array. As you can see in earlier tutorials that in single dimensional array we just used only single for loop, but in 2D array we will use for loop inside for loop or use can say nested loop. The reason is that in 2D array the data is arranged in rows and columns so for controlling row we use outer for loop and for controlling columns we use nested for loop. So let see how it works.


Program:
// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include <iostream>
using namespace std;

int main()
{
int arr[5][5]={};  // Null Array

//Filling 2D array

cout<<"Filling Data: " <<endl;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cin>>arr[i][j];
}
}


//Printing 2D array

cout<<"\nPrinting Data: " <<endl;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cout<<arr[i][j];
}
}

} // End of Program



Output: