CS201P - Introduction to Programming
(Practical)
Lab Manual | Core C++ Programming Exercises | Virtual University of Pakistan
1. Introduction to the Lab
CS201P is the hands-on companion to CS201 - Introduction to Programming. Where the theory course
explains programming concepts, this lab course is where those concepts are actually typed, compiled,
debugged, and tested. Each lab builds on skills from the previous one, moving from simple input/output
programs toward small object-oriented projects.
All exercises in this manual use standard C++ and can be compiled with any modern compiler such as g+
+ (e.g. g++ [Link] -o program) or an IDE like Code::Blocks or Visual Studio.
2. Setting Up and Running a First Program
#include <iostream> using namespace std; int main() { cout << "Hello,
World!" << endl; return 0; }
Every C++ program begins execution in the main() function. The #include <iostream> line brings in
input/output functionality, and using namespace std lets us write cout instead of std::cout.
3. Variables, Data Types, and Input/Output
int age; float gpa; char grade; string name; cout << "Enter your name: ";
cin >> name; cout << "Enter your age: "; cin >> age; cout << name << " is "
<< age << " years old." << endl;
• Common data types: int, float, double, char, bool, string.
• cin reads a single token (stops at whitespace); getline(cin, name) reads a full line including spaces.
• Type casting example: double avg = (double)total / count; ensures decimal division instead of
integer division.
4. Operators and Expressions
C++ supports arithmetic (+, -, *, /, %), relational (==, !=, <, >, <=, >=), and logical (&&, ||, !) operators.
Operator precedence follows standard mathematical rules, and parentheses can be used to force a
particular order of evaluation.
int a = 10, b = 3; cout << a + b << endl; // 13 cout << a % b << endl; //
1 (remainder) cout << (a > b && b > 0) << endl; // 1 (true)
5. Control Structures
If-Else and Nested Conditions
int marks; cin >> marks; if (marks >= 85) { cout << "Grade: A"; } else
if (marks >= 70) { cout << "Grade: B"; } else if (marks >= 50) { cout
<< "Grade: C"; } else { cout << "Fail"; }
Switch Statement
int day; cin >> day; switch (day) { case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break; default: cout << "Invalid day"; }
Loops
// for loop for (int i = 1; i <= 5; i++) { cout << i << " "; } // while
loop int i = 0; while (i < 5) { cout << i; i++; } // do-while loop
(executes at least once) int x = 0; do { cout << x; x++; } while (x <
3);
6. Functions
Functions break a program into reusable, testable pieces. A function has a return type, a name, a
parameter list, and a body.
int add(int a, int b) { return a + b; } void greet(string name)
{ cout << "Hello, " << name << "!" << endl; } int main() { int
result = add(3, 4); cout << result << endl; greet("Ali"); return
0; }
• A function prototype (declaration) must appear before main() if the full definition is written after it.
• Parameters can be passed by value (a copy is made) or by reference using & (the original variable is
modified).
void swap(int &a, int &b) { int temp = a; a = b; b = temp; }
• Default arguments and function overloading (same name, different parameter lists) are also
introduced in this course.
7. Arrays and Strings
int marks[5] = {80, 90, 70, 60, 85}; int sum = 0; for (int i = 0; i < 5; i++)
{ sum += marks[i]; } cout << "Average: " << (float)sum / 5 << endl;
Two-dimensional arrays are used to represent tables or matrices, and are accessed with two indices, e.g.
matrix[row][col].
int matrix[2][3] = {{1,2,3},{4,5,6}}; for (int r = 0; r < 2; r++) { for
(int c = 0; c < 3; c++) { cout << matrix[r][c] << " "; } cout
<< endl; }
8. Pointers - A First Look
int x = 10; int *ptr = &x; // ptr stores the address of x cout << *ptr;
// dereference: prints the value at that address, 10 *ptr = 20; //
changes x to 20 through the pointer
• Pointers are one of the trickier early topics - practice with simple examples before moving to
dynamic memory allocation (new/delete).
9. Introduction to Classes and Objects
Object-oriented programming groups data (attributes) and behavior (methods) together into a class.
Objects are individual instances of a class.
class Student { public: string name; int rollNo; void display()
{ cout << name << " - " << rollNo << endl; } }; int main() {
Student s1; [Link] = "Ali"; [Link] = 101; [Link]();
return 0; }
• Constructors initialize an object automatically when it is created, e.g. Student(string n, int r) { name
= n; rollNo = r; }.
• Access modifiers (public, private, protected) control which parts of a class can be accessed from
outside.
10. Common Lab Exercises
• Write a program to check if a number is prime.
• Write a program to find the largest and smallest elements in an array.
• Write a program to reverse a string using a loop (without built-in reverse functions).
• Write a function that swaps two numbers using reference parameters.
• Write a program to calculate the factorial of a number using recursion.
• Create a simple 'BankAccount' class with deposit(), withdraw(), and showBalance() methods.
• Write a menu-driven program (using switch and a loop) that performs basic calculator operations.
11. Debugging Tips
• Read compiler errors from the top - the first error often causes several that follow.
• Use cout statements to trace variable values at different points in the program while debugging.
• Compile frequently while writing code rather than writing the whole program before testing.
12. Quick Revision Points
• Always initialize variables before using them to avoid garbage/undefined values.
• Watch for off-by-one errors in loop conditions (< vs <=).
• Remember the difference between = (assignment) and == (comparison) - a very common beginner
mistake.
• Practice tracing code by hand on paper before running it; it builds a much stronger mental model.