Passing a 2D array to a C++ function

C++Server Side ProgrammingProgramming

Arrays can be passed to a function as an argument. In this program, we will perform to display the elements of the 2 dimensional array by passing it to a function.

Algorithm

Begin
   The 2D array n[][] passed to the function show().
   Call function show() function, the array n (n) is traversed using a nested for loop.
End

Example Code

 Live Demo

#include <iostream>
using namespace std;
void show(int n[4][3]);
int main() {
   int n[4][3] = {
      {3, 4 ,2},
      {9, 5 ,1},
      {7, 6, 2},
      {4, 8, 1}};
   show(n);
   return 0;
}
void show(int n[][3]) {
   cout << "Printing Values: " << endl;
   for(int i = 0; i < 4; ++i) {
      for(int j = 0; j < 3; ++j) {
         cout << n[i][j] << " ";
      }
   }
}

Output

Printing Values:
3 4 2 9 5 1 7 6 2 4 8 1
raja
Published on 29-Mar-2019 11:42:23
Advertisements