DSA Lab 2
DSA Lab 2
Experiment 2
Multi-dimensional arrays
Arrays:
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.
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>
int n, result=0;
int main ()
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 −
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 −
#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}};
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.
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;
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()
{
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;
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”.