Programming Fundamentals-SEPF-121 - Lab Manual
Programming Fundamentals-SEPF-121 - Lab Manual
Programming Fundamentals
SEPF-121
Preface
The Programming Fundamentals Lab Manual is designed to help students learn C++
programming in a simple and effective way. It provides step-by-step instructions and practical
examples to build their coding skills and logical thinking. This manual is aims helping students
become better programmers by mastering the fundamentals of C++. By focusing on logical
reasoning, problem-solving, and programming techniques, it paves the way for a strong
programming foundation. Along with basic programming concepts, it also provides
comprehensive guidance on functions, arrays, and pointers, ensuring that students are well-
versed in these fundamental aspects of programming.
Tools/ Technologies
• C++
• Microsoft Visual Studio
2
BS (Software Engineering) 2023
TABLE OF CONTENTS
Preface ....................................................................................................................................... 2
Tools/ Technologies .................................................................................................................. 2
LAB 1: Orientation to Microsoft Visual Studio .................................................................... 4
LAB 2: Output, Arithmetic Operators and Variables ......................................................... 6
LAB 3: Input and Datatypes ................................................................................................... 9
LAB 4: Decision Making (Conditions) ................................................................................. 11
LAB 5: Nested Conditions and Switch Statements ............................................................. 15
LAB 6: While Loop ................................................................................................................ 19
LAB 7: Do-While Loop.......................................................................................................... 21
LAB 8: For Loop (Basics)...................................................................................................... 24
LAB 9: For Loop (Advanced) and Nested Loops ................................................................ 26
LAB 10: Functions – Declaration, Definition and Calling ................................................. 29
LAB 11: Function Parameters and Arguments .................................................................. 29
LAB 12: Arrays (Basic Concepts) ........................................................................................ 37
LAB 13: Arrays (Advanced Concepts and Examples) ....................................................... 39
LAB 14: Referencing and Dereferencing (Use of Pointers) ............................................... 42
3
BS (Software Engineering) 2023
Objectives
The objective of this lab is to provide a basic orientation to Microsoft Visual Studio in
connection to using C++ as programming language.
Theoretical Description
This lab focuses on demystifying the essential elements of Visual Studio, tailored to the needs
of building C++ programmers. The students will get an idea of Solution Explorer, as the control
center for organizing projects and files. The lab will explore the Editor, used to craft the code
with syntax highlighting and intelligent suggestions.
Lab Task
Task 1: Navigating Solution Explorer
1. Launch Visual Studio and open a C++ project or create a new one.
2. Take a few moments to acquaint yourself with the Solution Explorer. It's like your
project's command center.
3. Explore the hierarchy of your project. You'll find files, resources, and various
elements neatly organized.
4. Try adding a new item to your project using Solution Explorer. Observe how it
reflects in your project's structure.
1. Within your project, open a C++ source code file using the Editor in Visual Studio.
2. Take note of the code highlighting. Visual Studio makes your code more readable by
using different colors for different elements.
3. Try typing a few lines of C++ code. Notice how Visual Studio provides auto-
completion suggestions and helps catch syntax errors.
4. Save your file through the Editor. Observe how it updates in the Solution Explorer.
4
BS (Software Engineering) 2023
5
BS (Software Engineering) 2023
Theoretical Description
Output Basics: You will start by learning how to use the cout statement to display text and
variables on the console. By the end of this section, you will be able to create simple output
messages.
Arithmetic Operations: In this section, you will explore arithmetic operators such as addition,
subtraction, multiplication, and division. You will practice performing calculations and
displaying the results.
Variables: Variables are fundamental for storing and manipulating data in any programming
language. You will explore how to declare and initialize variables,
Lab Task
Task 1: Create a “Hello World” program
#include <iostream>
int main() {
// It is a C++ Program
#include <iostream>
6
BS (Software Engineering) 2023
/* These lines are the part of comments and will not execute
*/
#include <iostream>
int main()
int a;
int b;
a = 10;
b = a + 5;
#include <iostream>
int main()
int a,b;
a = 10; b = a + 5;
#include <iostream>
int main()
7
BS (Software Engineering) 2023
cout<<"Hello\nHow\nAre\nYou\n";
cout<<”Hello\n\tHow\n\t\tAre\n\t\t\tYou\n”;
Escape Sequences
\ is considered an escape sequence character that causes and escape from the normal
interpretation of a string so that the next character is recognized as having a special meaning.
Following are the escape sequence characters along with their usage.
Escape Sequence Characters
\a Audible Alert
\b Backspace
\f Form feed
\n New Line (Carriage Return + Line Feed)
\r Return
\t Tab
\\ Backslash
\’ Single quotation
\” Double quotation
\xDD \xDB Hexadecimal Representation
Task 6:
• Write a program to print a salutation.
• Write a program to perform basic mathematical operations on numbers.
• Write a program to print a message using escape sequences.
8
BS (Software Engineering) 2023
Theoretical Description
Input Basics: You will start by understanding how to receive input from the user using the cin
statement. You'll learn how to prompt users for input and store it in variables.
Data Types: This section will cover different data types in C++ and how to handle user input
for each type, including integers, floating-point numbers, characters, and strings.
Lab Task
Task 1: Develop a C++ program that:
int main() {
9
BS (Software Engineering) 2023
• Ensure the program can handle unexpected input without crashing and provides clear
information about the data types used.
int main() {
int age;
float height;
char favoriteSymbol;
cout << "Age: " << age << " (int)" << endl;
cout << "Height: " << height << " (float)" << endl;
cout << "Favorite Symbol: " << favoriteSymbol << " (char)" << endl;
10
BS (Software Engineering) 2023
Theoretical Description
Decision-making allows a program to execute different code blocks based on specified
conditions. You will learn about conditional statements, logical operators, and how to control
the flow of your program using if, else if, and else statements.
Lab Task
int main() {
int number;
if (number > 0) {
} else {
11
BS (Software Engineering) 2023
int main() {
int number;
if (number % 2 == 0) {
} else {
int main() {
int score;
12
BS (Software Engineering) 2023
} else {
int main() {
cout << "Total cost: $" << purchaseAmount - discount << endl;
int number;
13
BS (Software Engineering) 2023
cout << "The number is within the specified range." << endl;
} else {
cout << "The number is outside the specified range." << endl;
14
BS (Software Engineering) 2023
Theoretical Description
Nested 'if' Statements: Nested ‘if’ allows programmers to create intricate decision-making
structures within their code. A nested 'if' statement is an 'if' statement within another 'if'
statement. This hierarchical approach enables the handling of complex conditions and multiple
scenarios. Students will apply this concept to practical exercises that involve multi-level
decision-making.
'Switch-Case' Statements: The 'switch' statement evaluates a variable and jumps to the
appropriate 'case' label based on its value. This eliminates the need for numerous 'if-else if'
statements, making code more efficient and readable. Students will create programs that
leverage 'switch-case' statements to solve problems with various options or choices.
Lab Task
In this task, you will use nested if statements to determine if a student has passed or failed a
course based on their scores in two exams. Additionally, you will check if the student's scores
are within valid ranges (0-100). Implement the following pseudocode into C++ code:
int main() {
15
BS (Software Engineering) 2023
if (exam1 >= 0 && exam1 <= 100 && exam2 >= 0 && exam2 <= 100) {
} else {
} else {
cout << "Invalid score input. Scores must be between 0 and 100." << endl;
Description: In this task, you will use a switch statement to determine the day of the week
based on a user's input (1-7). Implement the following pseudocode into C++ code:
int main() {
int day;
cout << "Enter a number (1-7) representing a day of the week: ";
switch (day) {
case 1:
break;
case 2:
break;
case 3:
16
BS (Software Engineering) 2023
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
default:
cout << "Invalid input. Please enter a number between 1 and 7." <<
endl;
Description: In this task, you will use nested if statements to categorize and provide discounts
for customers based on their total purchase amount.
int main() {
double totalPurchase;
char isMember;
17
BS (Software Engineering) 2023
} else {
cout << "Total purchase amount after discount: $" << totalPurchase << endl;
18
BS (Software Engineering) 2023
Theoretical Description
A 'while' loop is used to repeatedly execute a block of code as long as a specified condition
remains true. It's an essential tool for automating repetitive tasks and iterating through data
structures like arrays. In this lab, students will understand the syntax of 'while' loops, how to
set up loop conditions, and how to prevent infinite loops. They will also learn about the 'do-
while' loop, a variant of the 'while' loop.
Lab Task
int main() {
int n;
cin >> n;
int count = 1;
count++;
return 0;
19
BS (Software Engineering) 2023
int main() {
int sum = 0;
int number = 2;
sum += number;
number += 2;
cout << "Sum of even numbers from 2 to 100: " << sum;
20
BS (Software Engineering) 2023
Theoretical Description
The 'do-while' loop is similar to the 'while' loop, with one key difference: it executes its block
of code at least once before checking the loop condition. This makes it useful when you want
to ensure that a specific task is performed before considering whether to continue looping. In
this lab, students will learn about 'do-while' loop syntax, how to create exit conditions, and how
to avoid infinite loops.
Lab Task
Description: Create a C++ program that uses a 'do-while' loop to ask the user for their name
and greet them. Continue to ask for their name until they enter "exit."
int main() {
string name;
do {
cout << "Hello, " << name << "!" << endl;
Description: Write a program that calculates the sum of all numbers entered by the user until
they enter a negative number. Use a 'do-while' loop for this task.
21
BS (Software Engineering) 2023
int main() {
do {
if (number >= 0) {
sum += number;
Description: Create a program that asks the user to enter a password. If the entered password
is not "secure123," keep asking until the correct password is provided. Use a 'do-while' loop
for password validation.
int main() {
string password;
do {
Description: Write a C++ program to calculate the factorial of a positive integer entered by
the user. Use a 'do-while' loop to ensure a valid positive number is entered.
22
BS (Software Engineering) 2023
int main() {
int number;
do {
int factorial = 1;
int i = 1;
do {
factorial *= i;
i++;
cout << "Factorial of " << number << " is " << factorial;
23
BS (Software Engineering) 2023
Theoretical Description
The 'for' loop in C++ is used for repetitive tasks where the number of iterations is known. It
consists of three components: initialization, condition, and increment/decrement, all enclosed
in parentheses. In this lab, students will learn the fundamentals of 'for' loops, including how to
set up loop counters, define exit conditions, and control the loop flow.
Lab Task
Task 1: Counting to Ten
Description: Write a C++ program that uses a 'for' loop to count from 1 to 10 and display each
number.
int main() {
int main() {
24
BS (Software Engineering) 2023
Description: Write a program that takes an integer input from the user and prints its
multiplication table from 1 to 10 using a 'for' loop.
int main() {
int number;
cout << number << " x " << i << " = " << (number * i) << endl;
25
BS (Software Engineering) 2023
Theoretical Description
Building on the basics of 'for' loops, this lab delves into advanced applications. Students will
learn how to create nested 'for' loops, control loop flow, and tackle complex problems that
require iteration. The lab tasks will help students enhance their problem-solving skills and
algorithmic thinking.
Lab Task
Task 1: Prime Number Checker
Description: Create a program that checks whether a given number is prime or not. Use a 'for'
loop to iterate through possible divisors.
int main() {
int number;
if (number % i == 0) {
isPrime = false;
break;
if (isPrime) {
26
BS (Software Engineering) 2023
} else {
1
22
333
4444
55555
int main() {
int rows;
cout << i;
27
BS (Software Engineering) 2023
int main() {
int rows;
28
BS (Software Engineering) 2023
Theoretical Description
Functions are essential in C++ programming for modularizing code and promoting reusability.
In this lab, students will start with the basics of functions, including declaration, definition, and
function calls. They will create functions that perform specific tasks and understand the flow
of control in a program with functions.
Lab Task
Task 1:
Declare a function named greetUser that doesn't take any parameters. The function should have
a return type of void. This function will print a greeting message when called.
// Function declaration
void greetUser();
int main() {
greetUser();
// Function definition
void greetUser() {
cout << "Hello! Welcome to the C++ functions lab." << endl;
Task 2:
29
BS (Software Engineering) 2023
Define the showInfo function, which doesn't take any parameters and doesn't return anything
(void). This function should display information about a topic of your choice when called.
// Function declaration
void showInfo();
int main() {
showInfo();
// Function definition
void showInfo() {
cout << "You can customize it for different topics." << endl;
Task 3:
Create a program that defines a function printNumbers without parameters. This function
should print the numbers from 1 to 5 when called. Call this function from the main function.
// Function declaration
void printNumbers();
int main() {
printNumbers();
// Function definition
void printNumbers() {
30
BS (Software Engineering) 2023
31
BS (Software Engineering) 2023
Theoretical Description
Functions become even more powerful when they can accept input values through parameters
and return results. In this lab, students will explore the use of function parameters and
arguments to make functions more versatile and adaptable to different situations.
Lab Task
// Function declaration
int main() {
int a = 5, b = 7;
cout << "Sum of " << a << " and " << b << " is: " << result1 << endl;
cout << "Sum of " << x << " and " << y << " is: " << result2 << endl;
// Function definition
32
BS (Software Engineering) 2023
// Function declaration
int main() {
cout << "Volume of the first box is: " << volume1 << endl;
cout << "Volume of the second box is: " << volume2 << endl;
return 0;
// Function definition
33
BS (Software Engineering) 2023
// Function declaration
int main() {
int a = 5;
double b = 7.5;
float c = 3.2;
cout << "Average of " << a << ", " << b << ", and " << c << " is: " << average
<< endl;
// Function definition
// Function declaration
int main() {
34
BS (Software Engineering) 2023
Instructions:
Declare a function calculateArea() that takes the radius of a circle as an argument and returns
its area. Implement the function to calculate and return the area of the circle using the formula:
area = π * radius * radius.
Overload the calculateArea() function by declaring another version of it that takes the length
and width of a rectangle as arguments and returns its area. Implement this version of the
function to calculate and return the area of the rectangle using the formula: area = length *
width.
In the main() function, call the calculateArea() function with different arguments to calculate
the area of a circle and a rectangle. Display the results.
35
BS (Software Engineering) 2023
int main() {
cout << "Area of the circle with radius " << circleRadius << " is: " <<
circleArea << endl;
cout << "Area of the rectangle with length " << rectangleLength << " and width
" << rectangleWidth << " is: " << rectangleArea << endl;
36
BS (Software Engineering) 2023
Theoretical Description
An array is a collection of elements of the same data type stored in contiguous memory
locations. Understanding arrays is essential for managing and manipulating data efficiently.
Lab Task
Task 1: Declare and Initialize an Array
Declare an integer array of size 5 and initialize it with values 10, 20, 30, 40, and 50. Print the
elements.
int main() {
int main() {
cout << "Third element of the array: " << arr[2] << endl;
37
BS (Software Engineering) 2023
int main() {
arr[1] = 7.5;
38
BS (Software Engineering) 2023
Theoretical Description
Advanced array concepts are crucial for solving complex problems and optimizing memory
usage.
Lab Task
Task 1: Multi-Dimensional Array
Create a 2D integer array to represent a 3x3 matrix. Initialize it with values, and print the
matrix.
int main() {
int main() {
int size;
39
BS (Software Engineering) 2023
cout << "Enter " << size << " elements: ";
delete[] arr;
Create an array of integers and perform array manipulation operations, such as sorting, finding
the maximum and minimum values, and calculating the average.
int main() {
40
BS (Software Engineering) 2023
// Calculate average
int sum = 0;
sum += arr[i];
41
BS (Software Engineering) 2023
Theoretical Description
Pointers are variables that store memory addresses. Understanding referencing and
dereferencing is crucial for memory management and efficient data manipulation.
Lab Task
int main() {
cout << "Value using pointer: " << *ptr << endl;
int main() {
*ptrSalary = 4000.0;
42
BS (Software Engineering) 2023
int main() {
cout << "Element " << i + 1 << ": " << *(ptrNumbers + i) << endl;
Write a function that takes two integer pointers as arguments and swaps the values they point
to. Call the function to swap values of two integers and print the swapped values.
*a = *b;
*b = temp;
int main() {
int x = 5, y = 10;
cout << "Before swapping: x = " << x << ", y = " << y << endl;
swapValues(&x, &y);
cout << "After swapping: x = " << x << ", y = " << y << endl;
43