0% found this document useful (0 votes)
23 views2 pages

C++ DSA Arrays

The document discusses two-dimensional (2D) arrays in C++, explaining their structure as matrices with rows and columns accessed using two indices. It provides syntax for declaring 2D arrays, examples of initializing and printing them, and a program that allows user input for array dimensions and elements. The document emphasizes the analysis and explanation of linear data structure operations as part of the course objectives.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

C++ DSA Arrays

The document discusses two-dimensional (2D) arrays in C++, explaining their structure as matrices with rows and columns accessed using two indices. It provides syntax for declaring 2D arrays, examples of initializing and printing them, and a program that allows user input for array dimensions and elements. The document emphasizes the analysis and explanation of linear data structure operations as part of the course objectives.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CONT_23CSH-103 :: ELEMENTARY DATA STRUCTURES USING C++

Home My courses CONT_23CSH-103 :: ELEMENTARY DATA STRUCTURES USING C++ Chapter 2.2

2D Arrays

2D Arrays

CO3 - Analyse and explain the behaviour of linear data structure operations using the programming addressed in
the course.

2D array

A two-dimensional (2D) array can be thought of as a matrix with rows and column where its elements are selected
(identified) using two indices. In 2-D array, to declare and access elements of a 2-D array we use 2 subscripts instead
of 1.

Syntax: datatype array_name[ROW][COL];

The total number of elements in a 2-D array is ROW*COL.

Example: int a[m][n];

In rectangular 2-dimensional arrays, m number of rows and n number of columns in the array has the same number of
array elements.

For example:

1 2 3
4 5 6

Two-Dimensional Array Program in C++


This program initializes 8 elements in a two-dimensional array of size four rows and two columns, then prints the array
on output:

#include<iostream>
using namespace std;
int main()
{
int arr[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
int i, j;
cout<<"The Two-dimensional Array is:\n";
for(i=0; i<4; i++)
{
for(j=0; j<2; j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
cout<<endl;
return 0;
}
Get Array Elements of the Given Size from the User
Now this program allows the user to enter the dimension or size of a 2D array and then its elements of the given size to
store it in a 2D array arr[][] and print the array back on the output screen along with the index number (row and
column number starting from 0):

#include<iostream>
using namespace std;
int main()
{
int row, col, i, j, arr[10][10];
cout<<"Enter the Row and Column Size for Array: ";
cin>>row>>col;
cout<<"Enter "<<row*col<<" Array Elements: ";
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
cin>>arr[i][j];
}
cout<<"\nThe Array is:\n";
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
cout<<"\nArray Elements with its Index:\n";
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
cout<<"arr["<<i<<"]["<<j<<"] = "<<arr[i][j]<<" ";
cout<<endl;
}
cout<<endl;
return 0;
}

Last modified: Saturday, 23 December 2023, 3:18 PM

Previous activity

◄ PPT Lecture 2.2.2

Jump to...

Next activity

PPT Lecture 2.2.3 ►

POWERED BY CHANDIGARH UNIVERSITY

You might also like