CSP Lab Manual 9
CSP Lab Manual 9
CSP Lab Manual 9
Table of Contents
1.
Introduction
104
2.
Activity Time-boxing
104
3.
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
107
5.1
5.2
107
107
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.
112
9.
Evaluation Criteria
112
10.
Further Readings
112
10.1
112
Slides
P a g e | 103
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
P a g e | 104
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>
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
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
Column index
P a g e | 106
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.
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:
Total Vowels: 6
A: 1
E: 1
I: 2
O: 2
Department of Computer Science,
MAJU, 2013
P a g e | 107
U: 0
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
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:
P a g e | 108
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
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
P a g e | 109
In the source file created in the project WordCounter write following C++ code:
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
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.
P a g e | 110
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.
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).
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:
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
P a g e | 111
54,
3,
22, 62
11, 19,
6,
11
Practice Tasks
T1
T2
T3
Confirmation
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
Marks
20
10
35
20
5
10
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\
P a g e | 112