100% found this document useful (1 vote)
631 views51 pages

Lab Manual in Computer Programming 1

Programming for 1st year college
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
631 views51 pages

Lab Manual in Computer Programming 1

Programming for 1st year college
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 51

College of Computing Studies, Information and Communication Technology

Isabela State University


Cabagan, Isabela
© 2021

LABORATORY MANUAL in
Computer Programming 1

Ivy M. Tarun, DIT


Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

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.

Ivy M. Tarun, DIT


Author

ii
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Table of Contents

Lab Exercise No. Title Page


1 Integrated Development Environment for C++ 1
2 Basic Elements of C++ 11
• C++ Program Structure
• Variables, Literals, Constants
• Data Types
3 Basic Elements of C++ 16
• Basic Input/Output Statements
• C++ Operators
4 Selection Control Statements 19
• if Statement
• if-else Statement
5 Selection Control Statements 22
• Nested if and if-else Statement
• switch Statement
6 Repetition Control Statements 26
• for Statement
7 Repetition Control Statements 29
• while Statement
8 Repetition Control Statements 32
• do-while Statement
9 Functions 35
• C++ Library Functions
• User-Defined Functions
10 Functions 39
• Functions with parameters but with no return
values
11 Functions 42
• Functions with parameters and return values
12 Functions 45
• Recursive Functions
REFERENCES 48

iii
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Laboratory Exercise #1

Integrated Development Environment for C++

Learning Outcomes:

1. Use an IDE as text editor for C++ programming; and


2. Compile and run sample C++ programs using the desired IDE.

Guided Activity:

To help you get started with C++ programming, follow the steps below.

A. Using DevC++ for Windows

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

a. Click Tools> Compiler


Options.
b. Click Settings> Code
Generation.
c. Under Language
Standard, select ISO C++
11.

Coding in DevC++

You are now ready to program in DevC++. Do the following:

1. Click File> New> Source File.


Source code editor appears as shown below:

Source Code Editor

2. Type the sample C++ program below.

2
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

// my first program in C++


#include <iostream>

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.

B. Using Visual Studio Code for macOS

Visual Studio Code Installation

1. Download Visual Studio Code in https://code.visualstudio.com/download.


2. Click the download button appropriate for MacOS. Wait until downloading of files
is successful.
3. Double-click the downloaded file and then follow the instructions.

Visual Studio Code Setup

Below are the steps to follow for setting up the Visual Studio Code for C++
programming.

1. Open Visual Studio Code.


Visual Studio Code window appears as shown below.

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

a. Click the Extensions view icon


on the sidebar.
b. Search for C++.
c. Click Install.

3. Install the Code Runner Extension which will be used for running programs. Do the
following:

a. Click the Extensions view icon


on the sidebar.
b. Search for Code Runner.
c. Click Install.

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.

Coding in Visual Studio Code

You are now ready to program in Visual Studio Code. Do the following:

1. In Visual Studio Code, click File> Add Folder to Workspace.


2. Create a new folder named “C++ Files” inside the Documents folder.
3. Click Add.
This adds the C++ Files folder in your workspace as shown below.

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

9. Type the given C++ program.

6
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

// my first program in C++


#include <iostream>

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.

C. Using CxxDroid for mobile phones

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

Source Code Editor

Play

Coding in CxxDroid

In CxxDroid, there is no need to setup anything anymore. You can immediately


start coding. To code, do the following:

1. Type the sample code below:

#include <iostream>
int main()
{
std::cout << "welcome";
}

8
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

2. Click the Play button to view the output.


This displays the output as shown below:

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.

C++ Source File Output

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:

1. Create simple C++ programs using the correct program structure


2. Apply variables, literals and constants in a C++ program

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!";
}

2. Compile and run the program.


Notice that error messages are displayed in the Compiler window since the correct
program structure for C++ was not followed.
3. Edit the given program into:
#include <iostream>
using namespace std;

int main()
cout << " I love C++!";
cout << " Programming is real!";
}

4. Compile and run the program.


Notice that error messages are again displayed in the Compiler window since the
correct syntax was not followed.
5. Edit the given program into:
#include <iostream>
using namespace std;

int main()
{
cout << " I love C++!";
cout << " Programming is real!";
}

11
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

6. Compile and run the program.


The program has successfully compiled the program, thus the two messages were
displayed on a single line. Take note that cout does not automatically add line breaks
at the end unless instructed. To do that in C++, a new-line character can be specified
using \n.
7. In your program, insert \n in line 6 as shown below:

cout << " I love C++!\n";

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:

cout << " I love C++!" << endl;

Try this in line 6 of your program.


10. Compile and run the program.
This will give you the same output as the previous.
11. Edit the given program into:
#include <iostream>
using namespace std;

int main()
{
cout <<" I lov C++!";
cout <<" Programming is ral!";
}

12. Compile and run the program.


The program demonstrates a syntactically valid program but does not do what the
programmer intends to see. This is called semantic error. In this example, there were
spelling mistakes which cannot be detected by the compiler.
13. Now, try another program given below:
#include <iostream>
using namespace std;

int main ()
{
int a, b;
int diff;

a = 500;
b = 200;
a = a + 1;
diff = a - b;

cout << diff;


}

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:

15. Edit Line 12 into:


output = a - b;

16. Compile and run the program.


This will give you an error as shown below:

[Error] 'output' was not declared in this scope

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;
}

The program introduces the string data type.


18. Compile and run the program. This will give the output as shown below:

19. Edit Line 10 into:

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

22. Edit your program into:


#include <iostream>
using namespace std;

int main ()
{
int length, width;
#define length 10;
width = 15;

cout << "length:"<< length;


cout << " width:"<< width<<"
";
}

23. Compile and run the program.


The program shows the definition of constant length which is 10 in line 7 and the use of
assignment operator in line 8. Line 8 is a statement that assigns the integer value 15 to
variable width. The output will be:

Exercises:

1. Create a C++ program that displays “Hello Universe!”.


2. Create a C++ program that displays your first name and middle name in the first line
with a tab in between, and your last name in the second line. Use the appropriate
escape sequences.
3. Create a C++ program that adds the numbers 100 and 200 applying the use of
variables.
4. Create a C++ program that adds the numbers 100 and 200 applying the use of
constants.
5. Create a C++ program that declares the following variables and prints its values:

Neym = Juan Dela Cruz


Age = 18
Course= BSIT
Year= First
Section=A
Grade= 95.43

14
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Rubrics:

Item Criteria Points Points Earned


1 • Program Readability 2
• Correctness of Output 3
2 • Program Readability 2
• Appropriateness of Escape Sequences 2
• Correctness of Output 3
3 • Program Readability 2
• Correct Use of Variables 2
• Correctness of Output 3
4 • Program Readability 2
• Correct Use of Constants 2
• Correctness of Output 3
5 • Program Readability 2
• Correct Use of Variables and Data Types 5
• Correctness of Output 3
TOTAL 36

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:

1. Create simple C++ programs using input and output statements


2. Apply the C++ operators in a program

Guided Activity:

1. Open your IDE and type the following program:


#include <iostream>
using namespace std;

int main ()
{
int num;

cout<<"Please enter an integer: ";


cin>> num;
cout<<"You entered: "<< num <<"\n";
}

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:

4. Edit line 10 of the program into:

cout<<"\tYou entered: "<< num <<"\n";


The line demonstrates the use of the escape codes where \t inserts a horizontal tab
and \n inserts a new line. You may try other escape sequences and check the output.
5. Insert the following lines of codes at lines 11 to 16:
num=num * 10;
cout<<"New value of num is: "<< num <<"\n";
num++;
cout<<"New value of num is: "<< num <<"\n";
num += 50;
cout<<"New value of num is: "<< num <<"\n";

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:

Product 1: Loaf Bread


Price: 75.00
Product 2: Cheese
Price: 95.00
Product 3: Ham
Price: 300.50
Total Amount: 470.50

Amount Tendered: 500


Change: 29.50

Rubrics:

Item Criteria Points Points Earned


1 • Program Readability 2
• Correctness of Output 3
2 • Program Readability 2
• Correctness of Output 3
3 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
4 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
5 • Program Readability 2

17
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Item Criteria Points Points Earned


• Correct Use of Variables and Data Types 3
• Correctness of Output 5
TOTAL 34

18
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Laboratory Exercise #4
Selection Control Statements
• if Statement
• if-else Statement

Learning Outcomes:

1. Use the correct syntax of if and if-else statements


2. Apply if and if-else statements in a C++ program.

Guided Activity:

1. Open your IDE and type the sample code below:


#include <iostream>
using namespace std;

int main()
{
int num;

cout<<"Enter any integer between 0-11: ";


cin>> num;
if (num == 5)
cout << "The number you entered is equal to 5.";
}

2. Compile and run the program.


The program demonstrates the use of simple if statement. The message in line 11 will
be displayed only if the input stored in num is equal to 5. Otherwise, the program will
end.
3. Edit the code fragment of if into:
if (num == 5)
{
cout << "The number you entered is equal to 5.\n";
cout << "Thank you!";
}
4. Compile and run the program.
The program demonstrates the use of if with compound statements. The messages in
lines 12 and 13 will be displayed only if the input stored in num is equal to 5. Otherwise,
the program will end.
5. Edit the code fragment of if into:
if (num == 5)
cout << "The number you entered is equal to 5.";
else
cout << "The number you entered is not equal to 5.";
6. Compile and run the program.

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:

Item Criteria Points Points Earned


1 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
2 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
3 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
4 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
5 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
TOTAL 67

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:

1. Open your IDE and type the given program below:


#include <iostream>
using namespace std;

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

2. Compile and run the program.


The given program demonstrates the use of nested if statement where line 12 will be
displayed if the input integer is greater than 100, and the remainder is 0 when the input
integer is divided by 2. If the input integer is less than or equal to 100, the program ends.
The possible outputs will be:

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

4. Edit the program into:


#include <iostream>
using namespace std;

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

5. Compile and run the program.


The given program demonstrates the use of nested if-else statement where the first
inner if-else will be executed if the input integer is greater than 100, otherwise the
second inner if-else will be executed.
6. Try running the program and entering an input integer as many times as you can to
check the output.
7. This time, you try switch statement. Type the program below:

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";
}
}

8. Compile and run the program.


The program demonstrates the use of switch statement.
9. Try the inputs 1, 2, 3 and 4 one at a time and check the output.
Remember if your input is a character data type, the case should be enclosed with
single quotation marks. Take note however that you cannot use string in switch
statement of C++. You may have to use if-else instead.

Exercises:

A. Create a C++ program applying the nested if-else statement that:

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:

A – Chrome B – Safari C - Firefox

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

Hello Juan Dela Cruz!

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:

Item Criteria Points Points Earned


A.1 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
A.2 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
B • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
TOTAL 45

25
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Laboratory Exercise #6

Repetition Control Statements


• for Statement

Learning Outcomes:

1. Use the correct syntax of for statement


2. Apply for statement in a C++ program

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";
}

2. Compile and run the program.


The program demonstrates the use of a simple for 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 line 6 into:
for (int n=10; n>=1; n--)
4. Compile and run the program.
The for 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.
5. Insert after line 5 this statement: int n=10;
6. Edit your for structure into: for (; n>=1; n--)
7. Compile and run the program.
The program demonstrates a for loop with no initialization. This is possible when the
variable is already initialized before the loop. Take note that each field in the for loop
can be left empty but in all cases the semicolon between them are required.
8. Try this program:

26
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

#include <iostream>
using namespace std;

int main ()
{
int n,i;

for (n=0, i=10 ; n!=i ; ++n, --i )


{
cout << "n="<<n << ", "<< "i="<<i << "\n";
}
cout << "FINISHED!\n\n";
}

9. Compile and run the program.


The program demonstrates a for loop with fields having more than a single expression.
It makes use of the comma operator to separate multiple expressions. The n starts with
a value of 0, and i with 10, the condition is n!=i (i.e., that n is not equal to i). Because n
is increased by one, and i decreased by one on each iteration, the loop's condition
will become false after the 5th iteration when both n and i are equal to 5.
10. Edit the program into:
#include <iostream>
using namespace std;

int main ()
{
for (int n=5; n>=1; n--)
{
for (int i=5; i>=1; i--)
{
cout << i ;
}
cout << endl;
}
}

11. Compile and run the program.


The program demonstrates a nested for-loop statement, a for-loop within another for-
loop. The inner for-loop will only be executed when the condition in the outer for-loop
is met.

Exercises:

Create a C++ program applying the for statement for the following problems:

1. Displays horizontally all even numbers from 2 to 20.


2. Displays an input name nth times, where n is an input integer.

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:

Enter an integer: 20 Enter an integer: 24

20 40 60 80 100 24 48 72 96

5. A multiplication table from 1 to 10.

Rubrics:

Item Criteria Points Points Earned


1 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
2 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
3 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
4 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
5 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
TOTAL 75

28
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Laboratory Exercise #7

Repetition Control Statements


• while Statement

Learning Outcomes:

1. Use the correct syntax of while statement


2. Apply while statement in a C++ program

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;
}
}

7. Compile and run the program.


The program demonstrates a nested while-loop statement, a while-loop within another
while-loop. The inner while-loop will only be executed when the condition in the outer
while-loop is met.

Exercises:

Create a C++ program applying the while statement for the following problems:

1. Displays horizontally all even numbers from 2 to 20.


2. Displays an input name nth times, where n is an input integer.
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:

Enter an integer: 20 Enter an integer: 24

20 40 60 80 100 24 48 72 96

5. A multiplication table from 1 to 10.

Rubrics:

Item Criteria Points Points Earned


1 • Program Readability 2
• Correct Use of Variables and Data types 2

30
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Item Criteria Points Points Earned


• Correctness of Output 3
• Correctness of Program Logic 8
2 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
3 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
4 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
5 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
TOTAL 75

31
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Laboratory Exercise #8

Repetition Control Statements


• do-while Statement

Learning Outcomes:

1. Use the correct syntax of do-while statement


2. Apply do-while statement in a C++ program

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";
}

2. Compile and run the program.


The program demonstrates the use of a simple do-while statement. The variable n is set
to 1 and while n is less than or equal to 10 it calls cout << n << ", "; and it adds 1 to n as
long as the condition is met.
3. Edit line 6 into:
int n=10;
4. Edit line 10 into:
n--;
5. Edit line 12 into:
while (n>=1)
6. 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.
7. Try this program:

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);
}

8. Compile and run the program.


The program demonstrates a nested do-while-loop statement, a do-while loop within
another do-while loop. The inner do-while loop will only be executed when the
condition in the outer do-while loop is met.

Exercises:

Create a C++ program applying the do-while statement for the following problems:

1. Displays horizontally all even numbers from 2 to 20.


2. Displays an input name nth times, where n is an input integer.
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:

Enter an integer: 20 Enter an integer: 24

20 40 60 80 100 24 48 72 96

5. A multiplication table from 1 to 10.

Rubrics:

Item Criteria Points Points Earned


1 • Program Readability 2
• Correct Use of Variables and Data types 2

33
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Item Criteria Points Points Earned


• Correctness of Output 3
• Correctness of Program Logic 8
2 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
3 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
4 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
5 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
TOTAL 75

34
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Laboratory Exercise #9
Functions
• C++ Library Functions
• User-Defined Functions

Learning Outcomes:

1. Use the correct syntax of library and user-defined functions


2. Apply user-defined functions in a C++ program

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

cout << "2 raise to 10 is "<< pow(2,10) <<endl;


cout << "My first name has " << strlen(fname) <<" letters.\n";
cout << "My complete name is " << strcat(fname,lname);
}

2. Compile and run the program.


The sample program demonstrates the use of library functions namely pow, strlen and
strcat. These are just few of the many library functions of C++. When using these
functions, do not forget to include their respective header files.
3. This time, you try user-defined functions as demonstrated by the given program:

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;
}

4. Compile and run the program.


5. The program demonstrates the use of a function that has no parameters and no return
values. The main function is called automatically, so lines 11 and 12 are executed. Then,
the function myfunction is called and execution of statements in main is suspended.
Void was used as the function’s data type as it does not return any value. The program
control then jumps to myfunction () where line 6 is executed. When myfunction()
terminates, main() resumes execution where it left off. In this case, line 14 will be
executed.
Remember that when you call a function, use the function name followed by an empty
set of parentheses.
6. Now, try another program given below:
#include <iostream>
using namespace std;

int returnvalue()
{
return 100;
}

int main()
{
cout << returnvalue() <<endl;
cout << returnvalue() + 200 <<endl;
returnvalue();
}

7. Compile and run the program.


The program demonstrates a function that has no parameters but returns a value. First,
main() is called and line 11 is executed which calls for returnvalue(). So, main() is
suspended and execution jumps to line 4. The return type in line 3 is integer, hence it
will return an integer value provided that a return statement is used to indicate the
specific value to be returned. In this case, line 6 which is a return statement is executed
and returns a value of 100 and passes this value to line 11. So, 100 is displayed. In a
similar manner, 100 is again passed at line 12 and added to 200. So, 300 is displayed. In

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:

Item Criteria Points Points Earned


1.a • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 5
1.b • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 5
1.c • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
2.a • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 5
2.b • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 5
2.c • Program Readability 2

37
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Item Criteria Points Points Earned


• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
TOTAL 78

38
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Laboratory Exercise #10


Functions
• Functions with parameters but with no return
values

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:

1. Open your IDE and try this.


#include<iostream>
using namespace std;

void sum(int x, int y)


{
int z;
z = x + y;
cout<<"The sum of two integers:" <<z;
}

int main()
{
int a, b;
cout<<"Enter first number:"; cin>>a;
cout<<"Enter first number:"; cin>>b;
sum (a, b);
}

2. Compile and run the program.


The sample program demonstrates the use of functions with parameters but with no
return values. Here, a and b at line 16 are sent as arguments, and x and y are
parameters which will hold values of a and b to perform required operation inside the
sum function. It has no return values, hence the data type of the function is void.
3. Edit your program into:

39
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

#include<iostream>
using namespace std;

void sum(int x, int y)


{
int z;
z = x + y;
cout<<"The sum of two integers:" <<z <<endl;
}

void diff(int x, int y)


{
int z;
z = x - y;
cout<<"The difference of two integers:" <<z <<endl;
}

int main()
{
int a, b;
cout<<"Enter first number:"; cin>>a;
cout<<"Enter first number:"; cin>>b;
sum (a,b);
diff (a,b);
}

4. Compile and run the program.


The sample program shows that you can have as many user-defined functions in a
program. Both the sum and diff functions return no values that is why their return type
is void. Here, a and b at line 23 are sent as arguments to x and y parameters of the
sum function. It will then compute for z at line 7 and then it will execute line 8 displaying
the string provided along with the value of z. The execution goes back to the main
function specifically at line 24. Values of a and b are passed to parameters x and y of
the diff function. It will then compute for z at line 14 and then it will execute line 15
displaying the string provided along with the value of z.

Exercises:

Create a C++ program that applies the use of a function with parameters but with no
return values to:

1. Display the input name and age.


2. Compute for the average of three input grades.
3. Print an input name 10 times.
4. Display the Fibonacci Series up to n number of terms where n is an input from the
user.

40
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Rubrics:

Item Criteria Points Points Earned


1 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 5
2 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 5
3 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
4 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
TOTAL 54

41
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Laboratory Exercise #11

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:

1. Open your IDE for C++ and try this.


#include<iostream>
using namespace std;

int sum(int x, int y)


{
int z;
z = x + y;
return z;
}

int main()
{
int a, b;
cout<<"Enter first number:"; cin>>a;
cout<<"Enter first number:"; cin>>b;
cout<<"The sum is "<<sum (a, b);
}

2. Compile and run the program.


The sample program demonstrates the use of functions with parameters and return
values. Here, a and b at line 16 are sent as arguments, and x and y are parameters
which will hold values of a and b to perform required operation inside the sum function.
In the sum function, the value of z which has a return type of integer will be returned
to line 16.
3. Edit your program into:

42
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

#include<iostream>
using namespace std;

int sum(int x, int y)


{
int z;
z = x + y;
return z;
}

int diff(int x, int y)


{
int z;
z = x - y;
return z;
}

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;
}

4. Compile and run the program.


Both the sum and diff functions have integer data type hence, it will return an integer
value. Here, a and b at line 23 are sent as arguments to x and y parameters of the sum
function. It will then compute for z at line 7 and then it will execute line 8 returning the
value of z at line 23. This will display the string provided along with the returned value
of z. Then, line 24 will be executed where values of a and b are passed to parameters
x and y of the diff function. It will then compute for z at line 14 and then it will execute
line 15 returning the value of z at line 24. This will display the string provided along with
the returned value of z.

Exercises:

Create a C++ program that applies the use of a function with parameters and return
values to:

1. Display the input name and age.


2. Compute for the average of three input grades.
3. Print an input name 10 times.
4. Display the Fibonacci Series up to n number of terms where n is an input from the
user.

43
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Rubrics:

Item Criteria Points Points Earned


1 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 5
2 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 5
3 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
4 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
TOTAL 54

44
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Laboratory Exercise #12

Recursive Functions

Learning Outcomes:

1. Use the correct syntax of recursive functions


2. Apply recursive functions in a C++ program

Guided Activity:

1. Open your IDE for C++ and try this.


#include <iostream>
using namespace std;

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);
}

2. Compile and run the program.


The given program demonstrates the use of recursive function. After inputting an
integer, line 19 follows where n is passed to parameter n of sample function. if n is not
less than 1 then it will proceed to line 9 where it will display the value of n. This is followed
by line 10 where sample function is called and the value passed to the parameter is n-
1. The same step will follow until n is less than 1.
3. Try another example by typing the sample program below:

45
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

#include <iostream>
using namespace std;

int sample(int n1)


{
if (n1 < 1)
return 1;
else
return n1 * sample(n1-1);
}

int main()
{
int f,n;
cout <<"Enter any integer: ";
cin >> n;
f=sample(n);
cout << "The factorial of " <<n <<"is "<<f;
}

4. Compile and run the program.


The given program displays the factorial of an input number using recursive function.
After inputting an integer, line 17 follows where n is passed to parameter n1 of sample
function. if n1 is not less than 1 then it will proceed to line 9 where it will multiply the
value of n1 to the next value after calling the sample function again with n-1 as
argument. This recursive calling process continues until value of n is equal to 1. When n
is equal to 1, it returns 1 and execution of this function stops. The last value will be
returned to line 17 and stored to variable f. Line 18 will be executed and displays the
string provided along with the value of 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:

Enter a positive integer: 3

Sum = 6

46
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

Rubrics:

Item Criteria Points Points Earned


1 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
2 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
3 • Program Readability 2
• Correct Use of Variables and Data types 2
• Correctness of Output 3
• Correctness of Program Logic 8
TOTAL 45

47
Laboratory Manual in Computer Programming 1 Ivy M. Tarun, DIT

REFERENCES

Print:

• Halterman, Richard L. (2017). Fundamentals of Programming C++. Southern Adventist


University
• Hansen, Jeremy A. (2013). The Rook’s Guide to C++. Rook’s Guide Press

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

Photo by Arif Riyanto on Unsplash; Retrieved from


https://unsplash.com/s/photos/computer-programming

• 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

You might also like