Computer Programming LAB Manual
Computer Programming LAB Manual
Table of Contents
1.
Introduction
29
2.
Activity Time-boxing
29
3.
30
4.
Concept Map
30
4.1
4.2
4.3
30
30
31
5.
6.
7.
31
5.1
5.2
31
32
32
6.1
6.1
6.2
32
32
36
Tools
Setting-up Visual Studio 2008
Walk-through Task
Practice Tasks
38
7.1
7.2
7.3
7.4
7.5
7.6
38
38
38
38
38
39
Practice Task 1
Practice Task 2
Practice Task 3
Out comes
Testing
Evaluation Task (Unseen)
8.
Evaluation Criteria
39
9.
Further Readings
40
9.1
9.2
40
40
Books
Slides
40
P a g e | 28
2. Activity Time-boxing
Task No.
5.1
6.2
6.3
7
9
Total Time
20 mins
5 mins
15 mins
70 mins
60 mins
170 mins
P a g e | 29
4. Concept Map
We will review some important concepts regarding selections (conditions) in this section. The selection
is used to control the execution of your program and to make number of decisions. You will learn along
with examples about this.
4.1 One way Selection
Sometimes, we are only interested to make a decision for example, acquiring absolute value of a
number. In this case we just need to convert the negative number to the positive. However, if the
number is already positive then we do not need to do anything.
if (number<0)
number= -1 * number;
Apart from the one-way selection, we need to tackle both the cases. For example if the marks of a
student are greater than 50, then the student is passed otherwise the student is failed.
if (marks>50)
cout<<Passed;
else
cout<< failed;
You can use as many if-statements after the else part, for example, consider the following code:
if (marks>90)
cout<<Grade is A;
else if (marks>85)
cout<< Grade is B;
else
cout<< Fail;
The above statement will output the grade A for all students scoring more than 90 marks. However,
the grade of the students will be B who have scored more than 85 marks and less than 90, similarly, the
remaining students will get F grade. The statement will be executed in a way that first statement will be
checked, if the students marks are greater than 90, then the grade will be shown as A and the
remaining parts will not be executed (The else part). If the students marks are less than 90, then the first
statement would be evaluated as false, and the control will be passed to the else if (marks >85)
statement.
P a g e | 30
In the above statements, we have used only one statement that would be executed after the true of
false condition. However, most of the times, we need to execute multiple statements after the condition
evaluated as true or false. For example, consider the following statement:
if (marks>90)
cout<<Grade is A;
cout<<congratulations, you have performed excellent;
If the marks are more than 90, the program will display the following output:
Grade is A
Congratulations, you have performed excellent
However, suppose if the marks are less than 90, then guess what you will get:
Nothing .
Surprisingly. This will not happen; however, for the marks less than 90, the following would be printed
on screen:
Congratulations, you have performed excellent
This is due to the fact that c++ considers only one statement as associated with the condition, and the
statement
cout<<congratulations, you have performed excellent
is not part of the if-statement, and as the if has been evaluated as false therefore the above statement
will be executed
To tackle this situation, we need to do it in the following way.
if (marks>90)
{
cout<<Grade is A;
cout<<congratulations, you have performed excellent;
}
Now both statements will be associated with the if-statement. In case the if-statement is false, none of
the above statements will be executed.
After reading the reference material mentioned in the introduction, now you are ready to
Department of Computer Science,
MAJU, 2013
P a g e | 31
2. Write a program to input five number from user and check the number are divisible by 3 then
print the message on the screen that the number is divisible by three. Otherwise print The
number is not divisible by three.
5.2 Practices from home
1. Write a program that asks the user for two numbers and then print the maximum and minimum
number to the user.
2. Write a program that displays a menu to the user. Press 1 to find a sum of the numbers given by
the user Press 2 to find whether the number is even or odd
1) Go to start menu.
2) Click on Microsoft Visual Studio Folder.
3) Now select Microsoft Visual Studio as shown in the Figure 1.
1) Go to File menu.
2) Select New, a sub-window will open as shown in the Figure 2.
Department of Computer Science,
MAJU, 2013
P a g e | 32
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, DLL etc as shown in the Figure 3 (a).
3) From Application Type, Select Console Application as Figure 4 (b).
4) From additional options, select empty project as shown in the Figure 3 (b).
P a g e | 33
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.
Various options would appear. Click on add New Item to select a working file as show in the Figure 5.
P a g e | 34
From the various options, click on C++ File (.cpp) as shown in the Figure 6.
.
Figure 6: Select CPP file
Write the name of your file in the Name field as shown in the Figure 7.
P a g e | 35
When you click Add button, A new file would be added to your existing project as shown in the Figure7.
6.2
Walk-through Task
After completing 6.2 task, 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 selections and relational operators.
6.2.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.
P a g e | 36
6.2.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.
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.2.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.
P a g e | 37
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
Write a program that asks the user to input 5 numbers and find its average. Once you find the average
then compare that average with all inputs and count the numbers which are greater than average.
Write a program that takes the marks from the user and tells the grade corresponding to the marks. The
marks and corresponding grades are mentioned below:
Marks
Greater than 90
Greater than or equal to 86 and less than 90
Greater than or equal to 81 and less than 86
Greater than or equal to 77 and less than 81
Greater than or equal to 72 and less than 77
Greater than or equal to 68 and less than 72
Greater than or equal to 63 and less than 68
Greater than or equal to 58 and less than 63
Greater than or equal to 54 and less than 58
Greater than or equal to 50 and less than 54
Below 50
Grades
A
AB+
B
BC+
C
CD+
D
F
Write a program which reads salaries of 10 employees of an organization. The program will tell, what
the maximum salary is and what the minimum salary is
7.5 Testing
Now you need to perform the following test cases for all practice tasks mentioned above. The test cases
have been made available in Table 2.
P a g e | 38
Input
Num1 = 30
Num2= 5
Output
Line 1: a is not equal to b
Line 2: a is not less than b
Line 3: a is greater than b
Line 4: a is not equal to b
7.1
Num1=20
Num2 = 25
Num3= 63
Num4 = 80
Num5 = 95
Num6 = 85
Num7 = 79
Num8= 58
Num9=25
Num10=101
99
76
49
79
83
Salary1=50000
Salary2=45000
Salary3=39000
Salary4=99000
Salary5=12000
Salary6=58000
Salary7=54000
Salary8=25000
Salary9=10000
Salary10=10400
Average = 54
Total number greater than average = 6
7.2
7.3
7.6
Confirm
A
BF
B
B+
Maximum salary is = 99000
Minimum salary = 10000
The lab instructor will give you unseen task depending upon the progress of the class.
8. 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).
P a g e | 39
Sr. No.
1
2
3
4
5
6
Task No
5.1
6
7 and 8
9
Description
Problem Modeling
Procedures and Tools
Practice tasks and Testing
Evaluation Tasks (Unseen)
Comments
Good Programming Practices
Marks
20
10
70
20
5
10
9. Further Readings
9.1 Books
Text Book:
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\
Description
Example
==
(A == B) is not true.
!=
(A != B) is true.
>
<
>=
<=
P a g e | 40