Lab Manual in Computer Programming 1
Lab Manual in Computer Programming 1
LABORATORY MANUAL in
Computer Programming 1
PREFACE
This laboratory manual was designed and prepared for students who have
little or no background in computer programming. It provides the foundational skill
set required to write computer programs using C++ programming language.
It was likewise developed in conjunction with the lessons in the Learning
Module in Computer Programming 1. It consists of guided activities and exercises
that were constructively aligned with the learning outcomes.
This laboratory manual was carefully designed to help students become
effective self-directed learners, hence the inclusion of guided activities and rubrics
for the exercises for them to take charge of their own learning process and
evaluate their own learning performance.
ii
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Table of Contents
iii
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Laboratory Exercise #1
Learning Outcomes:
Guided Activity:
To help you get started with C++ programming, follow the steps below.
DevC++Installation
1. Download DevC++ in https://sourceforge.net/projects/orwelldevcpp/.
2. Click the appropriate download button. Wait until downloading of files is successful.
3. Double-click the downloaded file and then follow the instructions.
DevC++ Setup
1. Open DevC++.
This directs you to the Home page of DevC++ as shown below.
2. By default, the most recent version of C++ is not enabled. Hence, it must be
explicitly enabled. Do the following:
1
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Coding in DevC++
2
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
int main()
{
std::cout << "Welcome to C++ Programming!";
}
3. Save the sample C++ program by clicking File> Save As. Use “Welcome” as
filename. Make sure that the file type is C++ source files, then click Save.
4. To view the output, you must compile and run the sample program. To compile
and run the program, do any of the following:
i. Click Execute> Compile & Run, or
ii. Press F11.
You should be able to see the output as shown below:
5. Close the output window or simply press any key to go back to the source code
editor. Try deleting the semicolon (;) located at the end of line 6.
6. Press F11 to compile and run the program.
Note that the compiler catches errors. This generates warnings or error messages
so you can easily recognize and correct the problem. Warnings or error messages are
displayed in the Compiler window as shown below:
3
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
When the compilation is unsuccessful, check the error message as this will give you a
hint on what is wrong in your program.
7. Bring back the semicolon and then compile and run the program again. This will
give you zero errors and warnings.
Below are the steps to follow for setting up the Visual Studio Code for C++
programming.
2. Install the Microsoft C/C++ Extension which is used for debugging and intellisense.
Do the following:
4
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
3. Install the Code Runner Extension which will be used for running programs. Do the
following:
4. The C/C++ Extension does not include the C++ compiler or debugger. For MacOS,
the compiler is Clang. To verify that Clang is installed, go to Terminal window and
enter the command below:
clang --version
If clang is not installed, you may have to download Xcode in your App
Store. After which, enter the following command to install the command
line developer tools:
xcode-select --install
Afterwhich, restart your computer.
You are now ready to program in Visual Studio Code. Do the following:
4. Click File> Save Workspace As. Name your workspace as “C++ Programming”. (or
you may choose any for that matter)
5. You can now create a new file by clicking the New File icon.
6. Type “Welcome” as your new file.
This opens the source code editor.
5
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
7. Click Plain Text located at the lower right corner of the window.
8. Select C++ (cpp).
6
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
int main()
{
std::cout << "Welcome to C++ Programming!";
}
10. To view the output, just click Run Code button located at the upper right
corner of the window.
The output appears at the output window below the source code editor as shown
below:
11. To clear everything in the output window, just click Clear Output button.
12. Now, try deleting the semicolon (;) located at the end of line 6 and save.
13. Click Run Code again.
Notice that an error message is displayed in the Output window as shown below:
When the compilation is unsuccessful, check the error message as this will give
you a hint on what is wrong in your program.
14. Bring back the semicolon, and then compile and run the program again. This will
give you zero errors and warnings.
You can use CxxDroid to program in C++ using your android mobile phones.
CxxDroid Installation
7
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
To start programming in C++, you may have to first install the CxxDroid in your
mobile phone. Just search CxxDroid in the Play Store, then tap Install. After installation,
click Open.
After the installation of CxxDroid, this brings you to its Source Code Editor, plus
the Menu in the Title Bar and the Play button as shown below.
Menu
Play
Coding in CxxDroid
#include <iostream>
int main()
{
std::cout << "welcome";
}
8
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Exercises:
A. Type the given C++ programs using any IDE. Save these programs using the suggested
filename in each item.
a. Filename> myprogram1
#include <iostream>
int main()
{
std::cout << "I love programming!";
}
9
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
b. Filename> myprogram2
#include <iostream>
int main()
{
std::cout << "Hello ";
std::cout << "Universe!";
}
c. Filename> myprogram3
#include <iostream>
using namespace std;
int main()
{
cout << "Hello Universe"; // prints Hello Universe
}
B. Give the output of the given C++ programs above. Use the space provided.
myprogram1
myprogram2
myprogram3
10
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Laboratory Exercise #2
Basic Elements of C++
• C++ Program Structure
• Variables, Literals, Constants, Data Types
Learning Outcomes:
Guided Activity:
1. Type the given program below using your preferred IDE for C++.
#include <iostream>
using namespace std;
cout << " I love C++!";
{
int main()
cout << " Programming is real!";
}
int main()
cout << " I love C++!";
cout << " Programming is real!";
}
int main()
{
cout << " I love C++!";
cout << " Programming is real!";
}
11
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
8. Compile and run the program. This will give you an output as shown below:
I love C++!
Programming is real!
9. Alternatively, the endl manipulator can also be used to break lines. For example:
int main()
{
cout <<" I lov C++!";
cout <<" Programming is ral!";
}
int main ()
{
int a, b;
int diff;
a = 500;
b = 200;
a = a + 1;
diff = a - b;
Lines 6 and 7 show the straightforward syntax for variable declaration. Specifically,
these are examples of local variables. The variables a, b and diff have the same data
12
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
type which is integer. This means that these variables can hold integer values. If you
declare more than one variable of the same type, they can all be declared in a single
statement by separating their identifiers with commas as shown in Line 6. You may also
opt to separate them in another statement just like in Line 7.
14. Compile and run the program. This will give you an output as shown below:
This reminds you to always declare your variables before using them.
17. Edit your program into:
#include <iostream>
using namespace std;
int main ()
{
int a, b;
int out;
string sample;
a = 500;
b = 200;
a = a + 1;
out = a - b;
sample="The output is ";
cout << sample;
cout << out;
}
a = 500.5;
Then, compile and run the program. This will give you an output similar to item 18. The
program has no error, but it is logically or semantically incorrect since only the integer
portion of the value of a was considered. This reminds you to always check the
appropriate data types of your variables.
20. Edit Lines 6 and 7 into:
float a, b;
float out;
21. Then, compile and run the program again. This will give you an output as shown below:
13
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
int main ()
{
int length, width;
#define length 10;
width = 15;
Exercises:
14
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Rubrics:
15
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Laboratory Exercise #3
Basic Elements of C++
• Basic Input/Output Statements
• C++ Operators
Learning Outcomes:
Guided Activity:
int main ()
{
int num;
The program introduces the cin>> function that reads a value into the variable num.
2. Compile and run the program.
3. Then, enter 100 as input. You must press enter before the number is read by the
program. The output will be:
16
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Line 11 demonstrates the use of multiplication operator while Line 13 demonstrates the
use of the increment operator. On the other hand, Line 15 uses compound assignment
operator (+=) which is equivalent to num = num + 50.
6. Compile and run the program. Assuming that your input is 100, this will give an output
of:
Exercises:
1. Create a C++ program that lets the user enter his complete name.
2. Create a C++ program that lets the user enter his first name and last name, then
displays both in one line with a space in between.
3. Create a C++ program that computes the average of three input numbers.
4. Create a C++ program that displays the perimeter and area of a rectangle given the
length and width as inputs.
5. Create a simple point-of-sale program in C++ that allows you to enter three product
names and their corresponding prices then computes for the total amount, and the
change after inputting the amount tendered. Sample output is shown below:
Rubrics:
17
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
18
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Laboratory Exercise #4
Selection Control Statements
• if Statement
• if-else Statement
Learning Outcomes:
Guided Activity:
int main()
{
int num;
19
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
The program demonstrates the use of if with two conditions. The message in line 11 will
be displayed in case the input stored in num is equal to 5. Otherwise, the message in
line 13 will be displayed.
7. Edit the code fragment of if into:
if (num > 5)
cout << "The number you entered is greater than 5.";
else if (num<5)
cout << "The number you entered is less than 5.";
else
cout << "The number you entered is equal to 5.";
8. Compile and run the program.
The program demonstrates the use of several if-else structures. The message in line 11
will be displayed in case the input stored in num is greater than 5. The message in line
13 will be displayed in case the input stored in num is less than 5. Otherwise, the
message in line 15 will be displayed.
Exercises:
1. Create a C++ program that displays the “Hello ” plus the input name if the input course
is BSIT and ends when it is not BSIT.
2. Create a C++ program to input student name and grades in three subjects. Display the
student name, the average grade, and the subject with the highest grade.
3. Tracy’s ShoeStore is giving an early Christmas sale on all items in their store. Create a
program that inputs the total amount purchased. The program should then compute
and display the discount price and the amount to be paid by the customer based on
the following discount rate:
Total Amount Purchased Discount Rate
1000 – 1500 5%
1501 – 2000 8%
2001 - 3000 12%
3001 – 4000 15%
4001 and above 18%
4. Create a C++ program that checks whether a 3-digit integer input is Armstrong or not.
An Armstrong number of three digits is an integer such that the sum of the cubes of its
digits is equal to the number itself. For example 153 = 13 + 53 + 33.
5. Create a C++ program that transmutes an input grade into the following:
Grade Transmutation
98-100 1.0
95-97 1.25
92-94 1.5
89-91 1.75
86-88 2.0
83-85 2.25
80-82 2.5
77-79 2.75
76-75 3.0
Below 75 5.0
20
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Rubrics:
21
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Laboratory Exercise #5
Selection Control Statements
• Nested if and if-else Statements
• switch Statement
Learning Outcomes:
1. Use the correct syntax of nested if and if-else statements, and switch statement
2. Apply nested if and if-else statements, and switch statement in a C++ program.
Guided Activity:
int main()
{
int num;
cout<<"Enter any integer: ";
cin>> num;
if (num > 100)
{
if ((num%2)==0)
cout << "The number is even and greater that 100.";
else
cout << "The number is odd and greater that 100.";
}
}
3. Try running the program and entering an input integer as many times as you can to
check the output.
22
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
int main()
{
int num;
cout<<"Enter any integer: ";
cin>> num;
if (num > 100)
{
if ((num%2)==0)
cout << "The number is even and greater that 100.";
else
cout << "The number is odd and greater that 100.";
}
else
{
if ((num%2)!=0)
cout << "The number is odd and less than or equal to
100.";
else
cout << "The number is even and less than or equal to
100.";
}
}
23
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter any integer from 1 to 3: ";
cin>>n;
switch (n)
{
case 1:
cout << "input is 1";
break;
case 2:
cout << "input is 2";
break;
case 3:
cout << "input is 3";
break;
default:
cout << "input out of range \n";
}
}
Exercises:
1. Allows the user to enter a name and course. If the course is BSIT, it will display “Hello ”
plus the input name, otherwise the program ends. If the course is BSIT, it will prompt the
user to enter a section, then it displays “You belong to Section ” plus the corresponding
section name below:
24
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Sample Outputs:
Enter name: Juan Dela Cruz Enter name: Maria Dela Fuente
Enter Course: BSIT Enter Course: BSCS
Enter Section: A
You belong to Section Chrome!
2. Create a C++ program that checks whether an input number is positive or negative. If
positive, check whether the input number is odd or even.
B. Create a C++ program applying the switch statement that transmutes an input grade
into the following:
Grade Transmutation
98-100 1.0
95-97 1.25
92-94 1.5
89-91 1.75
86-88 2.0
83-85 2.25
80-82 2.5
77-79 2.75
76-75 3.0
Below 75 5.0
Rubrics:
25
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Laboratory Exercise #6
Learning Outcomes:
Guided Activity:
1. Observe how for-loop works. Type the given program in your IDE.
#include <iostream>
using namespace std;
int main ()
{
for (int n=1; n<=10; n++)
{
cout << n << ", ";
}
cout << "FINISHED!\n\n";
}
26
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
#include <iostream>
using namespace std;
int main ()
{
int n,i;
int main ()
{
for (int n=5; n>=1; n--)
{
for (int i=5; i>=1; i--)
{
cout << i ;
}
cout << endl;
}
}
Exercises:
Create a C++ program applying the for statement for the following problems:
27
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
3. A simple point-of-sale program in C++ that allows you to enter three product names
and their corresponding prices then computes for the total amount, and the change
after inputting the amount tendered.
4. Allows an integer input and displays skip counting by the integer input up to the number
before it exceeds 100. Sample outputs are given below:
20 40 60 80 100 24 48 72 96
Rubrics:
28
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Laboratory Exercise #7
Learning Outcomes:
Guided Activity:
1. Observe how while-loop works. Type the given program in your IDE.
#include <iostream>
using namespace std;
int main ()
{
int n=1;
while (n<=10)
{
cout << n << ", ";
n++;
}
cout << "FINISHED!\n\n";
}
2. Compile and run the program.
The program demonstrates the use of a simple while statement. The variable n is set to
1 and while n is less than or equal to 10 it calls cout << n << ", "; then it adds 1 to n as
long as the condition is met.
3. Edit lines 6 and 7 into:
int n=10;
while (n>=1)
4. Edit line 10 into: n--;
5. Compile and run the program.
The while loop sets n to 10. While n is greater than or equal to 1, it calls cout << n << ",
"; and it subtracts 1 to n as long as the condition is met. Take note that while-loop should
end at some point by making the condition false. Otherwise, the loop will continue
looping forever. This is called endless loop.
6. Try this program:
29
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
#include <iostream>
using namespace std;
int main ()
{
int n=1;
int i=1;
while (n<=5)
{
while (i<=5)
{
cout <<"X";
i++;}
n++;
i=1;
cout << endl;
}
}
Exercises:
Create a C++ program applying the while statement for the following problems:
20 40 60 80 100 24 48 72 96
Rubrics:
30
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
31
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Laboratory Exercise #8
Learning Outcomes:
Guided Activity:
1. Observe how do-while loop works. Type the given program in your IDE.
#include <iostream>
using namespace std;
int main ()
{
int n=1;
do
{
cout << n << ", ";
n++;
}
while (n<=10);
cout << "FINISHED!\n\n";
}
32
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
#include <iostream>
using namespace std;
int main ()
{
int n=1;
int i=1;
do
{
do
{
cout <<"X";
i++;
} while (i<=5);
n++;
i=1;
cout << endl;
} while (n<=5);
}
Exercises:
Create a C++ program applying the do-while statement for the following problems:
20 40 60 80 100 24 48 72 96
Rubrics:
33
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
34
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Laboratory Exercise #9
Functions
• C++ Library Functions
• User-Defined Functions
Learning Outcomes:
Guided Activity:
1. Observe how C++ functions work. Open your IDE and try this.
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int main ()
{
int x=10,y=2;
char fname[]="Tracy", lname[]="Lopez";
35
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
#include <iostream>
using namespace std;
void myfunction()
{
cout << "This is under myfunction." << endl;
}
int main()
{
cout << "This is under the main function.";
cout << endl;
myfunction();
cout << "End of main function." << endl;
}
int returnvalue()
{
return 100;
}
int main()
{
cout << returnvalue() <<endl;
cout << returnvalue() + 200 <<endl;
returnvalue();
}
36
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
line 13, returnvalue() is gain called so the function returns the value of 100 back to
main() at line 13. The return value is not sent to cout, so nothing is displayed.
8. Insert this statement after line 6:
cout<<"Returned";
9. Compile and run the program.
Notice that line 7 is not printed since a return statement is reached first and the function
returns back to main() immediately ignoring all other statements in the function.
Exercises:
1. Create a C++ program that applies the use of a function with no parameters and no
return values to:
a. Display the input name and age.
b. Compute for the average of three input grades.
c. Display the Fibonacci Series up to n number of terms where n is an input from the
user.
2. Create a C++ program that applies the use of a function with no parameters but with
return values to:
a. Display the input name and age.
b. Compute for the average of three input grades.
c. Display the Fibonacci Series up to n number of terms where n is an input from
the user.
Rubrics:
37
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
38
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Learning Outcomes:
1. Use the correct syntax of functions with parameters but with no return values
2. Apply functions with parameters but with no return values in a C++ program
Guided Activity:
int main()
{
int a, b;
cout<<"Enter first number:"; cin>>a;
cout<<"Enter first number:"; cin>>b;
sum (a, b);
}
39
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
#include<iostream>
using namespace std;
int main()
{
int a, b;
cout<<"Enter first number:"; cin>>a;
cout<<"Enter first number:"; cin>>b;
sum (a,b);
diff (a,b);
}
Exercises:
Create a C++ program that applies the use of a function with parameters but with no
return values to:
40
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Rubrics:
41
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Functions
• Functions with parameters and return values
Learning Outcomes:
1. Use the correct syntax of functions with parameters and return values
2. Apply functions with parameters and return values in a C++ program
Guided Activity:
int main()
{
int a, b;
cout<<"Enter first number:"; cin>>a;
cout<<"Enter first number:"; cin>>b;
cout<<"The sum is "<<sum (a, b);
}
42
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
#include<iostream>
using namespace std;
int main()
{
int a, b;
cout<<"Enter first number:"; cin>>a;
cout<<"Enter first number:"; cin>>b;
cout<<"The sum of two integers:" <<sum(a,b) <<endl;
cout<<"The difference of two integers:" <<diff(a,b) <<endl;
}
Exercises:
Create a C++ program that applies the use of a function with parameters and return
values to:
43
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Rubrics:
44
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Recursive Functions
Learning Outcomes:
Guided Activity:
void sample(int n)
{
if (n < 1)
return;
else {
cout << n << " ";
sample(n - 1);
}
}
int main()
{
int n;
cout <<"Enter any integer: ";
cin >> n;
sample(n);
}
45
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
#include <iostream>
using namespace std;
int main()
{
int f,n;
cout <<"Enter any integer: ";
cin >> n;
f=sample(n);
cout << "The factorial of " <<n <<"is "<<f;
}
Exercises:
1. Create a C++ program which has a recursive void function that has one parameter
which is a positive integer and that displays out that number of hashtags ’#’ to the
screen all on one line.
2. Create a C++ program which has a recursive void function that takes a single int
argument n and displays the integers 1, 2, ..., n.
3. Create a C++ program using recursive function that displays the sum of natural
numbers. Sample output is given below:
Sum = 6
46
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
Rubrics:
47
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT
REFERENCES
Print:
Online:
• www.learncpp.com
• www.cprogramming.com
• www.tutoriapoint.com
• www.codesdope.com
• www.cpp.thiyagaraaj.com
• www.codescracker.com
• www.ajune-programming.blogspot.com
• www.studytonight.com
• www.codescracker.com
Image Sources:
• Front Cover
• Learning Outcomes
http://clipart-library.com/clipart/46163.htm
• Guided Activity
http://clipart-library.com/clipart/pio5oy4oT.htm
• Exercises
https://webstockreview.net/pict/getfirst
48