Write a C++ Example Program for Two Dimensional Array

By | 27.12.2016

Two Dimensional Array


Write a C++ Example Program for Two Dimensional Array. Here’s simple Example Program for Two Dimensional Array in C++ Programming Language.


What is an Array ?


Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.


Two dimensional (2D) array can be made in C++ programming language by using two for loops, first is outer forloop 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

Here is source code of the C++ Example Program for Two Dimensional Array. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/*  C++ Example Program for Two Dimensional Array  */

#include<iostream>
using namespace std;

int main()
{

        int arr[10][10], row, col, i, j;
        cout<<"Enter rows for Array :: ";
        cin>>row;
        cout<<"\nEnter columns for Array :: ";
        cin>>col;
        cout<<"\nNow Enter "<<row<<"*"<<col<<" Array Elements :: \n";
        for(i=0; i<row; i++)
        {
                for(j=0; j<col; j++)
                {
                    cout<<"\nEnter arr["<<i<<"]["<<j<<"] Element :: ";
                        cin>>arr[i][j];
                }
        }
        cout<<"\nThe Entered Array is :: \n\n";
        for(i=0; i<row; i++)
        {
                for(j=0; j<col; j++)
                {
                        cout<<"\t"<<arr[i][j];
                }
                cout<<"\n\n";
        }

        return 0;
}

OUTPUT : :


/*  C++ Example Program for Two Dimensional Array  */

Enter rows for Array :: 3

Enter columns for Array :: 3

Now Enter 3*3 Array Elements ::

Enter arr[0][0] Element :: 1

Enter arr[0][1] Element :: 2

Enter arr[0][2] Element :: 3

Enter arr[1][0] Element :: 4

Enter arr[1][1] Element :: 5

Enter arr[1][2] Element :: 6

Enter arr[2][0] Element :: 7

Enter arr[2][1] Element :: 8

Enter arr[2][2] Element :: 9

The Entered Array is ::

        1       2       3

        4       5       6

        7       8       9


Process returned 0

Above is the source code for C++ Example Program for Two Dimensional Array which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3 3 votes
Article Rating
Category: Arrays Programs C++ Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments