Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

BSEM-F19-200 (Muhammad Muneeb

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Name: Muhammad Muneeb

Roll no. (Bsem-f19-200)

Section: 3D

Presented to: Sir Muhammad Ahmad

Course: Data Structure and Algorithms

Assignment #1
Q1: Using the concept of arrays take the maximum 20 random values from user. After that write the
function ascending which convert the given values into ascending values same as descending vice
versa. After that delete the number from array and search the number of particular location.

#include <iostream>

#include<conio.h>

using namespace std;

int A[20];

int del;

int temp;

void Display_Array()

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

cout<<A[i]<<" ";

cout<<endl;

void Ascending_order()

for(int i=1 ;i<20 ;i++)

for(int j=0 ;j<20-i ;j++)

if(A[j]>A[j+1])
{

temp = A[j];

A[j] = A[j+1];

A[j+1] = temp;

void Descending_order()

for(int i=1 ;i<20 ;i++)

for(int j=0 ;j<20-i ;j++)

if(A[j]<A[j+1])

temp = A[j];

A[j] = A[j+1];

A[j+1] = temp;

void delete_element()
{

int size,count=0;

cout<<"Enter array size : ";

cin>>size;

cout<<"Enter array elements : ";

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

cin>>A[i];

cout<<"Enter element to be delete : ";

cin>>del;

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

if(A[i]==del)

for(int j=i; j<(size-1); j++)

A[j]=A[j+1];

count++;

break;

}
cout<<"Element deleted successfully..!!\n";

cout<<"Now the new array is :\n";

for(int i=0; i<(size-1); i++)

cout<<A[i]<<" ";

cout<<endl;

int main()

cout<<"Enter values in array:"<<endl;

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

cin>>A[i];

system("cls");

cout<<"\nArray in random order "<<endl;

Display_Array();

cout<<"\nArray in Ascending order "<<endl;

Ascending_order();

Display_Array();

cout<<"\nArray in Descending order "<<endl;

Descending_order();
Display_Array();

delete_element();

getch();

Q2: Implement the Stack by using the concept of OOP in C++.

#include <iostream>

#include<conio.h>

using namespace std;

#define SIZE 5

char A[SIZE];

int top = -1;

char value;

//function to check if stack is empty

bool isempty()

if (top == -1)

return true;

else

return false;

//function to insert element in stack


void push(char value)

if (top == SIZE - 1)

cout<<"****************"<<endl;

cout << "Stack is full \n";

cout<<"****************"<<endl;

else

top++;

A[top] = value;

//function to remove element from stack

void pop()

if (isempty())

cout<<"****************"<<endl;

cout << "Stack is empty\n";

cout<<"****************"<<endl;

else

top--;
}

//function to display element at Top

void showTop()

if (isempty())

cout<<"****************"<<endl;

cout << "Stack is empty\n";

cout<<"****************"<<endl;

else

cout<<"****************"<<endl;

cout << "element at top is:" << A[top] << "\n";

cout<<"****************"<<endl;

//function to display elements of the Stack

void displaystack()

if (isempty())

cout<<"****************"<<endl;
cout << "Stack is empty\n";

cout<<"****************"<<endl;

else

cout<<"****************"<<endl;

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

cout << A[i] << " , ";

cout << "\n";

cout<<"****************"<<endl;

int main()

int choice;

int flag=1;

while(flag==1)

cout<<"..............Stack................"<<endl;

cout<<" *** Operations you can choose ***"<<endl;

cout<<" (1) Push (Enter a value)"<<endl;

cout<<" (2) Pop (Remove a value)"<<endl;

cout<<" (3) Show Top value "<<endl;

cout<<" (4) Display Stack "<<endl;

cout<<" (5) End Program "<<endl;

cout<<"Enter your Choice ";

cin>>choice;
system("CLS");

switch(choice)

case 1:

cout<<"Enter a value to Enqueue ";

cin>>value;

push(value);

break;

case 2:

pop();

break;

case 3:

showTop();

break;

case 4:

displaystack();

break;

case 5:

flag=0;
break;

getch();

Q3: Implement the Queue by using the concept of OOP in C++.

#include <iostream>

#include<conio.h>

using namespace std;

#define SIZE 5

int A[SIZE];

int front = -1;

int rear = -1;

int value;

//function to check if queue is empty

bool isempty()

if (front == -1 && rear == -1)

return true;

else

return false;

}
//function to insert element in queue

void enqueue(int value)

if (rear == SIZE - 1)

cout<<"****************"<<endl;

cout << "Queue is full \n";

cout<<"****************"<<endl;

else

if (front == -1)

front = 0;

rear++;

A[rear] = value;

//function to remove element from queue

void dequeue()

int sum_dq;

if (isempty())

cout<<"****************"<<endl;

cout << "Queue is empty\n";

cout<<"****************"<<endl;
}

else

if (front == rear)

front = rear = -1;

else

front++;

//function to display element at front

void showfront()

if (isempty())

cout<<"****************"<<endl;

cout << "Queue is empty\n";

cout<<"****************"<<endl;

else

cout<<"****************"<<endl;

cout << "element at front is:" << A[front] << "\n";

cout<<"****************"<<endl;

}
//function to display elements of the queue

void displayQueue()

if (isempty())

cout<<"****************"<<endl;

cout << "Queue is empty\n";

cout<<"****************"<<endl;

else

cout<<"****************"<<endl;

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

cout << A[i] << " ";

cout << "\n";

cout<<"****************"<<endl;

int main()

int choice;

int flag=1;

while(flag==1)

cout<<"..............Queue................"<<endl;

cout<<" *** Operations you can choose ***"<<endl;


cout<<" (1) Enqueue (Enter a value)"<<endl;

cout<<" (2) Dequeue (Remove a value)"<<endl;

cout<<" (3) Show Front "<<endl;

cout<<" (4) Display Queue "<<endl;

cout<<" (5) End Program "<<endl;

cout<<"Enter your Choice ";

cin>>choice;

system("CLS");

switch(choice)

case 1:

cout<<"Enter a value to Enqueue ";

cin>>value;

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

showfront();

break;

case 4:

{
displayQueue();

break;

case 5:

flag=0;

break;

getch();

You might also like