Two Dimensional Array Program in C++

Two Dimensional Array Program in C++

Two dimensional (2D) array can be made in C++ programming language by using two for loops,

first is outer for loop and the second one is inner for loop.

The outer for loop is responsible for rows and the inner for loop is responsible for columns as shown here in the following program.

C++ Programming Code for Two Dimensional (2D) Array

Following C++ program ask to the user to enter row and column size of the array then ask to the user to enter array elements,

and the program will display the array elements in two dimensional (i.e., display array elements in row and column):

#include <iostream>
using namespace std;
int main()
{
int arr[10][10], i,j,col,row;
cout<<"enter number of row for array:";
cin>>row;
cout<<"enter number of column for array:";
cin>>col;
cout<<"now enter"<<row<<"*"<<col<<"array element:";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>arr[i][j];
}

}
cout<<"the array is:\n";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<"\n";

}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/poission/p/10911486.html