Skip to main content
A program to insert values to the first column of a 2D array of 4X3 and to print the 2D array as a grid assigning 0 to other elements using a function

#include <iostream>
void displayArray(int arr[][3],int row,int col);
using namespace std;
int main ()
{
        int data[4][3];

        for(int i=0;i<4;i++)
        {
                cout<<"Please input the number"<<endl;
                cin>>data[i][0];
        }
        displayArray(data,4,3);

        return 0;
}

void displayArray(int arr[][3],int row,int col)
{
        for (int k=0;k<row;k++)
        {
                cout<<arr[k][0];//print the first element of the row
                for (int j=1;j<col;j++)
                {
                        arr[k][j]=0;
                        cout<<"\t"<<arr[k][j];//print the other two elements of the row assigning 0 to them
                }
         }
}
(OUTPUT)



Comments

Post a Comment