programming lab 1

Upload: shahid-khattak

Post on 03-Mar-2016

42 views

Category:

Documents


0 download

DESCRIPTION

enjoy

TRANSCRIPT

  • Mohammad Ali Jinnah University Islamabad Department of Computer Science,

    Faculty of Computing

    Lab 1: Formatting using Escape Sequences

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 1

    Table of Contents

    1. Introduction 2

    2. Activity Time-boxing 2

    3. Objective of the Experiment 2

    4. Concept Map 3

    4.1 Printing characters which are not part of a Keyboard 3 4.2 Printing special characters 3 4.3 Generating Sounds 3

    5. Home work before Lab 3

    5.1 Problem solution modeling 3 5.2 Practices from home 4

    6. Procedure & Tools 4

    6.1 Tools 4 6.2 Setting-up Visual Studio 2008 4 6.3 Walk-through Task 8

    7. Practice Tasks 10

    7.1 Practice Task 1 10 7.2 Practice Task 2 10 7.3 Practice Task 3 10 7.4 Practice Task 4 10 7.5 Practice Task 5 10 7.6 Practice Task 6 11 7.7 Practice Task 7 11 7.8 Out comes 11 7.9 Testing 11

    8. Evaluation Task (Unseen) 11

    9. Evaluation Criteria 11

    10.Further Readings 12

    10.1 Books 12 10.2 Slides 12

    Appendix A Escape Sequences 12

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 2

    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

    Table 1: Activity Time Boxing

    Task No. Activity Name Activity time Total Time

    8 Evaluation Task 30mins for each assigned task 60 mins

    5.1 Evaluation of Design 20 mins 20 mins

    6.2 Setting-up Visual Studio 5 mins 5 mins

    6.3 Specialized Tasks 15mins 15mins

    7 Practice tasks 10 mins for each task 70 mins

    Total Time 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.

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 3

    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

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 4

    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.

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 5

    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

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 6

    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.

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 7

    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.

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 8

    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

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 9

    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.

    Figure 11: Final output

    **********

    * Hello *

    * World *

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 10

    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 | Course Name | Credit Hour

    CS1114 | ITC | 4

    CS1234 | Computer Programming | 4

    CS1123 | Cal-I | 4

    CS1121 | Algebra | 3

    CS1124 | Discrete Maths | 3

    Total | 5 |18

    7.4 Practice Task 4 [Expected time = 10 mins]

    Write a C++ program which would print the following screen as output: Name | Zubair

    ENo | 1234

    Dept | EE

    Rank | Lecturer

    Sal | 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

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 11

    Jan | 100

    Feb | 150

    March | 130

    April | 90

    May | 110

    June | 135

    7.6 Practice Task 6 [Expected time = 10 mins]

    Write a C++ program which would print the following screen as output: Allownces | Amount

    House Rent | 1000

    Medical | 1500

    TA/DA | 2000

    Bonus | 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 Confirmation

    T1

    T2

    T3

    T4

    T5

    T6

    T7

    8. Evaluation Task(Unseen) [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).

  • Lab 1: Formatting using Escape Sequences

    Department of Computer Science,

    MAJU, 2014

    P a g e | 12

    Table 3: Evaluation of the Lab

    Sr. No. Task No Description Marks

    1 4.1 Problem Modeling 20

    2 5 Procedures and Tools 10

    3 6 and 7 Practice tasks and Testing 35

    4 8 Evaluation Tasks (Unseen) 20

    5 Comments 5

    6 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

    Sequence Represents

    \a Bell (alert)

    \b Backspace

    \f Formfeed

    \n New line

    \r Carriage return

    \t Horizontal tab

    \v Vertical tab

    \' Single quotation mark

    \ " Double quotation mark

    \\ Backslash

    \? Literal question mark

    \ooo ASCII character in octal notation

    \xhh ASCII character in hexadecimal notation

    \xhhhh

    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".