+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
C++ adt c++ implementations
1. PROGRAM TO COMPARE AN ARRAY OF NUMBERS
#include<iostream>
using namespace std;
int main()
{
//constant number
const int max=10;
int temp;
int number[max];
int i;
float avg;
//this program calculates the sum of array elemens
int sum=0;
cout<<"please enter 10 intergers. n";
for(i=0;i<max;i++)
{
cout<<"Number "<<i+1<<": ";
cin>>number[i];
sum+=number[i];
}
avg=sum/max;
cout<<"The array elements are:"<<"nn";
for(i=0;i<max;i++)
{
cout<<number[i]<<",";
}
cout<<"nn The sum is "<<sum<<"nn";
cout<<"The average is:"<<avg<<"nn";
//descending order sort
for(int k =0;k<max;k++)
{
for(int x=0;x<max;x++)
{
if(number[x]<number[x+1])
{
//swap the two numbers
temp = number[x];
number[x]=number[x+1];
number[x+1]=temp;
}
}
}
cout<<"nn Descending order :"<<"nn";
//output in descending order
for(i=0;i<max;i++)
{
cout<<number[i]<<",";
}
//ascending order sort
for(int j =0;j<max;j++)
{
2. for(i=0;i<max;i++)
{
if(number[i]>number[i+1])
{
//swap the two numbers
temp = number[i];
number[i]=number[i+1];
number[i+1]=temp;
}
}
}
cout<<"nn Ascending order :"<<"nn";
//output in ascending order
for(i=0;i<max;i++)
{
cout<<number[i]<<",";
}
return 0;
}
PROGRAM2
#include<iostream>
#include<math.h>
main()
{
double a,b,c,s,area, perimeter;
s=(a+b+c)*0.5;
area=sqrt(s*(s-a)*(s-b)*(s-c));
perimeter=a+b+c;
cout <"nter a"
cin>>a;
cout<<"Enter b";
cin>>b;
cout<<"Enter c";
cin>>c;
cout<<"Area is"<<""<<area<<"n";
cout<<"Perimeter is"<<""<<perimeter<<"n";
}
PROGRAM3
/*
* Salary allocating system using a Boolean expression.
*/
#include <iostream.h>
int main()
{
int pay;
int HourlySalary;
3. int TotalHours;
cout<<:"Enter salary";
cin>>HourlySalary;
cout<<endl;
cout<<"Enter the total hours worked"<<endl;
cin>>TotalHours;
if(TotalHours<=40)
pay=HourlySalary*TotalHours;
else
pay=(40.0*HourlySalary)+(TotalHours-40)*(HourlySalary*1.5);
cout<<endl;
cout<<"Worker’s pay is $"<<pay;
cout<<endl;
return 0;
}
http://programming-
technique.blogspot.com/search/label/Android%20Application%20Development
IMPLEMENTATION OF ADTs
(a) IMPLEMENTATION OF TREE ADT
Example 1
#include <iostream>
#include <cstdlib>
usingnamespace std;
classBinarySearchTree
{
private:
structtree_node
16. postorder( bt ) ;
}
/* insertsa newnode ina binarysearchtree */
voidinsert( struct btreenode **sr,intnum)
{
if ( *sr == NULL )
{
*sr = malloc( sizeof ( structbtreenode ) ) ;
( *sr ) -> leftchild=NULL ;
( *sr ) -> data = num;
( *sr ) -> rightchild=NULL ;
return;
}
else /*search the node to whichnewnode will be attached*/
{
/* if newdata is less,traverse toleft*/
if ( num< ( *sr ) -> data )
insert( &( ( *sr ) -> leftchild),num) ;
else
/* else traverse toright*/
insert( &( ( *sr ) -> rightchild),num) ;
}
return;
}
/* traverse a binarysearchtree in a LDR (Left-Data-Right) fashion*/
voidinorder( struct btreenode *sr)
{
if ( sr != NULL )
{
inorder( sr -> leftchild) ;
/* printthe data of the node whose leftchildisNULLor the pathhas alreadybeentraversed*/
printf ( "t%d",sr -> data ) ;
inorder( sr -> rightchild) ;
}
else
return;
}
/* traverse a binarysearch tree in a DLR (Data-Left-right) fashion*/
voidpreorder( struct btreenode *sr)
{
if ( sr != NULL )
{
17. /* printthe data of a node */
printf ( "t%d",sr -> data ) ;
/* traverse till leftchildisnotNULL*/
preorder( sr -> leftchild) ;
/* traverse till rightchildisnotNULL*/
preorder( sr -> rightchild) ;
}
else
return;
}
/* traverse a binarysearchtree in LRD (Left-Right-Data) fashion*/
voidpostorder( struct btreenode *sr)
{
if ( sr != NULL )
{
postorder( sr -> leftchild) ;
postorder( sr -> rightchild) ;
printf ( "t%d",sr -> data ) ;
}
else
return;
}
(b) IMPLEMENTATION OF STACK ADT
This C++ program implements the follow ing stack operations.
Push
Pop
Top
Empty
ALGORITHM/ STEPS:
Create an array to store the stack elements.
Get the size of the stack.
To push an element into the stack check if the top element is less than the size and increment
the top.
Else print overflow .
To pop an element, check if the stack is empty and decrement the stack.
If all the elements are popped, print underflow .
Find the topmost element in the stack by checking if the size is equal to top.
If the stack is empty print empty, else print not empty.
CODING:
#include<iostream.h>
#include<conio.h>
int max=7;
18. int t=0;
class stack
{
int s[7];
public:
void push(int);
void pop();
void top();
void empty();
void show ();
};
void stack::push(int y) //Push Operation
{
if(t<max)
{
t=t+1;
s[t]=y;
}
else
cout<<endl<<"stack overflow s..."<<endl;
}
void stack::pop() //Pop Operation
{
int item;
if(t>=0)
{
t=t-1;
item=s[t+1];
cout<<endl<<"popped item >>"<<item<<endl;
}
else
cout<<endl<<"stack underflow s"<<endl;
}
void stack::top() //To find the top of the stack
{
if(t>=0)
cout<<endl<<"topmost element >> "<<s[t]<<endl;
else
cout<<endl<<"stack underflow s..."<<endl;
}
void stack::empty() //To check if the stack is empty
{
if(t<0)
cout<<endl<<"stack is empty..."<<endl;
else
cout<<endl<<"stack is not empty..."<<endl;
}
void main()
{
int a,x;
stack s1;
clrscr();
do
{
cout<<"enter an option..."<<endl<<"1-push"<<endl<<"2-pop"<<endl<<"3-top"<<endl<<"4-empty"<<endl;
cout<<"5-end"<<endl;
cin>>a;
cout<<endl;
sw itch(a)
{
19. case 1:
{
cout<<endl<<"enter a value >> "<<endl;
cin>>x;
s1.push(x);
}
break;
case 2:
s1.pop();
break;
case 3:
s1.top();
break;
case 4:
s1.empty();
break;
}
}
w hile(a!=5);
getch();
}
(c ) IMPLEMENTATION OF QUEUE ADT
Example 1
Algorithm for Implementation of Queue in C++
1. Declare and initialize neccessary variables, front = 0, rear = -1 etc.
2. For enque operation,
If rear >= MAXSIZE - 1
print "Queue is full"
Else
- Increment rear by 1 i.e. rear = rear + 1;
- queue[rear] = item;
3. For next enqueue operation, goto step 2.
4. For dequeue operation
If front > rear
print "Queue is Empty"
Else
- item = queue[front]
- increment front by 1 i.e. front = front + 1
5. For dequeue next data items, goto step 4.
6. Stop
Source Code:
#include<iostream>
#include<cstdlib>
#define MAX_SIZE 10
20. using namespace std;
class Queue{
private:
int item[MAX_SIZE];
int rear;
int front;
public:
Queue();
void enqueue(int);
int dequeue();
int size();
void display();
bool isEmpty();
bool isFull();
};
Queue::Queue(){
rear = -1;
front = 0;
}
void Queue::enqueue(int data){
item[++rear] = data;
}
int Queue::dequeue(){
return item[front++];
}
void Queue::display(){
if(!this->isEmpty()){
for(int i=front; i<=rear; i++)
cout<<item[i]<<endl;
}else{
cout<<"Queue Underflow"<<endl;
}
}
int Queue::size(){
return (rear - front + 1);
}
bool Queue::isEmpty(){
if(front>rear){
return true;
}else{
return false;
}
}
bool Queue::isFull(){
if(this->size()>=MAX_SIZE){
return true;
}else{
return false;
}
}
int main(){
Queue queue;
int choice, data;
while(1){
21. cout<<"n1. Enqueuen2. Dequeuen3. Sizen4. Display all elementn5. Quit";
cout<<"nEnter your choice: ";
cin>>choice;
switch(choice){
case 1:
if(!queue.isFull()){
cout<<"nEnter data: ";
cin>>data;
queue.enqueue(data);
}else{
cout<<"Queue is Full"<<endl;
}
break;
case 2:
if(!queue.isEmpty()){
cout<<"The data dequeued is :"<<queue.dequeue();
}else{
cout<<"Queue is Emtpy"<<endl;
}
break;
case 3:
cout<<"Size of Queue is "<<queue.size();
break;
case 4:
queue.display();
break;
case 5:
exit(0);
break;
}
}
return 0;
}
Example 2
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
23. EXAMPLE 3
#include<stdio.h>
#include<conio.h>
#include<process.h>
int queue[5];
long front,rear;
void initqueue();
void display();
void main()
{
int choice,info;
clrscr();
while(1)
{
clrscr();
printf(" MENU n");
printf("1.Insert an element in queuen");
printf("2.Delete an element from queuen");
printf("3.Display the queuen");
printf("4.Exit!n");
printf("Your choice: ");
scanf("%i",&choice);
//Coding by: Snehil Khanor