Itp Lab 10
Itp Lab 10
________________________________________________________________________
Name: _____________________________________________ Roll No: _____________
PERFORMANCE OBJECTIVE
Upon successful completion of this experiment, the student will be able to:
Definitions:
An array is a collection of elements, all of the same type, that are stored in consecutive memory
locations. The type of each element in an array can be a simple type, such as a char or an integer
or double, or a more complex type such as objects of type String or a user-defined type such as
Time or Product (such as a product in a grocery store). Individual elements of an array are
accessed using an index or subscript. So far, we have only used one-dimensional arrays. A one-
dimensional array can be visualized as a linear list of elements, with each element accessed by a
single index.
Multi-dimensional arrays, like one-dimensional arrays, contain elements all of the same type and
are stored in memory in consecutive locations. However, a multi-dimensional array requires more
than one index to access an individual element. A two-dimensional array can be visualized as a
table with rows and columns OR as an array of arrays (a list of one-dimensional arrays (rows)!).
To access an individual element one needs to specify both a row index and a column index.
Three-dimensional arrays require 3 indices; 4-dimensional arrays require 4 indices, and so on.
Declaring 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];
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 10
ST
________________________________________________________________________
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.
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:
________________________________________________________________________
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:
The above statement will take 4th element from the 3rd row of the array. You can verify it in the
above digram.
#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}};
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
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 10
ST
________________________________________________________________________
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.
To declare a multidimensional array, one must specify the type of the elements, and the number
of elements in each dimension (for example, in a two-dimensional array the number of elements
in each row and each column). Some examples:
int bitMap[240][320]; //array to store a color code for each pixel in a graphics image that is
//240 rows by 320 columns
Time split[10][3]; //array of objects of type Time (from earlier labs) -- first index represents
//different races, the second the three splits in a 5K race
int grade[5][4];
//The row indices go from 0 to 4; the column indices go from 0 to 3. The //following code would
read numbers into the array:
for (int row = 0; row < 5; row++)
for (int col = 0; col < 4; col++)
cin >> grade[row][col];
The filled array can be visualized as follows:
row 0 = grade[0] 75 79 85 62
row 1 = grade[1] 79 76 55 99
row 2 = grade[2] 98 35 77 81
row 3 = grade[3] 63 95 78 60
row 4 = grade[4] 88 92 88 91
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 10
ST
________________________________________________________________________
The element grade[1][2] is the number in row 1, column 2 which is 55. The element grade [4][3] is
the number in row 4, column 3 which is 91.
75
Row
0 79
85
62
79
76
Row 1
55
99
98
35
Row 2
77
81
63
95
Row 3
78
60
88
92
Row 4
88
91
________________________________________________________________________
for (int row = 0; row < numRows; row++)
for (int col = 0; col < numCols; col++)
salary[row][col] = 1.05 * salary[row][col];
Often it is convenient to use the fact that a two-dimensional array is an array of one-dimensional
arrays. For example, suppose you have a function that does a sequential search of a one-
dimensional array. Assume it takes the array, the target, and the number of elements in the array
as parameters, and returns either -1 (if it doesn't find the target) or the index of the target.
Assume the declaration (prototype or profile) is as follows:
To process a single row of a two-dimensional array generally you need just one for loop. For
example, to "do something" to row i of the two-dimensional array twoDArray you would have a
loop similar to the following:
To process a two-dimensional array of objects, one generally must use the accessor functions to
access the data members of the object one wants to process. Recall that the Time class had a
function showTime() that printed the time. To print the times in the split array defined above, one
could use the following segment of code:
________________________________________________________________________
{
for (j = 0; j < 3; j++)
split[i][j].showTime();
cout << endl;
}
someFunction(a, 4);
In C++, arrays are always passed by reference. Whenever an array is passed as a parameter, its
base address is sent to the called function.
Generally, functions that work with arrays require 2 items of information as actual parameters: the
beginning address of the array (in memory), and the number of elements to process in the
array. When passing multi-dimensional arrays as function parameters, all subscripts except the
first must contain a value of the size of that particular index.
Below is an example of the function that prints a 2-dimensional array. (Note: The second
subscript has a value.)
________________________________________________________________________
Lab Assignment:
1) For both problems, turn in the program listing and the output. Include the standard
header in each program.
a. Complete and run the following program.
// strstuff.cpp
#include <iostream>
#include <string>
int main()
{
string sentence, first, last;
cout << "The length of this sentence is " << sentence.length() << '.'
<< endl;
________________________________________________________________________
print_reverse(sentence);
cout << endl;
return 0;
void print_forward(string s)
{
for (int i = 0; i <= s.length(); i++)
cout << s[i];
cout << endl;
}
void print_reverse(string s)
{
// Complete the code
}
b) Complete and run the following program. This program creates and prints the
multiplication table for 0 to 9.
//mtable.cpp
#include <iostream>
#include <iomanip>
int main()
{
int mtable[MAX][MAX];
create_table(mtable, MAX);
print_table(mtable, MAX);
return 0;
}
________________________________________________________________________
2) Write the general format of a loop to process the elements in column j of a two-
dimensional array.
3) Compile all the programs into C++ compiler and study the working of Multi-
dimensional arrays.