Lab 311
Lab 311
Roll No : 311
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;
}
int main() {
std::vector<Student> students(5);
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;
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;
int main() {
vector<int> myList; // Initialize an empty list
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;
front =0;
rear = 2;
queue[0]= 10;
queue[1]= 20;
queue[2]=30;
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.