PPS Lab Manual
PPS Lab Manual
LABORATORY MANUAL
of
PROGRAMMING FOR PROBLEM
SOLVING USING C++
(3AEES105)
Prepared by
Mr. Yogesh B. Kumbhar
Assistant Professor,
Department of Aeronautical Engineering
Contents
Sr. Page
Title / Topic of the Experiment
No. number
Write a program to use conditional statements (if, else, switch) for a simple
2 6
calculator or grading system.
Define a structure for student data and write a program to accept and 45
9
display student records.
Write a program to perform file operations: write and read student records 66
12
using file streams.
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 1
Write a C++ program to demonstrate input/output, use of
Date Conducted:
variables, and basic operators.
Purpose:
To introduce students to the basic structure of a C++ program, demonstrate the use of variables,
basic operators (arithmetic, relational, logical, assignment), and input/output operations using
cin and cout, with basic error handling using conditional statements.
Objectives:
Equipment/Components/Software:
Theory:
This experiment is part of Module I: Introduction to C++ Programming and aligns with CO1
and CO2. A C++ program consists of a main() function, which serves as the entry point.
Variables store data of various types (e.g., int for integers, float for floating-point numbers,
char for characters, bool for true/false, string for text). Operators perform operations on
variables:
Arithmetic: +, -, *, /, % (modulus).
Relational: ==, !=, <, >, <=, >=.
Logical: && (AND), || (OR), ! (NOT).
Assignment: = and compound assignments (+=, -=, etc.).
Input/output operations are performed using cin (input) and cout (output), defined in
the <iostream> library. Comments (// or /* */) enhance code readability. Conditional
statements (e.g., if) are used to handle errors, such as preventing division by zero,
aligning with CO2.
Terminology:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 1
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Precautions:
1. Include <iostream> and using namespace std; for cin and cout.
2. Initialize variables before use to avoid undefined behavior.
3. Use correct data types to match input/output requirements.
4. Ensure proper syntax for operators and avoid division by zero using conditional checks.
5. Save the program with a .cpp extension in Dev C++ before compiling.
6. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
int main() {
// Declare variables
int num1, num2;
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 2
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
// Arithmetic operations
cout << "Addition: " << num1 << " + " << num2 << " = " << (num1 +
num2) << endl;
cout << "Subtraction: " << num1 << " - " << num2 << " = " << (num1 -
num2) << endl;
cout << "Multiplication: " << num1 << " * " << num2 << " = " << (num1
* num2) << endl;
return 0;
}
5. Sample Output 1:
6. Sample Problem 2:
Write a program to take a string (name) and a float (temperature) as input, and display
a message with a relational check (e.g., temperature > 30).
7. Sample Code 2:
int main() {
// Declare variables
string name;
float temp;
8.
// Input name and temperature
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter temperature (Celsius): ";
cin >> temp;
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 3
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
} else {
cout << "Temperature is normal" << endl;
}
return 0;
}
9. Sample Output 2:
Practice Questions:
1. Easy: Write a C++ program to take an integer input and display its square using the
multiplication operator.
2. Moderate: Write a program to take two float numbers and display their average using
arithmetic operators.
3. Slightly Difficult: Write a program to input a character and check if it is a vowel (a, e,
i, o, u) using relational and logical operators, then print the result.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 4
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Operator CO1 Correctly implements Uses at least two Uses only one Operators used No operators or
Implementation arithmetic, relational, and operator types operator type or has incorrectly; partial incorrect usage.
logical operators with clear correctly; minor errors. output.
output. errors.
Input/Output CO1 Uses cin/cout with clear Minor formatting Incomplete or Minimal No input/output
Operations prompts and formatted issues in cin/cout but incorrect cin/cout input/output; implemented.
output. functional. usage. unclear output.
Error Handling CO2 Implements conditional Basic error handling Error handling Minimal error No error handling
statement (e.g., if) to handle present but attempted but handling; program implemented.
errors like division by zero incomplete or minor incorrect or may crash on
effectively. issues. ineffective. invalid input.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 5
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 2
Write a C++ Program to Use Conditional Statements (if, else,
Date Conducted:
switch) for a Simple Calculator or Grading System
Purpose:
To enable students to implement conditional statements (if, if-else, switch-case) in C++ to
develop a program that performs calculations or assigns grades based on user input.
Objectives:
1. Understand the syntax and usage of if, if-else, and switch-case statements.
2. Apply conditional statements to make decisions based on user input.
3. Develop a simple calculator or grading system using conditional logic.
4. Write a compilable C++ program with proper syntax, comments, and error handling.
5. Ensure code readability and proper documentation for submission.
Equipment/Components/Software:
Theory:
This experiment is part of Module II: Control Flow and Loops and aligns with CO2 (Develop
programs using conditional statements and loops). Conditional statements allow a program to
execute different code blocks based on specific conditions. The if statement checks a condition
and executes code if true. The if-else statement provides an alternative block if the condition
is false. Nested if statements allow multiple conditions to be checked hierarchically. The
switch-case statement is used for multiple discrete values of a variable, providing a cleaner
alternative to multiple if-else statements. These constructs are essential for decision-making in
applications like calculators or grading systems.
Terminology:
Precautions:
1. Include <iostream> and using namespace std; for cin and cout.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 6
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
2. Ensure valid input types to avoid runtime errors (e.g., expecting numbers but receiving
characters).
3. Use break in switch-case to prevent fall-through to unintended cases.
4. Validate input ranges (e.g., marks between 0 and 100 for grading).
5. Save the program with a .cpp extension in Dev C++ before compiling.
6. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
int main() {
// Declare variables
float num1, num2, result;
char operation;
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 7
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
case '+':
result = num1 + num2;
cout << num1 << " + " << num2 << " = " << result << endl;
break;
case '-':
result = num1 - num2;
cout << num1 << " - " << num2 << " = " << result << endl;
break;
case '*':
result = num1 * num2;
cout << num1 << " * " << num2 << " = " << result << endl;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
cout << num1 << " / " << num2 << " = " << result << endl;
} else {
cout << "Error: Division by zero!" << endl;
}
break;
default:
cout << "Invalid operation!" << endl;
}
return 0;
}
5. Sample Output 1:
6. Sample Problem 2:
Write a program to input a student’s marks (0–100) and assign a grade using if-else
statements (e.g., A for 90–100, B for 80–89, etc.).
7. Sample Code 2:
int main() {
// Declare variable for marks
float marks;
// Input marks
cout << "Enter marks (0-100): ";
cin >> marks;
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 8
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
return 0;
}
8. Sample Output 2:
Practice Questions:
1. Easy: Write a C++ program to check if a number is positive, negative, or zero using if-
else.
2. Moderate: Write a program to determine if a year is a leap year using if-else (divisible
by 4, but not by 100 unless divisible by 400).
3. Slightly Difficult: Write a program to create a menu-driven calculator using switch-
case that supports addition, subtraction, multiplication, division, and modulus, with
error handling for division by zero.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 9
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Input/Output CO2 Uses cin/cout with clear Minor formatting Incomplete or Minimal No input/output
Operations prompts and formatted issues in cin/cout but incorrect cin/cout input/output; implemented.
output for all cases. functional. usage. unclear output.
Error Handling CO2 Implements robust error Basic error handling Error handling Minimal error No error
handling (e.g., division by present but attempted but handling; handling
zero, invalid input) using incomplete. incorrect or program may implemented.
conditionals. ineffective. crash.
Code CO2 Code and output printed Code/output printed Code or output Only code or Neither code nor
Documentation and pasted in manual; but minor missing in manual; output provided; output
well-documented with documentation issues. limited comments. no comments. documented.
comments.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 10
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 3
Write a C++ Program Using Loops (for, while, do-while) to
Date Conducted:
Generate Number Series and Patterns
Purpose:
To enable students to implement looping constructs (for, while, do-while) in C++ to generate
number series (e.g., Fibonacci, arithmetic progression) and print patterns (e.g., star patterns),
while applying modular programming concepts.
Objectives:
1. Understand the syntax and usage of for, while, and do-while loops.
2. Apply loops to generate number series and patterns.
3. Use control statements like break and continue where appropriate.
4. Implement modular programming by structuring code logically.
5. Write a compilable C++ program with proper syntax, comments, and documentation.
Equipment/Components/Software:
Theory:
This experiment is part of Module II: Control Flow and Loops (CO2) and Module III:
Functions, Arrays, and Strings (CO3). Loops allow repetitive execution of code blocks. The
for loop is used when the number of iterations is known, with initialization, condition, and
update in one line. The while loop executes as long as a condition is true, suitable for dynamic
iteration counts. The do-while loop executes at least once before checking the condition.
Control statements like break exit a loop, and continue skips to the next iteration. Loops can
be used to generate number series (e.g., 1, 2, 3, … or Fibonacci) and patterns (e.g., triangle of
stars). Modular programming (CO3) involves organizing code into reusable blocks, though this
experiment focuses primarily on loops.
Terminology:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 11
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Precautions:
1. Include <iostream> and using namespace std; for cin and cout.
2. Ensure loop conditions prevent infinite loops (e.g., correct termination condition).
3. Validate user input to ensure it is within expected ranges (e.g., positive integers for
series length).
4. Use proper indentation and comments for readability.
5. Save the program with a .cpp extension in Dev C++ before compiling.
6. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
int main() {
// Declare variables
int n, first = 0, second = 1, next;
// Validate input
if (n <= 0) {
cout << "Please enter a positive number!" << endl;
return 1;
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 12
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
return 0;
}
5. Sample Output 1:
6. Sample Problem 2:
Write a program to print a right-angled triangle pattern of stars using nested for loops,
with the number of rows provided by the user.
7. Sample Code 2:
int main() {
// Declare variable for number of rows
int rows;
// Validate input
if (rows <= 0) {
cout << "Please enter a positive number!" << endl;
return 1;
}
return 0;
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 13
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
8. Sample Output 2:
Practice Questions:
1. Easy: Write a C++ program to print the first n natural numbers using a while loop.
2. Moderate: Write a program to generate an arithmetic progression (e.g., 2, 4, 6, …) for
n terms using a for loop, where n and the common difference are user inputs.
3. Slightly Difficult: Write a program to print a pyramid pattern of stars (centered
triangle) using nested for loops, with the number of rows provided by the user.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 14
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 15
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 4
Implement a Function to Compute Factorial, Sum of Digits, or
Date Conducted:
Fibonacci Series Using Function Calls
Purpose:
To enable students to design and implement modular programs in C++ by creating functions
to compute mathematical operations like factorial, sum of digits, or Fibonacci series,
emphasizing function definition, declaration, and parameter passing.
Objectives:
Equipment/Components/Software:
Theory:
This experiment is part of Module III: Functions, Arrays, and Strings and aligns with CO3
(Design modular programs using functions and arrays). Functions are reusable blocks of code
that perform specific tasks, improving modularity and readability. A function consists of a
return type, name, parameters, and body. Parameters can be passed by value (copy of data) or
by reference (direct access to data). Functions are declared before use (e.g., using a prototype)
and defined to specify their logic. This experiment focuses on implementing functions for
mathematical computations like factorial (n! = n × (n-1) × ... × 1), sum of digits (e.g., 123 = 1
+ 2 + 3 = 6), or Fibonacci series (0, 1, 1, 2, 3, …).
Terminology:
Precautions:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 16
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
1. Include <iostream> and using namespace std; for cin and cout.
2. Declare functions before calling them, either via prototypes or by defining them before
main().
3. Validate input to avoid invalid results (e.g., negative numbers for factorial).
4. Ensure proper return types and parameter types match the function’s purpose.
5. Save the program with a .cpp extension in Dev C++ before compiling.
6. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
// Function prototype
long long factorial(int n);
int main() {
// Declare variable
int num;
// Input number
cout << "Enter a non-negative integer: ";
cin >> num;
// Validate input
if (num < 0) {
cout << "Factorial is not defined for negative numbers!" << endl;
return 1;
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 17
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
return 0;
}
5. Sample Output 1:
6. Sample Problem 2:
Write a program to compute the sum of digits of a number using a function with
parameter passing by value.
7. Sample Code 2:
// Function prototype
int sumOfDigits(int num);
int main() {
// Declare variable
int num;
// Input number
cout << "Enter a positive integer: ";
cin >> num;
// Validate input
if (num < 0) {
cout << "Please enter a positive number!" << endl;
return 1;
}
return 0;
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 18
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
8. Sample Output 2:
Practice Questions:
1. Easy: Write a C++ program with a function to compute the square of a number using
parameter passing by value.
2. Moderate: Write a program with a function to compute the sum of the first n natural
numbers, where n is user input.
3. Slightly Difficult: Write a program with a function to generate the nth Fibonacci
number using recursion and parameter passing by value.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 19
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Input/Output CO3 Uses cin/cout with clear Minor formatting Incomplete or Minimal No input/output
Operations prompts and formatted issues in cin/cout but incorrect cin/cout input/output; implemented.
output for function results. functional. usage. unclear output.
Code CO3 Code and output printed Code/output printed Code or output Only code or Neither code nor
Documentation and pasted in manual; well- but minor missing in manual; output provided; output
documented with documentation issues. limited comments. no comments. documented.
comments.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 20
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 5
Demonstrate Function Overloading with Different Parameter
Date Conducted:
Types
Purpose:
To enable students to implement function overloading in C++ by creating multiple functions
with the same name but different parameter types or counts, emphasizing modular
programming and code reusability.
Objectives:
Equipment/Components/Software:
Theory:
This experiment is part of Module III: Functions, Arrays, and Strings and aligns with CO3
(Design modular programs using functions and arrays). Function overloading allows multiple
functions to share the same name but differ in their parameter list (number, type, or order of
parameters). The compiler selects the appropriate function based on the arguments provided
during the call. This promotes code reusability and flexibility. For example, a function area can
be overloaded to calculate the area of a square (int), rectangle (int, int), or circle (double).
Overloaded functions must have distinct signatures, and the return type alone is not sufficient
for overloading.
Terminology:
Function Overloading: Defining multiple functions with the same name but different
parameter lists.
Function Signature: The combination of a function’s name and its parameter list (type,
number, order).
Parameter: A variable passed to a function to perform operations.
Return Type: The data type of the value returned by a function (e.g., int, float, void).
Scope: The region of the program where a function or variable is accessible.
Modularity: Organizing code into reusable, independent functions for better
maintenance.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 21
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Precautions:
1. Include <iostream> and using namespace std; for cin and cout.
2. Ensure function signatures are unique (differ in parameter type, number, or order).
3. Validate input types to match the expected parameter types of overloaded functions.
4. Use clear function names and comments to distinguish overloaded functions.
5. Save the program with a .cpp extension in Dev C++ before compiling.
6. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 22
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 23
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Practice Questions:
1. Easy: Write a C++ program to overload a function square to compute the square of an
int and a float.
2. Moderate: Write a program to overload a function display to print a single int, a string,
or two int values.
3. Slightly Difficult: Write a program to overload a function volume to calculate the
volume of a cube (int), cylinder (double, double), and rectangular prism (int, int, int).
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 24
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 25
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 6
Write a C++ Program to Perform Basic Array Operations:
Date Conducted:
Search, Insert, Delete (1D Arrays)
Purpose:
To enable students to implement basic array operations (search, insert, delete) on one-
dimensional arrays in C++ using functions, and to demonstrate searching techniques, aligning
with modular programming and basic algorithm implementation.
Objectives:
Equipment/Components/Software:
Theory:
This experiment is part of Module III: Functions, Arrays, and Strings (CO3) and Module VI:
Searching, Sorting, and File Handling (CO6). A one-dimensional array is a collection of
elements of the same data type stored in contiguous memory locations, accessed using indices
(0 to size-1). Array operations include:
Search: Finding an element (e.g., using linear search, which checks each element
sequentially).
Insert: Adding an element at a specified position, shifting subsequent elements.
Delete: Removing an element from a specified position, shifting subsequent elements.
Functions enhance modularity (CO3) by encapsulating these operations. Linear search,
a basic algorithm (CO6), has a time complexity of O(n). Proper input validation ensures
robust array operations.
Terminology:
Array: A data structure storing elements of the same type in contiguous memory.
Index: A number used to access an array element (starts at 0).
Linear Search: A search algorithm that checks each element sequentially.
Insert: Adding an element to an array at a specific position.
Delete: Removing an element from an array at a specific position.
Function: A reusable block of code for specific tasks.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 26
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Precautions:
1. Include <iostream> and using namespace std; for cin and cout.
2. Validate array indices to prevent out-of-bounds errors (e.g., index < size).
3. Ensure array size is sufficient for insert operations to avoid overflow.
4. Check if an element exists before deletion.
5. Save the program with a .cpp extension in Dev C++ before compiling.
6. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
// Function prototype
int linearSearch(int arr[], int size, int key);
int main() {
// Declare variables
int size, key;
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 27
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
return 1;
}
return 0;
}
5. Sample Output 1:
6. Sample Problem 2:
Write a program to insert and delete an element in a 1D array using functions, with
proper array shifting.
7. Sample Code 2:
// Function prototypes
void insertElement(int arr[], int& size, int maxSize, int pos, int value);
void deleteElement(int arr[], int& size, int pos);
void displayArray(int arr[], int size);
int main() {
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 28
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
// Declare variables
int size, maxSize = 100, pos, value;
// Insert operation
cout << "Enter position (0 to " << size << ") and value to insert:
";
cin >> pos >> value;
insertElement(arr, size, maxSize, pos, value);
cout << "Array after insertion: ";
displayArray(arr, size);
// Delete operation
cout << "Enter position (0 to " << size-1 << ") to delete: ";
cin >> pos;
deleteElement(arr, size, pos);
cout << "Array after deletion: ";
displayArray(arr, size);
return 0;
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 29
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
}
for (int i = pos; i < size-1; i++) {
arr[i] = arr[i+1]; // Shift elements
}
size--;
}
8. Sample Output 2:
Practice Questions:
1. Easy: Write a C++ program with a function to search for an element in a 1D array and
return its index using linear search.
2. Moderate: Write a program with a function to insert an element at the end of a 1D
array, with size validation.
3. Slightly Difficult: Write a program with functions to insert and delete elements in a 1D
array, ensuring the array remains sorted after each operation.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 30
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 31
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 32
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 7
Write a C++ Program to Manipulate Strings Using Character
Date Conducted:
Arrays and Built-in Functions (strlen, strcpy, etc.)
Purpose:
To enable students to manipulate strings using character arrays and built-in C-string functions
(e.g., strlen, strcpy, strcat, strcmp) in C++, emphasizing modular programming and string
handling.
Objectives:
Equipment/Components/Software:
Theory:
This experiment is part of Module III: Functions, Arrays, and Strings and aligns with CO3
(Design modular programs using functions and arrays). In C++, strings can be represented as
character arrays (null-terminated, e.g., char str[] = "hello";) or using the std::string class. This
experiment focuses on character arrays and C-style string functions from the <cstring> library:
Terminology:
Character Array: An array of char type used to store a string, terminated by \0.
Null Terminator: The character \0 marking the end of a C-string.
strlen: Function to compute the length of a string.
strcpy: Function to copy a source string to a destination.
strcat: Function to append one string to another.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 33
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Precautions:
1. Include <iostream> and <cstring> for string functions and using namespace std;.
2. Ensure character arrays are large enough to hold input strings and null terminators.
3. Validate input to avoid buffer overflow or invalid string operations.
4. Use proper function calls for strcpy and strcat to avoid overwriting memory.
5. Save the program with a .cpp extension in Dev C++ before compiling.
6. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
// Function prototypes
void displayLength(char str[]);
void compareStrings(char str1[], char str2[]);
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 34
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
int main() {
// Declare character arrays
char str1[100], str2[100];
// Input strings
cout << "Enter first string: ";
cin.getline(str1, 100);
cout << "Enter second string: ";
cin.getline(str2, 100);
return 0;
}
5. Sample Output 1:
6. Sample Problem 2:
Write a program to copy one string to another using strcpy and concatenate two strings
using strcat.
7. Sample Code 2:
// Function prototypes
void copyString(char dest[], char src[]);
void concatenateStrings(char str1[], char str2[]);
int main() {
// Declare character arrays
char str1[100], str2[100], copy[100];
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 35
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
// Input strings
cout << "Enter first string: ";
cin.getline(str1, 100);
cout << "Enter second string: ";
cin.getline(str2, 100);
// Copy string
copyString(copy, str1);
cout << "Copied string: " << copy << endl;
// Concatenate strings
concatenateStrings(str1, str2);
cout << "Concatenated string: " << str1 << endl;
return 0;
}
8. Sample Output 2:
Practice Questions:
1. Easy: Write a C++ program with a function to compute the length of a string using
strlen.
2. Moderate: Write a program with a function to copy a string to another using strcpy and
display both strings.
3. Slightly Difficult: Write a program with a function to reverse a string using a character
array (without built-in functions) and verify it using strcmp.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 36
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 37
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 38
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 8
Implement a Program Using Pointers and Arrays; Show Pointer
Date Conducted:
Arithmetic and Dynamic Memory Allocation
Purpose:
To enable students to implement C++ programs using pointers with arrays, demonstrate pointer
arithmetic, and utilize dynamic memory allocation for flexible array management, emphasizing
modular programming and pointer concepts.
Objectives:
1. Understand the concept of pointers and their use with arrays in C++.
2. Implement pointer arithmetic to access and manipulate array elements.
3. Use dynamic memory allocation (new and delete) for arrays.
4. Develop modular programs using functions to handle pointer-based operations.
5. Write a compilable C++ program with proper syntax, comments, and documentation.
Equipment/Components/Software:
Theory:
This experiment is part of Module III: Functions, Arrays, and Strings (CO3) and Module IV:
Pointers and Structures (CO4). A pointer is a variable that stores the memory address of another
variable. Pointers can be used with arrays to access elements via pointer arithmetic, where
incrementing a pointer moves it to the next memory location based on the data type (e.g., ptr +
1 moves by sizeof(int) for an int pointer). Dynamic memory allocation, using new and delete,
allows arrays to be created at runtime with flexible sizes. Functions enhance modularity (CO3)
by encapsulating pointer operations, while proper memory management (CO4) prevents
memory leaks.
Terminology:
Precautions:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 39
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
1. Include <iostream> and using namespace std; for cin and cout.
2. Initialize pointers to avoid undefined behavior (e.g., set to nullptr or a valid address).
3. Ensure proper deallocation of dynamic memory using delete to prevent memory leaks.
4. Validate array indices and pointer operations to avoid accessing invalid memory.
5. Save the program with a .cpp extension in Dev C++ before compiling.
6. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
// Function prototype
int sumArray(int* arr, int size);
int main() {
// Declare variables
int size;
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 40
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
return 0;
}
5. Sample Output 1:
6. Sample Problem 2:
Write a program to dynamically allocate a 1D array, input elements, and reverse the
array using pointer arithmetic, then deallocate the memory.
7. Sample Code 2:
// Function prototype
void reverseArray(int* arr, int size);
int main() {
// Declare variables
int size;
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 41
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
// Reverse array
reverseArray(arr, size);
// Deallocate memory
delete[] arr;
return 0;
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 42
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
8. Sample Output 2:
Practice Questions:
1. Easy: Write a C++ program to traverse a 1D array using a pointer and print its elements.
2. Moderate: Write a program to dynamically allocate a 1D array, input elements, and
find the maximum element using pointer arithmetic.
3. Slightly Difficult: Write a program to dynamically allocate a 1D array, sort it using
pointer arithmetic, and deallocate the memory.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 43
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 44
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 9
Define a Structure for Student Data and Write a Program to
Date Conducted:
Accept and Display Student Records
Purpose:
To enable students to define and use structures in C++ to manage student data, implement
functions to accept and display records, and demonstrate basic data organization, aligning with
modular programming and data handling concepts.
Objectives:
Equipment/Components/Software:
Theory:
This experiment is part of Module IV: Pointers and Structures (CO4) and Module VI:
Searching, Sorting, and File Handling (CO6). A structure in C++ is a user-defined data type
that groups related data (e.g., int rollNo, string name, float marks) under a single name.
Structures allow organized storage of records, such as student data. Functions can be used to
input and display structure data, promoting modularity (CO4). This experiment also aligns with
CO6 by focusing on data handling, a precursor to file operations, as it involves structured data
management. Input validation ensures robust record handling.
Terminology:
Precautions:
1. Include <iostream> and <string> for cin, cout, and string usage, with using namespace
std;.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 45
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
2. Validate input (e.g., positive roll numbers, valid marks) to ensure data integrity.
3. Ensure structure members are accessed correctly using the dot operator.
4. Use cin.getline or getline for string input to handle spaces.
5. Save the program with a .cpp extension in Dev C++ before compiling.
6. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
// Structure definition
struct Student {
int rollNo;
string name;
float marks;
};
// Function prototypes
void inputStudent(Student& s);
void displayStudent(Student s);
int main() {
// Declare a student structure
Student s;
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 46
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
return 0;
}
// Validate input
if (s.rollNo < 0 || s.marks < 0 || s.marks > 100) {
cout << "Invalid input! Roll number and marks must be valid." <<
endl;
s.rollNo = 0;
s.marks = 0;
}
}
5. Sample Output 1:
Student Record:
Roll Number: 101
Name: First Name Last Name
Marks: 85
6. Sample Problem 2:
Write a program to define a structure for a student and use functions to accept and
display multiple student records using an array of structures.
7. Sample Code 2:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 47
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
// Structure definition
struct Student {
int rollNo;
string name;
float marks;
};
// Function prototypes
void inputStudents(Student s[], int size);
void displayStudents(Student s[], int size);
int main() {
// Declare variables
int size;
return 0;
}
// Validate input
if (s[i].rollNo < 0 || s[i].marks < 0 || s[i].marks > 100) {
cout << "Invalid input! Setting default values." << endl;
s[i].rollNo = 0;
s[i].marks = 0;
}
}
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 48
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
8. Sample Output 2:
Student Records:
Student 1:
Roll Number: 101
Name: First Name Last Name
Marks: 85
Student 2:
Roll Number: 102
Name: Jane Smith
Marks: 92
Practice Questions:
1. Easy: Write a C++ program to define a structure for a student (roll number, name) and
display one record.
2. Moderate: Write a program to define a structure for a student and use a function to find
the student with the highest marks in an array of structures.
3. Slightly Difficult: Write a program to define a structure for a student and sort an array
of student records by marks using a function.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 49
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 50
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 51
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 10
Create a Class with Data Members and Member Functions;
Date Conducted:
Include Constructors and Display Details
Purpose:
To enable students to implement a C++ class with data members, member functions, and
constructors, demonstrating object-oriented programming (OOP) concepts like encapsulation
and modularity, and to display object details.
Objectives:
Equipment/Components/Software:
Theory:
This experiment is part of Module III: Functions, Arrays, and Strings (CO3) and Module V:
Object-Oriented Programming Concepts (CO5). A class in C++ is a user-defined data type that
encapsulates data members (variables) and member functions (methods) to operate on that data.
Constructors are special member functions that initialize objects, called automatically when an
object is created. Encapsulation (CO5) involves bundling data and methods, often using access
specifiers (private, public) to protect data. Member functions promote modularity (CO3) by
organizing operations within the class. This experiment focuses on creating a class (e.g., for a
student) and displaying its details.
Terminology:
Class: A blueprint for objects, defining data members and member functions.
Object: An instance of a class.
Data Member: A variable defined within a class.
Member Function: A function defined within a class to operate on its data.
Constructor: A special function to initialize objects, with the same name as the class.
Encapsulation: Restricting access to data members using access specifiers.
Modularity: Organizing code into reusable units (e.g., member functions).
Precautions:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 52
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
1. Include <iostream> and <string> for cin, cout, and string usage, with using namespace
std;.
2. Ensure constructors have the same name as the class and no return type.
3. Use private for data members and public for member functions to enforce
encapsulation.
4. Validate input (e.g., positive values for roll number, valid marks) to ensure data
integrity.
5. Save the program with a .cpp extension in Dev C++ before compiling.
6. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
// Class definition
class Student {
private:
int rollNo;
string name;
float marks;
public:
// Default constructor
Student() {
rollNo = 0;
name = "Unknown";
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 53
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
marks = 0.0;
}
int main() {
// Create object with default constructor
Student s1;
return 0;
}
5. Sample Output 1:
Student Details:
Roll Number: 0
Name: Unknown
Marks: 0
6. Sample Problem 2:
Write a program to create a Student class with data members (roll number, name,
marks), a parameterized constructor, and member functions to set and display details.
7. Sample Code 2:
// Class definition
class Student {
private:
int rollNo;
string name;
float marks;
public:
// Parameterized constructor
Student(int r, string n, float m) {
rollNo = r;
name = n;
if (m >= 0 && m <= 100) {
marks = m;
} else {
marks = 0;
cout << "Invalid marks! Set to 0." << endl;
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 54
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
int main() {
// Declare variables for input
int rollNo;
string name;
float marks;
return 0;
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 55
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
8. Sample Output 2:
Student Details:
Roll Number: 101
Name: First Name Last Name
Marks: 85
Student Details:
Roll Number: 102
Name: Jane Smith
Marks: 92
Practice Questions:
1. Easy: Write a C++ program to create a Book class with data members (title, author)
and a default constructor to display details.
2. Moderate: Write a program to create a Rectangle class with data members (length,
breadth), a parameterized constructor, and a member function to calculate and display
the area.
3. Slightly Difficult: Write a program to create a Student class with an array of marks, a
constructor to initialize it, and a member function to compute and display the average
marks.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 56
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Write a constructor for a Car class with data members CO5 K3 (Applying)
(model, year).
Analyze the benefits of using member functions for CO3 K4 (Analyzing)
modularity.
How would you validate input in a constructor to ensure CO5 K4 (Analyzing)
data integrity?
Evaluate when to use a default constructor versus a CO5 K5 (Evaluating)
parameterized constructor.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 57
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 58
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 11
Demonstrate Inheritance by Creating Base Class Person and
Date Conducted:
Derived Class Student
Purpose:
To enable students to implement inheritance in C++ by creating a base class Person and a
derived class Student, demonstrating object-oriented programming (OOP) concepts such as
inheritance and encapsulation.
Objectives:
Equipment/Components/Software:
Theory:
This experiment is part of Module V: Object-Oriented Programming Concepts and aligns with
CO5 (Construct C++ classes applying OOP concepts: encapsulation, inheritance). Inheritance
allows a derived class (e.g., Student) to inherit properties and methods from a base class (e.g.,
Person), promoting code reuse and hierarchy. The derived class can extend or override base
class functionality. Encapsulation is achieved by using access specifiers (private, public,
protected) to control data access. Single inheritance, where Student inherits from Person, is
demonstrated here. Constructors initialize objects, and member functions handle input/output
operations.
Terminology:
Inheritance: A mechanism where a derived class inherits properties and methods from
a base class.
Base Class: The parent class (e.g., Person) from which another class inherits.
Derived Class: The child class (e.g., Student) that inherits from the base class.
Access Specifier: public, private, or protected, controlling access to class members.
Constructor: A special function to initialize objects.
Encapsulation: Restricting access to data members using access specifiers.
Precautions:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 59
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
1. Include <iostream> and <string> for cin, cout, and string usage, with using namespace
std;.
2. Use public inheritance to ensure base class members are accessible in the derived class
as intended.
3. Ensure constructors initialize all data members, including those inherited from the base
class.
4. Use protected for base class members that derived classes need to access directly.
5. Validate input (e.g., valid age, marks) to ensure data integrity.
6. Save the program with a .cpp extension in Dev C++ before compiling.
7. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 60
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
public:
// Constructor
Person() {
name = "Unknown";
age = 0;
}
public:
// Constructor
Student() : Person() {
rollNo = 0;
marks = 0.0;
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 61
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
int main() {
// Create student object
Student s1;
return 0;
}
5. Sample Output 1:
Student Details:
Name: First Name Last Name
Age: 20
Roll Number: 101
Marks: 85
6. Sample Problem 2:
Write a program to create a base class Person with name and age, and a derived class
Student with roll number and marks, using parameterized constructors to initialize
objects.
7. Sample Code 2:
public:
// Parameterized constructor
Person(string n, int a) {
name = n;
age = (a >= 0) ? a : 0;
if (a < 0) {
cout << "Invalid age! Set to 0." << endl;
}
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 62
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
public:
// Parameterized constructor
Student(string n, int a, int r, float m) : Person(n, a) {
rollNo = r;
marks = (m >= 0 && m <= 100) ? m : 0;
if (m < 0 || m > 100) {
cout << "Invalid marks! Set to 0." << endl;
}
}
int main() {
// Declare variables for input
string name;
int age, rollNo;
float marks;
// Input details
cout << "Enter name: ";
cin.ignore(1); // Clear buffer
getline(cin, name);
cout << "Enter age: ";
cin >> age;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter marks (0-100): ";
cin >> marks;
return 0;
}
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 63
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
8. Sample Output 2:
Student Details:
Name: Jane Smith
Age: 22
Roll Number: 102
Marks: 92
Practice Questions:
1. Easy: Write a C++ program with a Person base class (name, age) and a Student derived
class (roll number), displaying details using inherited methods.
2. Moderate: Write a program with a Person base class and a Student derived class,
adding a member function in Student to calculate grade based on marks.
3. Slightly Difficult: Write a program with a Person base class and a Student derived
class, using an array of Student objects to store and display multiple records.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 64
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 65
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Expt. No. 12
Write a Program to Perform File Operations: Write and Read
Date Conducted:
Student Records Using File Streams
Purpose:
To enable students to implement file handling in C++ using file streams (ifstream, ofstream,
fstream) to write and read student records, demonstrating data persistence and management.
Objectives:
1. Understand the use of file streams (ifstream, ofstream, fstream) for file operations in
C++.
2. Implement a program to write student records to a file and read them back.
3. Use structures or classes to organize student data.
4. Write a compilable C++ program with proper syntax, comments, and modular structure.
5. Ensure code readability and proper documentation for submission.
Equipment/Components/Software:
Theory:
This experiment is part of Module VI: Searching, Sorting, and File Handling and aligns with
CO6 (Develop programs using search/sort algorithms and file handling). File handling in C++
involves using file streams from the <fstream> library: ofstream for writing, ifstream for
reading, and fstream for both. Files can store structured data, such as student records (e.g., roll
number, name, marks). A structure or class organizes this data, and file operations enable
persistent storage. Key operations include opening, writing, reading, and closing files. Error
handling ensures robust file operations (e.g., checking if a file opened successfully).
Terminology:
Precautions:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 66
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
1. Include <iostream>, <fstream>, and <string> for file operations and string handling,
with using namespace std;.
2. Check if files open successfully using .is_open() to avoid runtime errors.
3. Close files using .close() after operations to free resources.
4. Validate input data (e.g., positive roll numbers, valid marks) before writing to files.
5. Ensure the file path is accessible and writable in Dev C++.
6. Save the program with a .cpp extension and create a text file (e.g., students.txt) in the
same directory as the program.
7. Check for compilation errors in Dev C++ and resolve them before execution.
Procedure:
// Structure definition
struct Student {
int rollNo;
string name;
float marks;
};
// Function prototypes
void writeStudentToFile(Student s, const string& filename);
void readStudentFromFile(const string& filename);
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 67
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
int main() {
// Declare student structure
Student s;
// Validate input
if (s.rollNo < 0 || s.marks < 0 || s.marks > 100) {
cout << "Invalid input! Program terminated." << endl;
return 1;
}
// Write to file
writeStudentToFile(s, "students.txt");
return 0;
}
5. Sample Output 1:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 68
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
6. Sample Problem 2:
Write a program to store multiple student records in a file using ofstream and read them
back using ifstream, with a structure for student data.
7. Sample Code 2:
// Structure definition
struct Student {
int rollNo;
string name;
float marks;
};
// Function prototypes
void writeStudentsToFile(Student s[], int size, const string& filename);
void readStudentsFromFile(const string& filename);
int main() {
// Declare variables
int size;
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 69
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
// Validate input
if (students[i].rollNo < 0 || students[i].marks < 0 ||
students[i].marks > 100) {
cout << "Invalid input! Setting default values." << endl;
students[i].rollNo = 0;
students[i].marks = 0;
}
}
// Write to file
writeStudentsToFile(students, size, "students.txt");
return 0;
}
8. Sample Output 2:
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 70
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
Student 2:
Roll Number: 102
Name: Jane
Marks: 92
Practice Questions:
1. Easy: Write a C++ program to write a single student’s roll number and name to a file
and read it back.
2. Moderate: Write a program to append a new student record to an existing file using
fstream in append mode.
3. Slightly Difficult: Write a program to read student records from a file and display only
those with marks above a user-specified threshold.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 71
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
How would you handle errors when a file cannot be opened? CO6 K4 (Analyzing)
Evaluate the benefits of using file streams versus manual CO6 K5 (Evaluating)
data entry for student records.
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 72
3AEES105 - Programming for
Department of Basic Sciences
problem solving using C++
laboratory manual of programming for problem solving using C++ (3AEES105) Page no. 73