0% found this document useful (0 votes)
13 views17 pages

Lab 311

Uploaded by

bilalanwar1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

Lab 311

Uploaded by

bilalanwar1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Muhammad Bilal

Roll No : 311

DSA LAB MANUAL


LAB 1

This program will read an integer array (one dimensional array) and sort its
elements in ascending order.
#include<stdio.h>
int main()
{
int a[5],i,j,temp;
printf("enter array elements:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("enter array elements:");
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
}
This program will read an integer array (one dimensional array) and sort its elements in
descending order.
#include<stdio.h>
int main()
{
int a[5],i,j,temp;
printf("enter array elements:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("enter array elements:");
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
}
LAB 2
Create a class Student with following data members (int Reg, string Name, float CGPA, float
SGPA, string Dept and string Semester) and following member functions through function
overloading
1: Set Data
2: Get Data
3: Display the average of CGPA of all students (5 students)
4: Display the average of SGPA of all students (5 students)

#include <iostream>
#include <string>
#include <vector>

class Student {
private:
int Reg;
std::string Name;
float CGPA;
float SGPA;
std::string Dept;
std::string Semester;

public:
// Function to set data
void setData(int reg, const std::string& name, float cgpa, float sgpa, const std::string& dept,
const std::string& semester) {
Reg = reg;
Name = name;
CGPA = cgpa;
SGPA = sgpa;
Dept = dept;
Semester = semester;
}

// Overloaded function to set data


void setData(int reg, const std::string& name) {
Reg = reg;
Name = name;
}

// Function to get data


void getData(int& reg, std::string& name, float& cgpa, float& sgpa, std::string& dept,
std::string& semester) {
reg = Reg;
name = Name;
cgpa = CGPA;
sgpa = SGPA;
dept = Dept;
semester = Semester;
}

// Overloaded function to get basic data


void getData(int& reg, std::string& name) {
reg = Reg;
name = Name;
}

// Static function to calculate average CGPA


static float averageCGPA(const std::vector<Student>& students) {
float totalCGPA = 0;
for (const auto& student : students) {
totalCGPA += student.CGPA;
}
return totalCGPA / students.size();
}

// Static function to calculate average SGPA


static float averageSGPA(const std::vector<Student>& students) {
float totalSGPA = 0;
for (const auto& student : students) {
totalSGPA += student.SGPA;
}
return totalSGPA / students.size();
}
};

int main() {
std::vector<Student> students(5);

// Set Data for each student


students[0].setData(1, "Alice", 3.5, 3.8, "CS", "2nd");
students[1].setData(2, "Bob", 3.6, 3.7, "IT", "2nd");
students[2].setData(3, "Charlie", 3.8, 3.9, "CS", "2nd");
students[3].setData(4, "David", 3.2, 3.5, "EE", "2nd");
students[4].setData(5, "Eve", 3.7, 3.6, "ME", "2nd");

// Get Data for each student and display


for (const auto& student : students) {
int reg;
std::string name;
float cgpa, sgpa;
std::string dept, semester;

student.getData(reg, name, cgpa, sgpa, dept, semester);


std::cout << "Reg: " << reg << ", Name: " << name
<< ", CGPA: " << cgpa << ", SGPA: " << sgpa
<< ", Dept: " << dept << ", Semester: " << semester << std::endl;
}

// Display average CGPA and SGPA


std::cout << "Average CGPA: " << Student::averageCGPA(students) << std::endl;
std::cout << "Average SGPA: " << Student::averageSGPA(students) << std::endl;

return 0;
}

description :

LAB 3
Perform following functions of Stack through Array
1: Push
2: Pop
3: Display
4: Search
5: Update
Call above mentioned functions in Main in following Sequence
Push
Push
Push
Display

#include <iostream>
#include <string>
class Stack {
private:
static const int maxSize = 100; // Maximum size of the stack
int arr[maxSize]; // Array to store stack elements
int top; // Index of the top element

public:
Stack() : top(-1) {} // Constructor to initialize the stack

// Push function
void push(int value) {
if (top >= maxSize - 1) {
std::cout << "Stack Overflow! Cannot push " << value << std::endl;
return;
}
arr[++top] = value;
std::cout << "Pushed: " << value << std::endl;
}

// Pop function
int pop() {
if (top < 0) {
std::cout << "Stack Underflow! Cannot pop." << std::endl;
return -1; // Indicating failure
}
return arr[top--];
}
// Display function
void display() {
if (top < 0) {
std::cout << "Stack is empty." << std::endl;
return;
}
std::cout << "Stack elements: ";
for (int i = 0; i <= top; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}

// Search function
void search(int value) {
for (int i = 0; i <= top; ++i) {
if (arr[i] == value) {
std::cout << "Element " << value << " found at position " << i << std::endl;
return;
}
}
std::cout << "Element " << value << " not found in the stack." << std::endl;
}

// Update function
void update(int oldValue, int newValue) {
for (int i = 0; i <= top; ++i) {
if (arr[i] == oldValue) {
arr[i] = newValue;
std::cout << "Updated " << oldValue << " to " << newValue << std::endl;
return;
}
}
std::cout << "Element " << oldValue << " not found for update." << std::endl;
}
};

int main() {
Stack stack;

// Perform operations in the specified sequence


stack.push(10);
stack.push(20);
stack.push(30);
stack.display();

stack.pop();
stack.display();

stack.search(20);
stack.update(20, 25);
stack.display();
return 0;
}

Description:

LAB 4 :
#include <iostream>
#include <vector>
using namespace std;

// Function to insert an element at the end of the list


void insert(vector<int> &list, int value) {
list.push_back(value);
}

// Function to insert an element at a specific index


void insertAt(vector<int> &list, int index, int value) {
if (index >= 0 && index <= list.size()) {
list.insert(list.begin() + index, value);
} else {
cout << "Invalid index!" << endl;
}
}

// Function to remove an element at a specific index


void removeAt(vector<int> &list, int index) {
if (index >= 0 && index < list.size()) {
list.erase(list.begin() + index);
} else {
cout << "Invalid index!" << endl;
}
}

// Function to display the elements of the list


void display(const vector<int> &list) {
if (list.empty()) {
cout << "The list is empty." << endl;
} else {
cout << "List contents: ";
for (int value : list) {
cout << value << " ";
}
cout << endl;
}
}

int main() {
vector<int> myList; // Initialize an empty list

// Perform operations as specified


insert(myList, 10); // Insert
insert(myList, 20); // Insert
insert(myList, 30); // Insert
display(myList); // Display List

insertAt(myList, 1, 15); // Insert at specific index


display(myList); // Display List
removeAt(myList, 2); // Remove value at a specific index
display(myList); // Display List after removal

return 0;
}
Output :

Description :
This program demonstrates the use of a dynamic list (implemented using std::vector in C++)
with the following operations:

LAB 5

Code 1 :
#include<iostream>
using namespace std;
void insert(int queue[],int max,int&front,int&rear,int item)
{
if(rear+1== max)
{
cout<<"overflow"<<endl;
}
else
{
if(front==-1 && rear == -1)
{
front =0;
rear=0;
}
else
{
rear=rear + 1;
}
queue[rear]=item;
}
}
int main()
{
const int max=6;
int queue[max];
int front=-1,rear=-1;

insert(queue,max,front,rear,10);
insert(queue,max,front,rear,20);
insert(queue,max,front,rear,30);
insert(queue,max,front,rear,40);
insert(queue,max,front,rear,50);
insert(queue,max,front,rear,60);

cout<<"queue contents:";
for(int i=front;i<=rear;i++)
{
cout<<queue[i]<<" ";
}
cout<<endl;
return 0;
}
output :

Description :
This code implements a simple queue using an array in C++. It defines a function, insert, to add
items to the queue, ensuring proper handling of overflow and updating the front and rear
pointers. The main function demonstrates inserting several elements into the queue and then
printing its contents

Code 2 :
#include<iostream>
using namespace std;
int deleteitem(int queue[],int max,int &front, int &rear)

{
int y;

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


{
cout<<"underflow"<<endl;
}
else
{
y=queue[front];
if(front == rear)
{
front == rear=-1;
}
else
{
front=front+1;
}
return y;
}
}
int main()
{
const int max=5;
int queue[max];
int front = -1, rear = -1;

front =0;
rear = 2;
queue[0]= 10;
queue[1]= 20;
queue[2]=30;

int deleteitem = deleteitem(queue,max,front,rear);


if(deleteitem != -1)
{
cout<<"deleteditem != -1"<<deleteditem <<endl;
}
cout<<"Queue contents after deletion:";
for(int i=front;i<=rear && front != i++)
{
cout<< queue[i]<< " ";
}
cout<<endl;
return 0;
}
output:

Description :
This code implements a simple queue in C++ with the functionality to delete an item from the
front of the queue. The deleteitem function handles the deletion operation and ensures that
the queue's state is updated correctly after the operation. The main function initializes the
queue with predefined values, performs a deletion, and then displays the contents of the
queue.

You might also like