You are on page 1of 11

Mohammad Ali Jinnah University Islamabad

Department of Computer Science,


Faculty of Computing

Lab Manual for Computer Programming


Lab 12: Multi-Dimensional Arrays and Manipulating Sequence
of Characters using C++ string Data-type

Lab 12: Multi-Dimensional Arrays and Introduction to Functions

Table of Contents
1.

Introduction

104

2.

Activity Time-boxing

104

3.

Objective of the Experiment

104

4.

Concept Map

105

4.1
4.2
4.3
5.

6.

7.

String manipulation
Multi-Dimensional Arrays
Summary

106
Error! Bookmark not defined.
107

Home work before Lab

107

5.1
5.2

107
107

Problem Solution Modeling


Practices from home

Procedure & Tools

108

6.1
6.2
6.3

108
108
108

Tools
Setting-up Visual Studio 2008
Walkthrough Task

Practice Tasks
7.1
7.2
7.3
7.4
7.5

110

Practice Task 1
Practice Task 2
Practice Task 3
Out comes
Testing

111
111
Error! Bookmark not defined.
111
111

8.

Evaluation Task (Unseen)

112

9.

Evaluation Criteria

112

10.

Further Readings

112

10.1

112

Slides

Department of Computer Science,


MAJU, 2013

P a g e | 103

Lab 12: Multi-Dimensional Arrays and Introduction to Functions

Lab 12: Multi-Dimensional Arrays and Manipulating


Sequence of Characters using C++ string Data-type
1. Introduction
In this lab, you will learn and practice multi-dimensional arrays, particularly 2-dimensional arrays. In the
Lab-11, you have already learned how to deal with data consisting of sequence of characters (text).
Sequence of characters can be manipulated using character arrays (char [ ]). Actually, character array is
the way C language deals with the text data; therefore it is also called C-String.
The section 2 presents a table that outlines some major activities and tasks you will do as the part of this
lab. Table 1 also provides the estimated-time for each activity, which will help you to organize your tasks
well. Section 3 presents some of the learning objectives for this lab. Section 4 (Concept Map) discusses
and provides a comprehensive introduction of the topic. Section 5 lists the set of home-tasks you are
required to complete before this lab. Section 6 presents a walkthrough task that you will do as the first
practical activity during your lab. The walkthrough task has many small steps which you should follow as
directed in-order to complete the task and to get the desired output. After that, you will be ready to
work on some tasks on your own. The section 7 lists practice tasks for this purpose. As the part of
section 8, your lab instructor will give you some tasks at runtime and will evaluate those according to
the criteria mentioned in section 9. Section 10 lists some further reading links.
Note: Before coming to the lab, you are required to read Lab contents until section 5. You will
start your practical work from section 6 onward in the lab.

Relevant Lecture Readings:


a) Lecture No. 1718
b) Text Book: Computer Programming by D.S. Malik, second edition, pages: 504521

2. Activity Time-boxing
Table 1: Activity Time Boxing

Task No.
5.1
6.2
6.3
7
8

Activity Name
Evaluation of Design
Setting-up Visual Studio
Specialized Tasks
Practice tasks
Evaluation Task
Total Time

Activity time
20 mins
5 mins
30 mins
20 mins for each task
55 mins for all assigned task

Total Time
20 mins
5 mins
30 mins
60 mins
55 mins
170 mins

3. Objective of the Experiment

Introducing multi-dimensional arrays in particular 2D arrays.


To get basic understanding of string data-type.
To get familiarize and practice basic string functions.
Department of Computer Science,
MAJU, 2013

P a g e | 104

Lab 12: Multi-Dimensional Arrays and Introduction to Functions

Learning how to structure code to improve code-readability and code reuse.

4. Concept Map
As you have already learned in the lab 11, arrays are basically a collection of data-items or values all
based on same data-type. In C++, we can create arrays having single-dimension (a list), two dimensions
(a table), and even more than two dimensions. This lab will focus on two-dimensional arrays and will
also provide introduction of strings.
To deal with sequence of characters, C++ provides its own specialized data-type based on string class. In
C++, std::string is the standard way to deal with the sequence of characters. A string variable can store
sequence of characters, letters, symbols (*, $, %), and non-keyboard characters such as escape
sequences [Lab-1]. Some of the examples of text that a string variable can store are: Hello,
BC050102, I Love C++ Programming!, Grade:\n\tA.
The string data-type provides many built-in functions to ease the tasks related to text manipulation.
Some of the example tasks which can be performed using built-in string functions are: text comparison,
combining two text values (concatenation), finding occurrence of some character or sequence of
characters in a text, and replacing/erasing some parts of the text. The Appendix A shows some of the
most used string functions along their parameter specification/meaning. String is not the built-in data
type rather it is part of the C++ standard library. Therefore, to use string types you have to include the
corresponding library in the program using include statement:
#include <string>

4.1 String manipulation using specialized data-type

C/C++ provides character data-type to deal with individual character values. In C, character arrays are
used to deal with more than one characters or text. This character array is often referred as C-String.
C++ provides a specialized data-type (called string) to handle or manipulate text. A string type variable
can store same information but provides many useful methods/functions to process the text. C++s
string type has several other advantages too over C-String (character array) as discussed below:
4.1.1 Memory Storage

One of the main drawbacks of using character array is that it has fixed size in memory, whether or not
you utilize full allocated space. For example: if you create a character array based on 80 elements
(characters), in memory 80 bytes will be reserved for it. If you store Pakistan in this character array
only 9 array elements (including \0 or null character) will be utilize, other 71 bytes will remain allocated
but un-utilized. Strings have variable sizes and the amount of space they take up in memory depends on
the length of the string.
4.1.2 String Functions

C++ string type provides many built-in functions to ease text handling. Thats why using string type
instead of character arrays will ease writing programs in less time and with shorter source code.
Furthermore, you do not have to implement major text manipulation functionalities by yourself. In
Appendix A list of basic string functions is provided.
Department of Computer Science,
MAJU, 2013

P a g e | 105

Lab 12: Multi-Dimensional Arrays and Introduction to Functions

4.2 Multi-Dimensional Arrays

A 2 Dimensional (2D) array represents set of rows and columns in the form of a table. To declare a 2D
array, you have to provide two size values; first size represents number of rows while the second size
values represents number of columns of a 2D array.
Column index
0

int marks_of_three_students[3][5];

1
Row index

Above C++ code creates a two dimensional array having 3 rows (indexed from 02) and 5 columns
(indexed from 04). In a 2D array, accessing an array element requires two index values (one for row
and other for column). For examples: cout<<marks_of_three_students[1][3]; Prints value of the
element at 2nd row and 4th column.
As you have learned that it is very convenient to access singe dimensional arrays using for-loops.
Similarly, to conveniently access elements of a 2D array, generally nested for-loop (to generate both the
row index and column index) is used as shown in the below code example:
for(int i=0;i<3; i++) //For Row index
for(int j=0;j<5; j++) // For Column index
cin>>marks_of_three_students[i][j];

Similar to one-dimensional arrays, 2D arrays can also be initialized. To initialize a 2D array, you have to
provide comma ( , ) separated lists of values (enclosed in braces { and } ) where each list
corresponds to specific row of the 2D array. All values to be used in initialization should belong to the
same data-type the 2D array processes. In the following example, a 2D named table (int type) is created
and being initialized:
int table[3][4] = {{7, 9, 3, 21}, {30, 1, 9, 16},{45, 87, 4, 10}};

The above C++ statement will create a 2D array having 3 rows and 4 columns (total 12 int type
elements). First list of numbers will be use to initialize row-0, second list of values are placed in row-1,
and the third list at row index-2 as shown below:

Row index

21

30

16

45

87

10

Department of Computer Science,


MAJU, 2013

Column index

P a g e | 106

Lab 12: Multi-Dimensional Arrays and Introduction to Functions

4.3 Summary

Arrays are very useful data-structure that helps to process large amount of data of same-type.
Arrays simplify coding of the programs (processing large amount of similar data) using fewer
code lines as compared to the non-array based C++ program having same functionality.

5. Homework before Lab


5.1 Problem Solution Modeling

Write the pseudo-code of the following task. You are required to bring this code with you and submit
to your lab instructor.
4.1.1 Problem description: Write pseudo-code of a program that calculates the sum of odd numbers of
a matrix. First, create a 2D array having 5 rows and 5 columns. After that, the program should sum all
those numbers in the matrix which are odd. In the end, print the calculated sum.
5.2 Practices from home
4.2.1 Task-1

Write a C++ program that displays the row numbers of a matrix containing at least two prime numbers.
First, the program should create a 2D array (5 rows, 5 columns). Then, ask the user to input values in the
matrix or 2D array. After that, program should display row number (or index) which has at least two
prime numbers. If no row (of the matrix) contains at least two prime numbers then the program should
display the message No row found containing two prime numbers.
Sample Input:
Please enter data for matrix of 5 x 5:
2 6 18 49 8
33 15 11 17 13
8 21 47 37 28
12 12 12 12 12
2 3 4 5 6
Sample Output: Row number 2, 3, 5 contains prime numbers.
4.2.2 Task-1

Write a C++ program that asks the user to enter a string. The program should count total number of
vowels in the input string. The output of the program should report vowel count for each vowel
character (a, e, i, o , u), and total or sum of all vowel count.
Sample Input:

Please enter a string: i love C++ programming!


Sample Output:

Total Vowels: 6
A: 1
E: 1
I: 2
O: 2
Department of Computer Science,
MAJU, 2013

P a g e | 107

Lab 12: Multi-Dimensional Arrays and Introduction to Functions

U: 0

6. Procedure & Tools


6.1 Tools

Visual Studio 2008.


6.2 Setting-up Visual Studio 2008

[Expected time = 5 mins]

Setup Visual Studio and make a project named SumRowCol. For setup details please refer to the Lab-1
(Section 6.2).
6.3 Walkthrough Task 1

[Expected time = 30 mins]

Write a C++ program which calculates sum of each column and row separately. For this, first create a 2D
array (3 rows, 5 columns) and then ask the user to input values in the array. After that, first calculate
sum of each row separately and display that sum. Then, calculate sum of each column separately and
display that sum on the screen. To achieve such output, you need to follow the following instructions.
6.3.1 Writing Code
In the source file created in the project SumRowCol write following C++ code:

Department of Computer Science,


MAJU, 2013

P a g e | 108

Lab 12: Multi-Dimensional Arrays and Introduction to Functions

Figure 1: C++ code for sum Row-Col program


6.3.2 Compilation

After writing the code, compile your code according to the guidelines mentioned in Lab-1 (section 6.3.2)
6.3.3 Executing the Program

A sample output after running the program is shown below:

Figure 2: Final output of sum Row-Col program.


6.4 Walkthrough Task 2

[Expected time = 30 mins]

Write a C++ program that counts total words in a sentence. First, get input in a string variable prg for a
paragraph (terminated by enter key). Your program should count total number of words in the
paragraph (using find function of string) and print that value on screen as follows:
You entered 20 words for the paragraph.
To achieve such output, you need to follow the following instructions.
6.4.1 Writing Code

Department of Computer Science,


MAJU, 2013

P a g e | 109

Lab 12: Multi-Dimensional Arrays and Introduction to Functions

In the source file created in the project WordCounter write following C++ code:

.Figure 1: C++ code for word counter program

6.4.2 Compilation

After writing the code, compile your code according to the guidelines mentioned in Lab-1 (section 6.3.2)
6.4.3 Executing the Program

A sample output after running the program is shown below:

Figure 2: Final output of word count program

7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You need to
finish the tasks in the required time. When you finish them, put these tasks in the folder specified by the
lab instructor.

Department of Computer Science,


MAJU, 2013

P a g e | 110

Lab 12: Multi-Dimensional Arrays and Introduction to Functions

7.1 Practice Task 1

[Expected time = 30 mins]

Write a C++ program which repeatedly asks the user to enter their contact numbers. Create a string
variable named Phone_No and take the input in the following pattern: +92423672123 where the
first three characters represent country code (e.g., +92 as shown in the example pattern), next two
characters (e.g., 42 according the shown example number) represent the city code, while next 7
characters represent the actual number.
Your task is to display appropriate messages for the numbers belonging to three cities: Islamabad
(having city code 51), Lahore (city code 42), and Karachi (city code: 21). Any number having different
country code or city code must be reported as Un-known number. Please also ensure that the user
must enter the number according to the shown pattern only and the size of the input string must be 12
characters.

7.2 Practice Task 2

[Expected time = 30 mins]

Write a program that creates and then reads a matrix of 4 rows and 4 columns of type int. while reading;
the program should not accept values greater than 100. For any entered value greater than 100, the
program should ask for input repeatedly. After reading all numbers, the system should find the largest
number in the matrix and its location or index values. The program should print the largest number and
its location (row and column).

7.3 Out comes

After completing this lab, student will be able to use string data type and its function for text
manipulation.

7.4 Testing
Test Cases for Practice Task-1
Sample Inputs:

Enter a number: +92214352682


Sample Outputs:

This number belongs to Karachi


Do you want to enter another number (y/n): n
End of program. Bye!
Test Cases for Practice Task-2

Sample Input-1
13, 15, 76, 85
10,

4,

73, 91

1,

8,

17, 52

74, 3, 15, 26
Sample Input-2
77, 1, 19, 74

Sample output 1
Largest values in matrix is 91 at row 1 column 3

Sample output 2

Department of Computer Science,


MAJU, 2013

P a g e | 111

Lab 12: Multi-Dimensional Arrays and Introduction to Functions

54,

3,

22, 62

11, 19,

6,

Largest values in matrix is 89 at row 3 column 1

11

32, 89, 15, 85

Practice Tasks
T1
T2
T3

Confirmation

8. Evaluation Task (Unseen)

[Expected time = 55 mins]

9. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student
has finished the complete/partial task(s).
Sr. No.
1
2
3
4
5
6

Task No
4.1
6
7.1, 7.2, and 7.3
8

Table 2: Evaluation of the Lab


Description
Problem Modeling
Procedures and Tools
Practice tasks and Testing
Evaluation Tasks (Unseen)
Comments
Good Programming Practices

Marks
20
10
35
20
5
10

10. Further Readings


10.1 Slides

The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\

Department of Computer Science,


MAJU, 2013

P a g e | 112

You might also like