Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Manual 01 - Programming Fundamentals Revision

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Lab Manual: Object Oriented Programming

University of Management and Technology,


Lahore Campus
Lab- 01 Manual

Lab Instructor: Riaz Ahmad


Department of Computer Science
Email: riazahmad@umt.edu.pk

Lab: 01 Programming Fundamentals Revision


Following are the topics that we will cover in this Lab.
 What are Data Types in C++?
 What is if, if else and how it Works?
 What is Switch?
 What is Loop (for, while, do while), Nested Loop and how it works?
 What are Arrays (1D,2D)?
 What are functions and why we use functions in C++ programming?
 What are Pointers in C++?

Lab Description:
This lab is designed to understand basic Overview of Programming Fundamentals in C++. Program
Construction, I/O Statements, Preprocessor Directives, Data types revised, Control/Iterative Statements,
Arrays, Pointers, Functions, Pass/Return by Value, Pass/Return by Reference, Recursion, Inline
Functions, Default Arguments, Function Overloading in C++.

Lab Rubrics:

Areas Problem Understanding Apply basic programming concepts


Assessed
(CLO 1) (CLO 2)

Poor Not able to understand Problem No able to identify Problem and design solution

Average Able to partially understand the problem Partially able to identify Problem and design solution

Thoroughly Identify the problem and design a complete and


Very Good Fully Understand the problem
accurate solution.

Department of Computer Science, UMT, Lahore. 1 Riaz Ahmad


Lab Manual: Object Oriented Programming
Objective:
To familiarize the students with the overview of Programming Fundamentals Concepts.

Scope:
The student should know the following at the end of this lab:
 Problem understanding
 Problem Solving
 How to use conditionals statements, loops, arrays, pointers, functions etc.
Theory: [CLO1]
1. Data Types in C++
Data types specify the type of data a variable can hold. Some basic data types in C++ include:
int: for integer values (e.g., int a = 5;)
float: for floating-point numbers (e.g., float b = 3.14;)
double: for double-precision floating-point numbers
char: for characters (e.g., char c = 'A';)
bool: for boolean values (true or false)
void: for functions that do not return a value
string: used to store sequences of characters (not a primitive type, part of the standard library)
2. if, if-else, and how they work
The if and if-else statements are conditional statements used to execute code blocks based on a condition:
 if: Executes a block of code if a condition is true.
 if (condition) { // code to be executed if condition is true }
 if-else: Executes one block of code if the condition is true and another block if it is false.
 if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is
false }
3. Switch Statement
The switch statement allows you to execute one block of code among many options based on the value of a
variable.
switch (expression) {
case value1:
// code for value1
break;

Department of Computer Science, UMT, Lahore. 2 Riaz Ahmad


Lab Manual: Object Oriented Programming
case value2:
// code for value2
break;
default:
// code for default case
}
 The break statement stops the switch case from continuing to the next one.
 The default case executes if none of the specified cases match the expression.
4. Loops in C++
Loops allow repetitive execution of a block of code as long as a condition is true.
 for loop: Repeats a block of code a certain number of times.
for (initialization; condition; increment) {
// code to be executed
}
 while loop: Repeats a block of code as long as the condition is true.
while (condition) {
// code to be executed
}
 do-while loop: Similar to while, but guarantees that the code will execute at least once, since the
condition is checked after the execution of the block.
do {
// code to be executed
} while (condition);
 Nested Loop: A loop inside another loop. For example:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
// code inside nested loop
}
}

Department of Computer Science, UMT, Lahore. 3 Riaz Ahmad


Lab Manual: Object Oriented Programming
5. Arrays (1D, 2D)
 1D Array: A collection of elements of the same type stored in contiguous memory locations.
int arr[5] = {1, 2, 3, 4, 5};
 2D Array: An array of arrays, useful for storing tabular data.
int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

6. Functions and Their Purpose


A function is a reusable block of code that performs a specific task.
 Functions help break down complex problems into smaller, manageable tasks.
 They improve code reusability and modularity.
 A typical function definition looks like this:
return_type function_name(parameters) {
// function body
}
For example:
int add(int a, int b) {
return a + b;
}

7. Pointers in C++
A pointer is a variable that stores the memory address of another variable.
 Pointers are used for dynamic memory allocation, accessing arrays, and managing memory
efficiently.
 Example:
int a = 10;
int* p = &a; // p is a pointer to the memory address of a
 Dereferencing a pointer allows access to the value at the memory location:
int value = *p; // value is 10

Department of Computer Science, UMT, Lahore. 4 Riaz Ahmad


Lab Manual: Object Oriented Programming
Task 1: [CLO 2] [CLO 3]
Given two floats x and y (take input from user), write a C++ program that finds their sum.

For Example:

Input:
Please enter first number: 5.2
Please enter second number: 1.5

Output:
Sum of numbers is: 6.7

Task 2: [CLO 2] [CLO 3]


Write a C++ program that takes a number and find square, cube and half of the input.

For Example:

Input:
Please enter the integer: 8

Output:
Square of numbers is: 64
Cube of numbers is: 512
Half of numbers is: 4

Task 3: [CLO 2] [CLO 3]


Fibonacci sequence is a sequence in which every number after the first two is the sum of the two
preceding ones. Write a C++ program that takes a number n from user and populate a array with first n
Fibonacci numbers.
For Example:

Input:
Please enter size: 10

Output:
Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

Department of Computer Science, UMT, Lahore. 5 Riaz Ahmad


Lab Manual: Object Oriented Programming
Task 4: [CLO 2] [CLO 3]
Write a C++ program that creates an array of 10. It then adds 3 to each element of the array.
You have to add to the elements using pointer only.

For Example:

Input:
1
11
2
30
26
12
5
9
15
28

Output:
Resulting Array: 3, 13, 5, 33, 29, 15, 8, 12, 18, 31

Task 5: [CLO 2] [CLO 3]


Write a function that checks whether a given number is prime or not.

Task 6: [CLO 2] [CLO 3]


Write a function that calculates factorial of a given number.
Task 7: [CLO 2] [CLO 3]
Write a C++ program, that prints grade of marks.
If marks are between 85-100 grade them A+
If marks are between 80-85 grade them A
If marks are between 75-80 grade them A-
If marks are between 70-75 grade them B+
If marks are between 65-70 grade them B-
If marks are between 60-65 grade them B
If marks are between 55-60 grade them C+
If marks are between 50-55 grade them C
< 50 F

Task 8: [CLO 2] [CLO 3]


Write a C++ program, to find the number is even or odd.

Task 9: [CLO 2] [CLO 3]

Department of Computer Science, UMT, Lahore. 6 Riaz Ahmad


Lab Manual: Object Oriented Programming
Write a C++ program to check either the user is male or female. Hint: if user enter M he’s male else she’s
female.

Task 10: [CLO 2] [CLO 3]


Write C++ program to check whether the user enters a positive number or the negative number.

Task 11: [CLO 2] [CLO 3]


Write C++ function that will perform the following.
 Read separate numbers.
 Calculate the average of the five numbers.
 Find the smallest (minimum) and largest (maximum) of the five entered numbers.

Task 12: [CLO 2] [CLO 3]


Write program that takes three numbers from user and display them all in sorted order.

Task 13: [CLO 2] [CLO 3]


Write a program in C++ and perform the following: [4 Marks]
1. Declare and initialize two integer type arrays of size 4 with name A and B respectively.
2. Call a function which will swap the values of array A and B
3. Print swapped arrays by calling a function.

Task 14: [CLO 2] [CLO 3]


Write a C++ Program in which you need to implement Following Functions. Take Int type 1D
Array Size 10.
Sort1DArray() // Sort 1D Int type Array
SearchArray() // Search 1D Array
FindmaximumNumber() // find Maximum number from 1D Array
CalculateSumArray() // Calculate sum of Array.
Note: Send parameters from main. You need to implement main function as well.

Task 15: [CLO 2] [CLO 3]


Write a program in C++ and perform the following:
Find out the greatest and the smallest among three numbers using pointers. You can use function
if you want.

Task 16: [CLO 2] [CLO 3] [CLO4]

Department of Computer Science, UMT, Lahore. 7 Riaz Ahmad


Lab Manual: Object Oriented Programming
Print the Following Pattern Using Nested Loop.
*
**
***
****
*****
*****
****
***
**
*
Lab Manual 01 Completed

Department of Computer Science, UMT, Lahore. 8 Riaz Ahmad

You might also like