0% found this document useful (0 votes)
128 views9 pages

DSA Lab 2

1) Arrays allow storing multiple values of the same data type using a single variable name. Multi-dimensional arrays extend this to store arrays within arrays, effectively creating tables of data. 2) Two-dimensional arrays represent tables with rows and columns that can be initialized and accessed using index values to specify the row and column. 3) Loops are commonly used to initialize two-dimensional arrays by iterating through each row and column to assign values. Nested for loops iterate through the rows first in the outer loop and columns in the inner loop.

Uploaded by

Waleed Raja
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
0% found this document useful (0 votes)
128 views9 pages

DSA Lab 2

1) Arrays allow storing multiple values of the same data type using a single variable name. Multi-dimensional arrays extend this to store arrays within arrays, effectively creating tables of data. 2) Two-dimensional arrays represent tables with rows and columns that can be initialized and accessed using index values to specify the row and column. 3) Loops are commonly used to initialize two-dimensional arrays by iterating through each row and column to assign values. Nested for loops iterate through the rows first in the outer loop and columns in the inner loop.

Uploaded by

Waleed Raja
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/ 9

Army Public college Of Management Sciences, Rawalpindi

DATA STRUCTURES & ALGORITHM

Experiment 2
Multi-dimensional arrays
Arrays:

An array is a series of elements of the same type placed in contiguous memory


locations that can be individually referenced by adding an index to a unique
identifier. That means that, for example, we can store 5 values of type int in an
array without having to declare 5 different variables, each one with a different
identifier. Instead of that, using an array we can store 5 different values of the
same type, int for example, with a unique identifier. A typical declaration for an
array in C++ is:

type name [elements];

where type is a valid type (like int, float...), name is a valid identifier and the
elements field (which is always enclosed in square brackets []), specifies how
many of these elements the array has to contain.

Therefore, in order to declare an array called abc:


int abc [5];

Initializing Arrays:

When declaring a regular array of local scope (within a function, for example), if
we do not specify otherwise, its elements will not be initialized to any value by
default, so their content will be undetermined until we store some value in them.
The elements of global and static arrays, on the other hand, are automatically
initialized with their default values, which for all fundamental types this means
they are filled with zeros.

In both cases, local and global, when we declare an array, we have the possibility
to assign initial values to each one of its elements by enclosing the values in braces
{ }. For example:
int abc [5] = { 16, 2, 77, 40, 12071 };
Accessing the values of an array:

In any point of a program in which an array is visible, we can access the value of
any of its elements individually as if it was a normal variable, thus being able to
both read and modify its value. The format is as simple as:
name[index]

For example, to store the value 75 in the third element of abc, we could write the
following statement:

abc[2] = 75;

and, for example, to pass the value of the third element of abc to a variable called
a, we could write:
a = abc[2];

Example:

#include <iostream>

using namespace std;

int billy [] = {16, 2, 77, 40, 12071};

int n, result=0;

int main ()

for ( n=0 ; n<5 ; n++ )


{ result += billy[n];

cout << result;

return 0; }
Answer:
12206
Multi-dimensional arrays:
C++ allows multidimensional arrays. Here is the general form of a
multidimensional array declaration −

type name[size1][size2]...[sizeN];
For example, the following declaration creates a three dimensional 5 . 10 . 4
integer array −

int threedim[5][10][4];
Two-Dimensional Arrays
The simplest form of the multidimensional array is the two-dimensional array. A
two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a
two-dimensional integer array of size x,y, you would write something as follows −

type arrayName [ x ][ y ];
Where type can be any valid C++ data type and arrayName will be a valid C++
identifier.
A two-dimensional array can be think as a table, which will have x number of
rows and y number of columns. A 2-dimensional array a, which contains three
rows and four columns can be shown as below −

Thus, every element in 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.
Initializing Two-Dimensional Arrays
Multidimensioned arrays may be initialized by specifying bracketed values for
each row. Following is an array with 3 rows and each row have 4 columns.

int a[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. The following
initialization is equivalent to previous example −

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};


Accessing Two-Dimensional Array Elements
An element in 2-dimensional array is accessed by using the subscripts, i.e., row
index and column index of the array. For example −

int val = a[2][3];


The above statement will take 4th element from the 3rd row of the array. You can
verify it in the above digram.
Example:

#include <iostream>
using namespace std;

int main () {
// an array with 5 rows and 2 columns.
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};

// output each array element's value


for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ ) {

cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
}

return 0;
}

When the above code is compiled and executed, it produces the following result −

a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
As explained above, you can have arrays with any number of dimensions,
although it is likely that most of the arrays you create will be of one or two
dimensions.

Accessing each location of Two Dimensional Arrays:-

In order to store values in a C++ two dimensional arrays the programmer have to
specified the number of row and the number of column of a matrix. To access each
individual location of a matrix to store the values the user have to provide exact
number of row and number of column.

For Example:-
int matrix [2][2];
matrix [0] [0] = 43;
matrix [0] [1] = 44;
matrix [1] [0] = 56;
matrix [1] [1] = 72;

How to enter data in a Two Dimensional Arrays:-

Nested loop is used to enter data in 2-D arrays. It depends upon the programmer
which loop he wants to use it could be While loop or it could be a For loop. The
outer loop acts as the number of rows of a matrix and the inner loop acts as the
number of columns of a matrix.
For Example:-
#include<iostream.h>
#include<conio.h>

main()
{

int matrix [5] [5];


for (int m1=0 ; m1<5 ; m1++)
{
for (int m2=0 ; m2<5 ; m2++)
{
matrix [m1] [m2] = 5 ;
}
}
getch();
}

The above portion of a code uses nested For loop to assigns 5 to all the locations
of a matrix of a two dimensional array in C++.
As you can see the above array is declared by a keyword int therefore it is a 2-D
integer array.

Now consider a following example of a complete program which will elaborate the
working.

#include<iostream.h>
#include<conio.h>
main()
{
int matrix [2][3];
// For taking integer inputs in a matrix //
for (int m1=0 ; m1<2 ; m1++)
{
for (int m2=0 ; m2<3 ; m2++)
{
cout<<"Enter Integer :";
cin>>matrix [m1][m2];
}
}
cout<<endl;

// For displaying elements of a matrix on a screen //


for (int m1=0 ; m1<2 ; m1++)
{
for (int m2=0 ; m2<3 ; m2++)
{
cout<<"Your Entered Integer are :";
cout<<matrix [m1][m2];
cout<<endl;
}
}
getch();
}

Lab Tasks:
Q1. Write a program to find out the greatest number in the array.
Q2. Write a program to find out the minimum number in the array.
Q3. Write a Program to display all elements of an initialized two dimensional
array.
Q4. Write a Program to store temperature of two different cities for a week and
display it.
Q5. Write a program to input data into an array. Enter a value from the keyboard
and find out the index of the entered value in the array. If the entered number is not
found in the array, display the message “Number not found”.

You might also like