Table of Contents
In this article, we will discuss the Two Dimensional Array in Cpp language.
In this post, we will learn how to access the Two Dimensional Array in Cpp language(how to input elements and how to display it)
Knowledge Area
In the Cpp programming Language, an array is a fixed sequenced collection of the element of the same data types. an array can be used to represent a list of number(int, float, double, etc) or name (string) or other data type of similar elements. It is one of the ways of simply a grouping of similar type data of single variables names.
Three types of arrays are in the C++ programming language
1. One dimensional array
2. Two-dimensional array
3. multidimensional array
Two dimensional Array
Declaration of Two dim Array
Syntax
return_type Array_name[size][size];
Example
int marks[4][6];
char alphabet[3][4];
This declare is two dimension array in c have 4 rows and 6 coloum
Example
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int marks[3][3];//array declaration
marks[0][0]=78; //initialization of array of array
marks[0][1]=68;
marks[0][2]=35;
marks[1][0]=79;
marks[1][1]=52;
marks[1][2]=49;
marks[2][0]=45;
marks[2][1]=73;
marks[2][2]=63;
//Display elements
cout << "Array elements display here\n" << endl;
cout << marks[0][0] << endl;
cout << marks[0][1] << endl;
cout << marks[0][2] << endl;
cout << marks[1][0] << endl;
cout << marks[1][1] << endl;
cout << marks[1][2] << endl;
cout << marks[2][0] << endl;
cout << marks[2][1] << endl;
cout << marks[2][2] << endl;
getch();
return 0;
}
When the above code executed, it produces the following result
int marks[2][3]=
{50,71,42,53,36,58};
int marks[2][3]=
{{50,71,42},{53,36,58}};
different way to initialize two dimensional Array
int marks[2][3]={{50,46,57}{78,63,84}}
float height[ ][3]={{124.5,146.8,157.3}{178.9,163.0,154.6}}
Example 1
#include <iostream>
using namespace std;
int main()
{ int marks[2][5]={{17,56,47,53,78},{37,46,67,83,98}};
cout << marks[0][0] << endl;
cout << marks[0][1] << endl;
cout << marks[0][2] << endl;
cout << marks[0][3] << endl;
cout << marks[0][4] << endl;
cout << marks[1][0] << endl;
cout << marks[1][1] << endl;
cout << marks[1][2] << endl;
cout << marks[1][3] << endl;
cout << marks[1][4] << endl;
return 0;
}
When the above code executed, it produces the following result
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int arr[3][3],i,j;
cout << "Enter the numbers for 2 D array" << endl;
for(i=0; i<3; i++){//Enter array elements using for loop
for(j=0; j<3; j++){
cin>>arr[i][j];//store array elements
}
}
cout << "\nYour entered elements display here\n" << endl;
for(i=0; i<3; i++){
for(j=0; j<3; j++){//display the array elements using for loop
cout << "you entered to index arr["<<i<<"]["<<j<<"]: "<<arr[i][j] <<endl;
}
}
getch();
return 0;
}
When the above code executed, it produces the following result
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int arr[3][3],i,j;
cout << "Enter the numbers for 2 D array" << endl;
i=0;
while(i<3){
j=0;
while(j<3){
cin>>arr[i][j];//store array elements
j++;
}
i++;
}
cout << "\nYour entered elements display here\n" << endl;
i=0;
while(i<3){
j=0;
while(j<3){//display the array elements
cout << "you entered to index arr["<<i<<"]["<<j<<"]: "<<arr[i][j] <<endl;
j++;
}
i++;
}
getch();
return 0;
}
When the above code executed, it produces the following result
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int arr[3][3],i,j;
cout << "Enter the numbers for 2 D array" << endl;
i=0;
do{
j=0;
do{
cin>>arr[i][j];//store array elements
j++;
}while(j<3);
i++;
}while(i<3);
cout << "\nYour entered elements display here\n" << endl;
i=0;
do{
j=0;
do{//display the array elements
cout << "you entered to index arr["<<i<<"]["<<j<<"]: "<<arr[i][j] <<endl;
j++;
}while(j<3);
i++;
}while(i<3);
getch();
return 0;
}
When the above code executed, it produces the following result
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.