Sei sulla pagina 1di 13

Mohammad Ali Jinnah University Islamabad

Department of Computer Science,


Faculty of Computing

Lab 1: Formatting using Escape Sequences

Lab 1: Formatting using Escape Sequences

Table of Contents
1.

Introduction

2.

Activity Time-boxing

3.

Objective of the Experiment

4.

Concept Map

4.1
4.2
4.3

3
3
3

5.

6.

7.

Printing characters which are not part of a Keyboard


Printing special characters
Generating Sounds

Home work before Lab

5.1
5.2

3
4

Problem solution modeling


Practices from home

Procedure & Tools

6.1
6.2
6.3

4
4
8

Tools
Setting-up Visual Studio 2008
Walk-through Task

Practice Tasks

10

7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.9

10
10
10
10
10
11
11
11
11

Practice Task 1
Practice Task 2
Practice Task 3
Practice Task 4
Practice Task 5
Practice Task 6
Practice Task 7
Out comes
Testing

8.

Evaluation Task (Unseen)

11

9.

Evaluation Criteria

11

10.Further Readings
10.1
10.2

12

Books
Slides

12
12

Appendix A Escape Sequences

12

Department of Computer Science,


MAJU, 2014

P age |1

Lab 1: Formatting using Escape Sequences

Lab 1: Formatting using Escape Sequences


1. Introduction
This lab will introduce you about how to format the output screen for C++ program using escape
sequence. Further details about escape sequences will be discussed in the concept map (section 4 of this
document).
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. 2
b) Text Book: Computer Programming by D. S. Malik, Third edition, pages: 70-77

2. Activity Time-boxing
Task No.
8
5.1
6.2
6.3
7

Table 1: Activity Time Boxing


Activity Name
Activity time
Evaluation Task
30mins for each assigned task
Evaluation of Design
20 mins
Setting-up Visual Studio
5 mins
Specialized Tasks
15mins
Practice tasks
10 mins for each task
Total Time

Total Time
60 mins
20 mins
5 mins
15mins
70 mins
170 mins

3. Objective of the Experiment

To get basic understanding of Visual studio environment.


To practice formatting and outputs in C++ using escape sequences.
To get knowledge of different alerts generated through escape sequences.

Department of Computer Science,


MAJU, 2014

P age |2

Lab 1: Formatting using Escape Sequences

4. Concept Map
4.1 Printing characters which are not part of a Keyboard

There are different types of characters on the keyboard such as: alphabets, numbers, and special
symbols. These symbols can be printed on the output screen as per the requirement. However, there
are certain characters which are not available on the keyboard and thus cannot be printed directly from
a keyboard. For example, when we want to print new line, there is no key on the keyboard which can be
used directly to print a new line. Similarly, if you want to print vertical tab, you do not have a key on the
keyboard to print it. In these scenarios, the escape sequences will help you. For example using the
escape sequence, \n, will help you to print a new line. Similarly, using the escape sequence, \v, will
help you to print a vertical tab etc. The details of further escape sequences can be found in Appendix A.
4.2 Printing special characters

There are certain characters which have special meanings in C++. For example double quotation marks
(), single quotation mark (), question marks (?)etc. For example, you know, the double quotation marks
() is used to print a text stream in the cout object. For example the following statement in C++:
cout<<Hello World;

willbe print as:


Hello World

However, these are certain times, when you need to print the quotation marks () itself in the text. To
achieve this task, you can use escape sequence, For example, the following statement in C++
cout<<\Hello\ World;

will be print as:


Hello World

The details of further escape sequences can be found in Appendix A.


4.3 Generating Sounds

If you want to generate a sound from your program at a typical point, you can use escape sequence to
achieve this. For example, writing \a will make a bell alert in the program. The details of further
escape sequences can be found in Appendix A.

5. Home work before Lab


5.1 Problem solution modeling

Solve the question number 1 and question 2 on the page number 109 of your text book. You need to
bring this code with you and need to give this code to the lab instructor.This code will be evaluated
and it carries certain marks as can be seen from the section 9, Table 2.You can use escape sequences
represented in Appendix A for formatting purposes.
Department of Computer Science,
MAJU, 2014

P age |3

Lab 1: Formatting using Escape Sequences

5.2 Practices from home

Q1. Write a C++ program which would print the following screen as output:
*

******************************
Welcome to my program *
******************************

Q2: Write a C++ program which would print the following screen as output:
*** My Introduction ***
*Name: Your Name *
*DOB: Your Birthdate
*
*City: Your city
*
*Prog: Your Prog *
*The End*

6. Procedure& Tools
6.1 Tools

Visual Studio 2008.


6.2 Setting-up Visual Studio 2008

[Expected time = 5 mins]

6.2.1 Open visual studio 2008.

1) Go to start menu.
2) Click on Microsoft Visual Studio Folder.
3) Now select Microsoft Visual Studio as shown in the Figure 1.

Figure 1: Selecting Microsoft Visual Studio

6.2.2 Open File Menu.

1) Go to File menu.
2) Select New, a sub-window will open as shown in the Figure 2.
3) Select Project from the sub-window.

Department of Computer Science,


MAJU, 2014

P age |4

Lab 1: Formatting using Escape Sequences

Figure 2: Opening File Menu

6.2.3

Select New Project.

1) Click on Project.
2) When you click on Project, a dialog box will open. On the dialog box, there are various options
like Windows Application, Console Application, DLLetc as shown in the Figure 3 (a).
3) From Application Type, Select Console Application as Figure 3 (b).
4) From additional options, select empty project as shown in the Figure 3 (b).

Figure 3 (a): Selecting New Project

Department of Computer Science,


MAJU, 2014

P age |5

Lab 1: Formatting using Escape Sequences

Figure 3 (b): Selecting New Project


6.2.4

Selection of source folder.

After the step 6.2.3, you will see a screen just like shown in the Figure 4.
There would be different options like Add, Cut, Copy etc. Click on Add and a submenu will open.

Figure 4: Selecting Source File Folder

6.2.5

Selection of Working File.

Various options would appear. Click on add New Item to select a working file as show in the Figure 5.

Department of Computer Science,


MAJU, 2014

P age |6

Lab 1: Formatting using Escape Sequences

Figure 5: Selecting New Item


6.2.6 Selection ofcpp file.

From the various options, click on C++ File (.cpp) as shown in the Figure 6.

.
Figure 6: Select CPP file

6.2.7 Give the name to the file.

Write the name of your file in the Name field as shown in the Figure 7.

Department of Computer Science,


MAJU, 2014

P age |7

Lab 1: Formatting using Escape Sequences

Figure 7: Writing Name of the file

When you click the Add button,A new file would be added to your existing project as shown in the
Figure7.
6.3 Walk-through Task

[Expected time = 15 mins]

After completing 6.2task, now you are ready for the specialized task. You need to practice the following
task to get basic understanding of developing a small program using escape sequences. When you finish
this task, the following output will be printed.
*
*

**********
Hello
*
World *

To achieve such output, you need to follow the following instructions.


6.3.1 Writing Code

Remember you created a file with name myprog in the task 6.2.7.now write the following code as
shown in the Figure 8. Write the code in the intended form as shown.

Figure 8: Writing the C++ code

Department of Computer Science,


MAJU, 2014

P age |8

Lab 1: Formatting using Escape Sequences

6.3.2 Compilation

After writing the code, now you are ready to compile it. For compilation, select Build Solution from the
Build option in the menu bar as shown in the Figure 9.

Figure 9: Compiling the C++ program

When you click on build solution, the compiler would start processing your program. The progress of
build activity would be displayed on the output window as shown in the Figure 10.

Figure 10: Build Activity

The progress would show whether the build was successful or not. It would display the errors if any.
Otherwise it would display the message Build succeeded
.
6.3.3 Executing the Program

Now run the program by pressing Ctrl+F5 from keyboard or selecting play option from Debug menu
(Debug menu can be accessed from the Figure 10).When you press Ctrl+F5, the compiler would start
executing the program and would display the final output to your screen as shown in the Figure 11.
*
*

**********
Hello
*
World *

Figure 11: Final output


Department of Computer Science,
MAJU, 2014

P age |9

Lab 1: Formatting using Escape Sequences

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
your lab instructor.
7.1 Practice Task 1

[Expected time = 10 mins]

Your need to print the formatted output like shown below:


$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$****************************$
***********\\\WELCOME to CP*******
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
7.2 Practice Task 2

[Expected time = 10 mins]

Your need to print the formatted output like shown below:


*** My Introduction
*Name: Your Name
*DOB: Your Birthdate
*City: Your city
*Prog: Your Prog
*
The End

***
*
*
*
*
*

7.3 Practice Task 3

[Expected time = 10 mins]

Write a C++ program which would print the following screen as output:
Course Code |
CS1114
|
CS1234
|
CS1123
|
CS1121
|
CS1124
|
Total
|

Course Name
ITC
Computer Programming
Cal-I
Algebra
Discrete Maths
5

| Credit Hour
| 4
| 4
| 4
| 3
| 3
|18

7.4 Practice Task 4

[Expected time = 10 mins]

Write a C++ program which would print the following screen as output:
Name
ENo
Dept
Rank
Sal

|
|
|
|
|

Zubair
1234
EE
Lecturer
12,000

7.5 Practice Task 5

[Expected time = 10 mins]

Write a C++ program which would print the following screen as output:
Month | Num of Hours
Department of Computer Science,
MAJU, 2014

P a g e | 10

Lab 1: Formatting using Escape Sequences


Jan
Feb
March
April
May
June

|
|
|
|
|
|

100
150
130
90
110
135

7.6 Practice Task 6

[Expected time = 10 mins]

Write a C++ program which would print the following screen as output:
Allownces
House Rent
Medical
TA/DA
Bonus

|
|
|
|
|

Amount
1000
1500
2000
50000

7.7 Practice Task 7

[Expected time = 10 mins]

Write a program in C++ using escape sequence which would produce the sound of a bell when user
enters the character.
7.8 Out comes

The outcomes of this lab were:


a) You have learnt escape sequences in C++
b) You have practiced escape sequences in C++
In this Lab, you have learned that how the escape sequences are used to format the output
7.9 Testing

For this task, no test cases are required. The instructor will check the outputs of the practice tasks.
Table 2: Testing of Practice Tasks
Practice Tasks
T1
T2
T3
T4
T5
T6
T7

8. Evaluation Task(Unseen)

Confirmation

[Expected time = 60mins]

The lab instructor will give you unseen task depending upon the progress of the class.

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).
Department of Computer Science,
MAJU, 2014

P a g e | 11

Lab 1: Formatting using Escape Sequences

Sr. No.
1
2
3
4
5
6

Task No
4.1
5
6 and 7
8

Table 3: Evaluation of the Lab


Description
Marks
Problem Modeling
20
Procedures and Tools
10
Practice tasks and Testing
35
Evaluation Tasks (Unseen)
20
Comments
5
Good Programming Practices
10

10. Further Readings


10.1 Books
Text Book:

Computer Programming by D.S Malik, second edition


Reference Books:

1. Beginning C++, the complete language, by Ivor Horton, Wrox Publishers.


2. How to Program in C++, Dietel and Dietel
10.2 Slides

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

Appendix A Escape Sequences


Escape
Represents
Sequence
\a
\b
\f
\n
\r
\t
\v
\'
\ "
\\
\?
\ooo
\xhh
\xhhhh

Bell (alert)
Backspace
Formfeed
New line
Carriage return
Horizontal tab
Vertical tab
Single quotation mark
Double quotation mark
Backslash
Literal question mark
ASCII character in octal notation
ASCII character in hexadecimal notation
Unicode character in hexadecimal notation if this escape sequence
is used in a wide-character constant or a Unicode string literal.
For example, WCHAR f = L'\x4e00' or WCHAR b[] = L"The Chinese
character for one is \x4e00".

Department of Computer Science,


MAJU, 2014

P a g e | 12

Potrebbero piacerti anche