[go: up one dir, main page]

100% found this document useful (1 vote)
39 views4 pages

Lab # 13 Two Dimentional Arrays in C++

Uploaded by

alizarizwan006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
39 views4 pages

Lab # 13 Two Dimentional Arrays in C++

Uploaded by

alizarizwan006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

University of Management and Technology

Department of Artificial Intelligence


CC1021L Programming Fundamentals Lab
Fall 2023
Participant ID: Participant Name:

Date: Total Marks: Obtained Marks:

Lab # 13: Two Dimensional Arrays in C++


Learning Objectives
 Develop a fundamental understanding of functions in C++, including their syntax,
components, and role in modular programming.
 Gain practical proficiency in creating, calling, and debugging functions, addressing
parameters, return values, and the concept of function overloading, through a series of
hands-on coding exercises and projects.
}

Two Dimensional Arrays


The two-dimensional array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns. However, 2D arrays
are created to implement a relational database lookalike data structure. It provides ease of
holding the bulk of data at once which can be passed to any number of functions wherever
required.

every element in the array a is identified by an element name of the form a[ i ][ j ], where 'a' is
the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each element in 'a'.

Declaring 2-D Arrays


data_type array_name [rows][columns];
Example: The following statement declares a 2-D array with four rows and three columns.
int arr[4][3];

Initializing Two-Dimensional Arrays


In 1D array, we don't need to specify the size of the array if the declaration and initialization are
being done simultaneously. However, this will not work with 2D arrays. We will have to define
at least the second dimension of the array. The two-dimensional array can be declared and
defined in the following way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Multidimensional arrays may be initialized by specifying bracketed values for each row.
Following is an array with 3 rows and each row has 4 columns.
int arr[3][4] =
{
{0, 1, 2, 3}, /* initializers for row indexed by 0 */
{4, 5, 6, 7}, /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are optional. Above initialization can be
written in this manner.
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Accessing Two-Dimensional Array Elements


An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and
column index of the array.
Example # 1: Following program use a nested loop to handle a two-dimensional array.
#include <iostream>
using namespace std;

int main(){
int array[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
for (int i = 0; i < 3; i++){
for (int j = 0; j < 4; j++){
cout << "array[" << i << "][" << j << "] = " << array[i]
[j] << endl;
}
}
return 0;
}
Example#2: Storing elements in a matrix and printing it.
#include <iostream>
using namespace std;
int main (){
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cout<<"Enter a[“i”][“j“]: ";
cin>>arr[i][j];
}
}
cout<<"\n printing the elements ....\n";
for(i=0;i<3;i++)
{
cout<<endl;
for (j=0;j<3;j++)
{
cout<<arr[i][j];
}
}
}
Example # 3: Transpose a 2D
#include <iostream>

using namespace std; // Adding the using namespace std

int main() {
const int rows = 3;
const int columns = 4;
int matrix[rows][columns] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

// Display the original matrix


cout << "Original Matrix:\n";
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
cout << matrix[i][j] << " ";
}
cout << endl;
}

// Calculate and display the transpose of the matrix


cout << "\nTranspose of Matrix:\n";
for (int j = 0; j < columns; ++j) {
for (int i = 0; i < rows; ++i) {
cout << matrix[i][j] << " ";
}
cout << endl;
}

return 0;
}

Dynamic Array
Dynamic arrays in C++ refer to arrays whose size can be dynamically allocated at runtime.
Unlike static arrays, where the size is fixed at compile-time, dynamic arrays allow you to
allocate memory for the array during program execution. This is useful when you don't know the
size of the array beforehand or when you need to change the size dynamically.
In C++, dynamic arrays are typically created using pointers and memory allocation operators like
new and delete (or malloc and free, though it's generally recommended to use the former in
C++).
#include <iostream>
using namespace std;

int main() {

int size;

// Get the size of the dynamic array from the user


cout << "Enter the size of the array: ";
cin >> size;

// Dynamically allocate memory for the int array


int* dynamicArray = new int[size];

// Display the address of the dynamic array


cout << "Address of Dynamic Array: " << &dynamicArray[0] << endl;

// Populate the dynamic array


for (int i = 0; i < size; ++i) {
dynamicArray[i] = i * 2;
}

// Display the content of the dynamic array


cout << "Dynamic Array:\n";
for (int i = 0; i < size; ++i) {
cout << dynamicArray[i] << " ";
}

// Display the address of each dynamic memory location


cout << "\nAddresses of Dynamic Array Elements:\n";
for (int i = 0; i < size; ++i) {
cout << "Element " << i << ": " << &dynamicArray[i] << endl;
}

// Deallocate the memory for the dynamic array


delete[] dynamicArray;

return 0;
}

You might also like