Lab Manual 01 - Programming Fundamentals Revision
Lab Manual 01 - Programming Fundamentals Revision
Lab Manual 01 - Programming Fundamentals Revision
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:
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
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;
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
For Example:
Input:
Please enter first number: 5.2
Please enter second number: 1.5
Output:
Sum of numbers is: 6.7
For Example:
Input:
Please enter the integer: 8
Output:
Square of numbers is: 64
Cube of numbers is: 512
Half of numbers is: 4
Input:
Please enter size: 10
Output:
Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
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