Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

24es132-Programming and Algorithms Lab Manual-1

Uploaded by

ramyadevi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

24es132-Programming and Algorithms Lab Manual-1

Uploaded by

ramyadevi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

ROHINI COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF ELECTRICAL AND ELECTRONICS


ENGINEERING

24ES132-PROGRAMMING AND ALGORITHMS


LABORATORY

LAB MANUAL

I Year - I Semester

Regulation 2021
Course PROGRAMMING AND
24ES132 Course Title:
Code: ALGORITHMS LABORATORY
Credits: 2 L–T–P 0-0-4

Course objectives:
 To develop an algorithm & programming on software tools and Digital processors with
peripheral interfaces.
 To expertise in open source software / packages /tools.
 To do hands-on in commercial and licensed Hardware-software suites.

Teaching-Learning Process:
Suggested strategies that teachers may use to effectively achieve the course outcomes:
1. Interactive Simulations
2. Lab experiment videos
3. Blended Mode of Learning
4. Project based Learning
5. Experiential Learning
6. NPTEL and Other Videos
7. Smart Class Room
8. Flipped Class
LIST OF EXPERIMENTS
1. Implementation of basic programming concepts like conditionals and loops
2. Implementation of function and operator overloading
3. Creation of classes and objects.
4. Implementation of constructors and destructors
5. Implementation of array of objects and dynamic objects.
6. Implementation of inheritance and its types
7. Implementation of polymorphism and its types.
8. Implementation of various sorting algorithms.
9. Application of Stack
10. Implementation of queue using array.
11. Implementation of Linked Lists: Singly linked, doubly linked and Circular lists and applications.

Course outcomes: On completion of the course, the student will have the ability to:

CO1 Implement various object oriented concepts through simple programs.

CO2 Implement different data structures using C++

CO3 Apply the different data structures for implementing solutions to practical problems

CO4 Demonstrate searching algorithms

CO5 Demonstrate sorting algorithms

COs and POs Mapping:


CO/PO PO1 PO2 PO3
CO1 3 - 2
CO2 3 - 2
CO3 3 - 2
CO4 3 - 2
CO5 3 - 2
CO 3 - 2

Level 3- Highly Mapped, Level 2- Moderately Mapped, Level 1- Low Mapped, Level 0- Not
Mapped
Scheme of Evaluation:
Final
Max Reduce Total
Component Typeof assessment mark
Marks d
s
Marks
Continuous Internal Continuous 75 75 10
Examination (CIE) - Assessment 60
0
Laboratory
Model Lab Exam 25 25

End Semester
Lab Exam 100 40 40 40
Examination (ESE)

Total 100
Exp. No. :
Date :
IMPLEMENTATION OF BASIC PROGRAMMING CONCEPTS LIKE
CONDITIONALS AND LOOPS

AIM :-
To write a program to implement the basic programming concepts like conditions and
loops.
PROGRAM:-

#include <iostream>

Using namespace std;

Int main() {
Int num;

// Asking user input


Cout << “Enter a positive integer: “;
Cin >> num;

// Conditional statement
If (num > 0) {
Cout << “You entered a positive number.” << endl;

// Loop to print numbers from 1 to the entered number


Cout << “Printing numbers from 1 to “ << num << “:” << endl;
For (int i = 1; i <= num; i++) {
Cout << i << “ “;
}
Cout << endl;

// While loop to calculate the sum of numbers from 1 to num


Int sum = 0;
Int i = 1;
While (i <= num) {
Sum += i;
I++;
}
Cout << “The sum of numbers from 1 to “ << num << “ is: “ << sum << endl;

} else if (num == 0) {
Cout << “You entered zero.” << endl;
} else {
Cout << “You entered a negative number.” << endl;
}

Return 0;
}

Output:

RESULT :-

Thus the program to implement the basic programming concepts like conditions and
loops is executed and the output is verified.
Exp. No. :
Date :

IMPLEMENTATION OF FUNCTION AND OPERATOR OVERLOADING

AIM :-
To write a program to implement the function and operator overloading.

PROGRAM :-
#include <iostream>

Using namespace std;

// Function to add two integers

Int add(int a, int b) {

Return a + b;

}
// Function to add three integers

Int add(int a, int b, int c) {

Return a + b + c;
}

// Function to add two double values

Double add(double a, double b) {

Return a + b;
}

Int main() {

Cout << “Add two integers: “ << add(3, 4) << endl; // Calls add(int, int)
Cout << “Add three integers: “ << add(3, 4, 5) << endl; // Calls add(int, int, int)

cout << "Add two doubles: " << add(2.5, 3.5) << endl; // Calls add(double, double)

return 0;

Output:

RESULT :-
Thus the program to implement the function and operator overloading is executed and the
output is verified.
Exp. No. :
Date :

CREATION OF CLASSES AND OBJECTS

AIM :-
To write a program to create classes and objects .

PROGRAM :-
#include <iostream>
using namespace std;

// Define a class called "Car"

class Car {

private:

// Data members (attributes)

string brand;
string model;

int year;

public:

// Constructor to initialize a Car object

Car(string b, string m, int y) {

brand = b;

model = m;
year = y;

// Member function to display car details


void displayDetails() {

cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year << endl;

// Setter for the year attribute

void setYear(int y) {

year = y;

// Getter for the year attribute

int getYear() {

return year;
}

};

int main() {

// Creating objects of class Car

Car car1("Toyota", "Corolla", 2018);

Car car2("Honda", "Civic", 2020);

// Displaying details of car1 and car2

car1.displayDetails();

car2.displayDetails();

// Modifying the year of car1 using the setter function

car1.setYear(2021);
cout << "Updated year of car1: " << car1.getYear() << endl;
return 0;

Output:

RESULT :-
Thus the program to create classes and objects is executed and the output is verified.
Exp. No. :
Date :
IMPLEMENTATION OF CONSTRUCTORS AND DESTRUCTORS

AIM :-
To write a program to implement the constructors and destructors .

PROGRAM :-
#include <iostream>

using namespace std;

class Person {

private:

string name;

int age;

public:
// Default constructor

Person() {

name = "Unknown";
age = 0;

cout << "Default constructor called" << endl;

// Parameterized constructor
Person(string n, int a) {

name = n;

age = a;

cout << "Parameterized constructor called for " << name << endl;
}

// Destructor

~Person() {
cout << "Destructor called for " << name << endl;

// Member function to display person details

void display() {

cout << "Name: " << name << ", Age: " << age << endl;

};

int main() {

// Creating an object using the default constructor

Person person1;

person1.display();

// Creating an object using the parameterized constructor


Person person2("Alice", 30);

person2.display();

// Creating an object in a block scope

Person person3("Bob", 25);

person3.display();
} // person3 goes out of scope here, and the destructor is called
cout << "End of main function" << endl;

return 0;
}

Output:

RESULT :-
Thus the program to implement the constructors and destructors is executed and the output is
verified.
Exp. No. :
Date :

IMPLEMENTATION OF ARRAY OF OBJECTS AND DYNAMIC OBJECTS

AIM :-
To write a program to implement the array of objects and dynamic objects.

PROGRAM :-
#include <iostream>
using namespace std;

class Student {

private:

string name;

int rollNumber;

public:

// Parameterized constructor

Student(string n, int r) {
name = n;

rollNumber = r;

// Default constructor
Student() {

name = "Unknown";

rollNumber = 0;

}
// Function to display student details

void display() {

cout << "Name: " << name << ", Roll Number: " << rollNumber << endl;
}

};

int main() {

// Creating an array of Student objects with 3 elements

Student students[3] = { {"Alice", 1}, {"Bob", 2}, {"Charlie", 3} };

// Displaying details of each student


for (int i = 0; i < 3; i++) {

students[i].display();

return 0;

Output:

RESULT :-
Thus the program to implement the array of objects and dynamic objects is executed and the
output is verified .
Exp. No. :
Date :

IMPLEMENTATION OF INHERITANCE AND ITS TYPES

AIM :-
To write a program to implement the inheritance and its types.

PROGRAM :-
#include <iostream>
using namespace std;

// Base class

class Animal {

public:

void eat() {

cout << "Eating..." << endl;


}

};

// Derived class (Single Inheritance)

class Dog : public Animal {

public:

void bark() {

cout << "Barking..." << endl;


}

};

// Derived class (Multiple Inheritance)


class Pet {

public:

void beFriendly() {

cout << "Being friendly..." << endl;


}

};

class Bulldog : public Dog, public Pet {

public:

void guard() {

cout << "Guarding..." << endl;

}
};

// Derived class (Multilevel Inheritance)

class Puppy : public Dog {

public:

void weep() {

cout << "Weeping..." << endl;


}

};

int main() {

Dog dog;

dog.eat(); // From Animal

dog.bark(); // From Dog


Bulldog bulldog;

bulldog.bark(); // From Dog

bulldog.beFriendly(); // From Pet

bulldog.guard(); // From Bulldog

Puppy puppy;

puppy.weep(); // From Puppy

puppy.bark(); // From Dog

puppy.eat(); // From Animal

return 0;

Output:

RESULT :-
Thus the program to implement the inheritance and its types is executed and the output is
verified .
Exp. No. :
Date :

IMPLEMENTATION OF POLYMORPHISM AND ITS TYPES

AIM :-
To write a program to implement the polymorphism and its types.

PROGRAM :-
#include <iostream>
using namespace std;

// Base class

class Shape {

public:

virtual void draw() {

cout << "Drawing a shape" << endl;


}

};

// Derived class

class Circle : public Shape {

public:

void draw() override {

cout << "Drawing a circle" << endl;


}

};

// Derived class
class Rectangle : public Shape {

public:

void draw() override {

cout << "Drawing a rectangle" << endl;


}

};

int main() {

Shape* shape1 = new Circle();

Shape* shape2 = new Rectangle();

// Runtime polymorphism
shape1->draw(); // Output: Drawing a circle

shape2->draw(); // Output: Drawing a rectangle

delete shape1;

delete shape2;

return 0;
}

Output:

RESULT :-
Thus the program to implement the polymorphism and its types is executed and the output is
verified .
Exp. No. :
Date :

IMPLEMENTATION OF VARIOUS SORTING ALGORITHMS

AIM :-
To write a program to implement the various sorting algorithms .

PROGRAM :-
#include <iostream>

Using namespace std;

Void bubbleSort(int arr[], int n) {

For (int i = 0; i < n – 1; i++) {

For (int j = 0; j < n – i – 1; j++) {

If (arr[j] > arr[j + 1]) {


Swap(arr[j], arr[j + 1]);

}
}

Int partition(int arr[], int low, int high) {

Int pivot = arr[high];


Int i = (low – 1);

For (int j = low; j < high; j++) {

If (arr[j] < pivot) {

I++;
Swap(arr[i], arr[j]);

Swap(arr[i + 1], arr[high]);


Return (i + 1);

Void quickSort(int arr[], int low, int high) {

If (low < high) {

Int pi = partition(arr, low, high);

quickSort(arr, low, pi – 1);

quickSort(arr, pi + 1, high);
}

Int main() {

Int arr[] = {64, 25, 12, 22, 11};

Int n = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, n);

cout << “Bubble Sorted array: “;

for (int i = 0; i < n; i++)

cout << arr[i] << “ “;

cout << endl;

int arr2[] = {64, 25, 12, 22, 11};


quickSort(arr2, 0, n – 1);
cout << “Quick Sorted array: “;

for (int i = 0; i < n; i++)

cout << arr2[i] << “ “;

cout << endl;

return 0;

Output:

RESULT :-
Thus the program to implement the various sorting algorithms is executed and the output is
verified .
Exp. No. :
Date :

APPLICATION OF STACK

AIM :-
To write a program of application of stack .

PROGRAM :-
#include <iostream>
#include <stack>

Using namespace std;

Int main() {

Stack<int> stk;

Stk.push(10);

Stk.push(20);
Stk.push(30);

While (!stk.empty()) {
Cout << “Stack top: “ << stk.top() << endl;

Stk.pop();

Return 0;
}
Output:

RESULT :-
Thus the program of application of stack is executed and the output is verified .
Exp. No. :
Date :

IMPLEMENTATION OF QUEUE USING ARRAY

AIM :-
To write a program to implement the queue using array .

PROGRAM :-
#include <iostream>
using namespace std;

#define SIZE 5

class Queue {

private:

int items[SIZE], front, rear;

public:

Queue() {
front = -1;

rear = -1;

void enqueue(int value) {


if (rear == SIZE - 1)

cout << "Queue is full" << endl;

else {

if (front == -1) front = 0;


items[++rear] = value;

cout << "Inserted " << value << endl;

void dequeue() {

if (front == -1 || front > rear)

cout << "Queue is empty" << endl;

else

cout << "Removed " << items[front++] << endl;

void display() {

if (front == -1)

cout << "Queue is empty" << endl;

else {

for (int i = front; i <= rear; i++) {

cout << items[i] << " ";

}
cout << endl;

};

int main() {

Queue q;
q.enqueue(10);
q.enqueue(20);

q.enqueue(30);

q.display();

q.dequeue();
q.display();

return 0;

Output:

RESULT :-
Thus the program to implement the queue using array is executed and the output is verified.
Exp. No. :
Date :

IMPLEMENTATION OF LINKED LISTS: SINGLY LINKED, DOUBLY LINKED AND


CIRCULAR LISTS AND APPLICATIONS

AIM :-
To write a program to implement the Linked Lists: Singly linked, doubly linked and Circular
lists and applications.

PROGRAM :-
#include <iostream>

Using namespace std;

Class Node {

Public:

Int data;

Node* next;

Node(int val) : data(val), next(nullptr) {}

};

Class SinglyLinkedList {

Private:

Node* head;

Public:

SinglyLinkedList() : head(nullptr) {}
Void insert(int data) {

Node* newNode = new Node(data);

If (!head) {

Head = newNode;
} else {

Node* temp = head;

While (temp->next)

Temp = temp->next;

Temp->next = newNode;

Void display() {

Node* temp = head;

While (temp) {

Cout << temp->data << “ -> “;

Temp = temp->next;

Cout << “nullptr” << endl;


}

};

Int main() {

SinglyLinkedList list;

List.insert(10);

List.insert(20);
List.insert(30);
List.display();

Return 0;

}
#include <iostream>

Using namespace std;

Class Node {

Public:

Int data;

Node* next;

Node* prev;
Node(int val) : data(val), next(nullptr), prev(nullptr) {}

};

Class DoublyLinkedList {

Private:

Node* head;

Public:

DoublyLinkedList() : head(nullptr) {}

Void insert(int data) {

Node* newNode = new Node(data);

If (!head) {

Head = newNode;
} else {
Node* temp = head;

While (temp->next)

Temp = temp->next;

Temp->next = newNode;
newNode->prev = temp;

Void display() {

Node* temp = head;

While (temp) {

Cout << temp->data << “ <-> “;


Temp = temp->next;

Cout << “nullptr” << endl;

};

Int main() {
DoublyLinkedList list;

List.insert(10);

List.insert(20);

List.insert(30);

List.display();

Return 0;
}
#include <iostream>

Using namespace std;

Class Node {
Public:

Int data;

Node* next;

Node(int val) : data(val), next(nullptr) {}

};

Class CircularLinkedList {
Private:

Node* head;

Public:

CircularLinkedList() : head(nullptr) {}

// Function to insert a node at the end of the list


Void insert(int data) {

Node* newNode = new Node(data);

If (head == nullptr) {

Head = newNode;

Head->next = head; // Points to itself to form the circular link

} else {

Node* temp = head;


While (temp->next != head) {
Temp = temp->next;

Temp->next = newNode;

newNode->next = head; // Point back to the head


}

// Function to display the list

Void display() {

If (head == nullptr) {

Cout << “The list is empty.” << endl;

Return;
}

Node* temp = head;

Do {

Cout << temp->data << “ -> “;

Temp = temp->next;

} while (temp != head);


Cout << “(head)” << endl;

// Function to delete a node by value

Void deleteNode(int key) {

If (head == nullptr) {

Cout << “The list is empty. Nothing to delete.” << endl;


Return;
}

// If the node to be deleted is the head node

If (head->data == key) {
If (head->next == head) {

// Only one node in the list

Delete head;

Head = nullptr;

} else {

Node* temp = head;

While (temp->next != head) {

Temp = temp->next;
}

Node* toDelete = head;

Head = head->next;

Temp->next = head;

Delete toDelete;

Cout << “Node with value “ << key << “ deleted.” << endl;
Return;

// If the node to be deleted is not the head

Node* current = head;

Node* previous = nullptr;

Do {
Previous = current;
Current = current->next;

If (current->data == key) {

Previous->next = current->next;

Delete current;
Cout << “Node with value “ << key << “ deleted.” << endl;

Return;

} while (current != head);

Cout << “Node with value “ << key << “ not found.” << endl;

};

Int main() {

CircularLinkedList list;

// Inserting nodes into the circular linked list

List.insert(10);

List.insert(20);
List.insert(30);

List.insert(40);

// Displaying the circular linked list

Cout << “Circular Linked List: “;

List.display();

// Deleting a node from the circular linked list


List.deleteNode(20);

Cout << “After deleting 20: “;

List.display();

List.deleteNode(40);

Cout << “After deleting 40: “;

List.display();

List.deleteNode(10);

Cout << “After deleting 10: “;

List.display();

List.deleteNode(30);

Cout << “After deleting 30: “;

List.display();

Return 0;

Output:

RESULT:-
Thus the program to implement the Linked Lists: Singly linked, doubly linked and Circular
lists and applications is executed and the output is verified.

You might also like