Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Two Dimensional Array in Cpp language

Posted on December 12, 2016January 29, 2020

Table of Contents

  • Two dimensional Array in Cpp language
    • Initializing Two dim Array
      • Initializing standard method
      • Initializing during declaration
    • Initializing and displaying elements using loops

Two dimensional Array in Cpp language

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

  • What is Array
  • Type of Arrays
  • Declaration of two dim Array in C++
  • Two dim Array Initialization in C++
  • Two dim Array processing in C++
 

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

Two Dimensional Array in Cpp language
2 D array

Initializing Two dim Array

Initializing standard method

 

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

Two Dimensional Array in Cpp language
Output

Initializing during declaration

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

Two Dimensional Array in Cpp language
Output

Initializing and displaying elements using loops

Using for loops

#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

Two Dimensional Array in Cpp language
Output
Using while loops
#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

Two Dimensional Array in Cpp language
Output
Using do-while loops
#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

Two Dimensional Array in Cpp language
Output

 



One dim Array in  Java       One dim Array in  C++       One dime Array in  C

Two dim Array in  Java       Two dim Array in  C++      Two dim Array in  C

Three dim Array in  Java   Three dim Array in  C++    Three dim Array in  C

Array programs c++ language

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes