Merge pull request #70 from christianbender/master

changed the directory structure
This commit is contained in:
Christian Bender 2017-12-23 18:33:38 +01:00 committed by GitHub
commit 6eae74cc96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 2290 additions and 0 deletions

View File

@ -0,0 +1,232 @@
#include<iostream>
using namespace std;
struct node
{
int val;
node *left;
node *right;
};
struct queue
{
node *t[100];
int front;
int rear;
};
queue q;
void enqueue(node *n)
{
q.t[q.rear++]=n;
}
node * dequeue()
{
return (q.t[q.front++]);
}
void Insert(node *n, int x)
{
if (x<n->val)
{
if (n->left==NULL)
{
node *temp=new node;
temp->val=x;
temp->left=NULL;
temp->right=NULL;
n->left=temp;
}
else
{
Insert(n->left, x);
}
}
else
{
if (n->right==NULL)
{
node *temp=new node;
temp->val=x;
temp->left=NULL;
temp->right=NULL;
n->left=temp;
}
else
{
Insert(n->right, x);
}
}
}
int findMaxInLeftST(node *n)
{
while(n->right!=NULL)
{
n=n->right;
}
return n->val;
}
void Remove(node *p, node *n, int x)
{
if (n->val==x)
{
if (n->right==NULL && n->left==NULL)
{
if (x<p->val)
{
p->right=NULL;
}
else
{
p->left=NULL;
}
}
else if (n->right==NULL)
{
if (x<p->val)
{
p->right=n->left;
}
else
{
p->left=n->left;
}
}
else if (n->left==NULL)
{
if (x<p->val)
{
p->right=n->right;
}
else
{
p->left=n->right;
}
}
else
{
int y=findMaxInLeftST(n->left);
n->val=y;
Remove(n, n->right, y);
}
}
else if (x<n->val)
{
Remove(n, n->left, x);
}
else
{
Remove(n, n->right, x);
}
}
void BFT(node *n)
{
if(n!=NULL)
{
cout<<n->val<<" ";
enqueue(n->left);
enqueue(n->right);
BFT(dequeue());
}
}
void Pre(node *n)
{
if (n!=NULL)
{
cout<<n->val<<" ";
Pre(n->left);
Pre(n->right);
}
}
void In(node *n)
{
if (n!=NULL)
{
In(n->left);
cout<<n->val<<" ";
In(n->right);
}
}
void Post(node *n)
{
if (n!=NULL)
{
Post(n->left);
Post(n->right);
cout<<n->val<<" ";
}
}
int main()
{
q.front=0;
q.rear=0;
int value;
int ch;
node *root=new node;
cout<<"\nEnter the value of root node :";
cin>>value;
root->val=value;
root->left=NULL;
root->right=NULL;
do
{
cout<<"\n1. Insert";
cout<<"\n2. Delete";
cout<<"\n3. Breadth First";
cout<<"\n4. Preorder Depth First";
cout<<"\n5. Inorder Depth First";
cout<<"\n6. Postorder Depth First";
cout<<"\nEnter Your Choice : ";
cin>>ch;
int x;
switch(ch)
{
case 1:
cout<<"\nEnter the value to be Inserted : ";
cin>>x;
Insert(root, x);
break;
case 2:
cout<<"\nEnter the value to be Deleted : ";
cin>>x;
Remove(root, root, x);
break;
case 3:
BFT(root);
break;
case 4:
Pre(root);
break;
case 5:
In(root);
break;
case 6:
Post(root);
break;
}
}
while(ch!=0);
}

View File

@ -0,0 +1,125 @@
#include<iostream>
using namespace std;
struct node
{
int val;
node *prev;
node *next;
};
node *start;
void insert(int x)
{
node *t=start;
if (start!=NULL)
{
while(t->next!=NULL)
{
t=t->next;
}
node *n= new node;
t->next=n;
n->prev=t;
n->val=x;
n->next=NULL;
}
else
{
node *n= new node;
n->val=x;
n->prev=NULL;
n->next=NULL;
start=n;
}
}
void remove(int x)
{
node *t=start;
while(t->val!=x)
{
t=t->next;
}
t->prev->next=t->next;
t->next->prev=t->prev;
delete t;
}
void search(int x)
{
node *t= start;
int found =0;
while(t!=NULL)
{
if(t->val==x)
{
cout<<"\nFound";
found=1;
break;
}
t=t->next;
}
if(found==0)
{
cout<<"\nNot Found";
}
}
void show()
{
node *t=start;
while(t!=NULL)
{
cout<<t->val<<"\t";
t=t->next;
}
}
void reverseShow()
{
node *t=start;
while(t->next!=NULL)
{
t=t->next;
}
while(t!=NULL)
{
cout<<t->val<<"\t";
t=t->prev;
}
}
int main()
{
int choice, x;
do
{
cout<<"\n1. Insert";
cout<<"\n2. Delete";
cout<<"\n3. Search";
cout<<"\n4. Forward print";
cout<<"\n5. Reverse print";
cout<<"\n\nEnter you choice : ";
cin>>choice;
switch (choice)
{
case 1 : cout<<"\nEnter the element to be inserted : ";
cin>>x;;
insert(x); break;
case 2 : cout<<"\nEnter the element to be removed : ";
cin>>x;
remove(x); break;
case 3 : cout<<"\nEnter the element to be searched : ";
cin>>x;
search(x); break;
case 4 : show(); break;
case 5 : reverseShow(); break;
}
}
while(choice!=0);
return 0;
}

View File

@ -0,0 +1,107 @@
#include<iostream>
using namespace std;
struct node
{
int val;
node *next;
};
node *start;
void insert(int x)
{
node *t=start;
if (start!=NULL)
{
while(t->next!=NULL)
{
t=t->next;
}
node *n= new node;
t->next=n;
n->val=x;
n->next=NULL;
}
else
{
node *n= new node;
n->val=x;
n->next=NULL;
start=n;
}
}
void remove(int x)
{
node *t=start;
node *p;
while(t->val!=x)
{
p=t;
t=t->next;
}
p->next=t->next;
delete t;
}
void search(int x)
{
node *t= start;
int found =0;
while(t!=NULL)
{
if(t->val==x)
{
cout<<"\nFound";
found=1;
break;
}
t=t->next;
}
if(found==0)
{
cout<<"\nNot Found";
}
}
void show()
{
node *t=start;
while(t!=NULL)
{
cout<<t->val<<"\t";
t=t->next;
}
}
int main()
{
int choice, x;
do
{
cout<<"\n1. Insert";
cout<<"\n2. Delete";
cout<<"\n3. Search";
cout<<"\n4. Print";
cout<<"\n\nEnter you choice : ";
cin>>choice;
switch (choice)
{
case 1 : cout<<"\nEnter the element to be inserted : ";
cin>>x;;
insert(x); break;
case 2 : cout<<"\nEnter the element to be removed : ";
cin>>x;
remove(x); break;
case 3 : cout<<"\nEnter the element to be searched : ";
cin>>x;
search(x); break;
case 4 : show(); break;
}
}
while(choice!=0);
return 0;
}

View File

@ -0,0 +1,185 @@
#include<iostream>
using namespace std;
struct list
{
int data[50];
int top=0;
bool isSorted=false;
int BinarySearch(int *array, int first, int last, int x)
{
if(last<first)
{
return -1;
}
int mid=(first+last)/2;
if(array[mid]==x)
return mid;
else if(x<array[mid])
return (BinarySearch(array, first, mid-1, x));
else if(x>array[mid])
return (BinarySearch(array, mid+1, last, x));
}
int LinarSearch(int *array, int x)
{
for (int i = 0; i < top; i++)
{
if (array[i]==x)
{
return i;
}
}
return -1;
}
int Search(int x)
{
int pos=-1;
if (isSorted)
{
pos=BinarySearch(data, 0, top-1, x);
}
else
{
pos=LinarSearch(data, x);
}
if (pos!=-1)
{
cout<<"\nElement found at position : "<<pos;
}
else
{
cout<<"\nElement not found";
}
return pos;
}
void Sort()
{
int i, j, pos;
for(i=0; i<top; i++)
{
int min=data[i];
for(j=i+1; j<top; j++)
{
if(data[j]<min)
{
pos=j;
min=data[pos];
}
}
int temp=data[i];
data[i]=data[pos];
data[pos]=temp;
}
isSorted=true;
}
void insert(int x)
{
if(!isSorted)
{
if(top==49)
{
cout<<"\nOverflow";
}
else
{
data[top]=x;
top++;
}
}
else
{
int pos=0;
for (int i = 0; i < top-1; i++)
{
if(data[i]<=x && x<=data[i+1])
{
pos=i+1;
break;
}
}
if (pos==0)
{
pos=top-1;
}
for (int i = top; i > pos; i--)
{
data[i]=data[i-1];
}
top++;
data[pos]=x;
}
}
void Remove(int x)
{
int pos=Search(x);
cout<<"\n"<<data[pos]<<" deleted";
for (int i = pos; i < top; i++)
{
data[i]=data[i+1];
}
top--;
}
void Show()
{
for (int i = 0; i < top; i++)
{
cout<<data[i]<<"\t";
}
}
};
int main()
{
list L;
int choice;
int x;
do
{
cout<<"\n1.Insert";
cout<<"\n2.Delete";
cout<<"\n3.Search";
cout<<"\n4.Sort";
cout<<"\n5.Print";
cout<<"\n\nEnter Your Choice : ";
cin>>choice;
switch (choice)
{
case 1: cout<<"\nEnter the element to be inserted : ";
cin>>x;
L.insert(x);
break;
case 2: cout<<"\nEnter the element to be removed : ";
cin>>x;
L.Remove(x);
break;
case 3: cout<<"\nEnter the element to be searched : ";
cin>>x;
L.Search(x);
break;
case 4: L.Sort();
break;
case 5: L.Show();
break;
}
}
while(choice!=0);
return 0;
}

View File

@ -0,0 +1,78 @@
#include<iostream>
using namespace std;
int queue[10];
int front=0;
int rear=0;
void Enque(int x)
{
if(rear==10)
{
cout<<"\nOverflow";
}
else
{
queue[rear++]=x;
}
}
void Deque()
{
if (front==rear)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\n"<<queue[front++]<<" deleted";
for (int i = front; i < rear; i++)
{
queue[i-front]=queue[i];
}
rear=rear-front;
front=0;
}
}
void show()
{
for (int i = front; i < rear; i++)
{
cout<<queue[i]<<"\t";
}
}
int main()
{
int ch, x;
do
{
cout<<"\n1. Enque";
cout<<"\n2. Deque";
cout<<"\n3. Print";
cout<<"\nEnter Your Choice : ";
cin>>ch;
if (ch==1)
{
cout<<"\nInsert : ";
cin>>x;
Enque(x);
}
else if (ch==2)
{
Deque();
}
else if (ch==3)
{
show();
}
}
while(ch!=0);
return 0;
}

View File

@ -0,0 +1,90 @@
#include<iostream>
using namespace std;
struct node
{
int val;
node *next;
};
node *front, *rear;
void Enque(int x)
{
if (rear==NULL)
{
node *n= new node;
n->val=x;
n->next=NULL;
rear=n;
front=n;
}
else
{
node *n = new node;
n->val=x;
n->next=NULL;
rear->next=n;
rear=n;
}
}
void Deque()
{
if (rear==front)
{
cout<<"\nUnderflow";
}
else
{
node *t = front;
cout<<"\n"<<t->val<<" deleted";
front=front->next;
delete t;
}
}
void show()
{
node *t=front;
while(t!=NULL)
{
cout<<t->val<<"\t";
t=t->next;
}
}
int main()
{
int ch, x;
do
{
cout<<"\n1. Enque";
cout<<"\n2. Deque";
cout<<"\n3. Print";
cout<<"\nEnter Your Choice : ";
cin>>ch;
if (ch==1)
{
cout<<"\nInsert : ";
cin>>x;
Enque(x);
}
else if (ch==2)
{
Deque();
}
else if (ch==3)
{
show();
}
}
while(ch!=0);
return 0;
}

View File

@ -0,0 +1,77 @@
#include<iostream>
using namespace std;
int stack[10];
int top=0;
void push(int x)
{
if(top==10)
{
cout<<"\nOverflow";
}
else
{
stack[top++]=x;
}
}
void pop()
{
if (top==0)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\n"<<stack[--top]<<" deleted";
}
}
void show()
{
for (int i = 0; i < top; i++)
{
cout<<stack[i]<<"\n";
}
}
void topmost()
{
cout << "\nTopmost element: "<<stack[top-1];
}
int main()
{
int ch, x;
do
{
cout<<"\n1. Push";
cout<<"\n2. Pop";
cout<<"\n3. Print";
cout<<"\n4. Print topmost element:";
cout<<"\nEnter Your Choice : ";
cin>>ch;
if (ch==1)
{
cout<<"\nInsert : ";
cin>>x;
push(x);
}
else if (ch==2)
{
pop();
}
else if (ch==3)
{
show();
}
else if(ch==4)
{
topmost();
}
}
while(ch!=0);
return 0;
}

View File

@ -0,0 +1,75 @@
#include<iostream>
using namespace std;
struct node
{
int val;
node *next;
};
node *top;
void push(int x)
{
node *n = new node;
n->val=x;
n->next=top;
top=n;
}
void pop()
{
if (top==NULL)
{
cout<<"\nUnderflow";
}
else
{
node *t = top;
cout<<"\n"<<t->val<<" deleted";
top=top->next;
delete t;
}
}
void show()
{
node *t=top;
while(t!=NULL)
{
cout<<t->val<<"\n";
t=t->next;
}
}
int main()
{
int ch, x;
do
{
cout<<"\n1. Push";
cout<<"\n2. Pop";
cout<<"\n3. Print";
cout<<"\nEnter Your Choice : ";
cin>>ch;
if (ch==1)
{
cout<<"\nInsert : ";
cin>>x;
push(x);
}
else if (ch==2)
{
pop();
}
else if (ch==3)
{
show();
}
}
while(ch!=0);
return 0;
}

160
Datastructures/Tree.cpp Normal file
View File

@ -0,0 +1,160 @@
#include<iostream>
using namespace std;
struct node
{
int val;
node *left;
node *right;
};
struct queue
{
node *t[100];
int front;
int rear;
};
queue q;
void enqueue(node *n)
{
q.t[q.rear++]=n;
}
node * dequeue()
{
return (q.t[q.front++]);
}
void CreateTree(node *curr, node *n, int x, char pos)
{
if(n!=NULL)
{
char ch;
cout<<"\nLeft or Right of "<<n->val<<" : ";
cin>>ch;
if(ch=='l')
CreateTree(n, n->left, x, ch);
else if(ch=='r')
CreateTree(n, n->right, x, ch);
}
else
{
node *t=new node;
t->val=x;
t->left=NULL;
t->right=NULL;
if (pos=='l')
{
curr->left=t;
}
else if(pos=='r')
{
curr->right=t;
}
}
}
void BFT(node *n)
{
if(n!=NULL)
{
cout<<n->val<<" ";
enqueue(n->left);
enqueue(n->right);
BFT(dequeue());
}
}
void Pre(node *n)
{
if (n!=NULL)
{
cout<<n->val<<" ";
Pre(n->left);
Pre(n->right);
}
}
void In(node *n)
{
if (n!=NULL)
{
In(n->left);
cout<<n->val<<" ";
In(n->right);
}
}
void Post(node *n)
{
if (n!=NULL)
{
Post(n->left);
Post(n->right);
cout<<n->val<<" ";
}
}
int main()
{
q.front=0;
q.rear=0;
int value;
int ch;
node *root=new node;
cout<<"\nEnter the value of root node :";
cin>>value;
root->val=value;
root->left=NULL;
root->right=NULL;
do
{
cout<<"\n1. Insert : ";
cout<<"\n2. Breadth First";
cout<<"\n3. Preorder Depth First";
cout<<"\n4. Inorder Depth First";
cout<<"\n5. Postorder Depth First";
cout<<"\nEnter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
int x;
char pos;
cout<<"\nEnter the value to be Inserted : ";
cin>>x;
cout<<"\nLeft or Right of Root : ";
cin>>pos;
if(pos=='l')
CreateTree(root, root->left, x, pos);
else if(pos=='r')
CreateTree(root, root->right, x, pos);
break;
case 2:
BFT(root);
break;
case 3:
Pre(root);
break;
case 4:
In(root);
break;
case 5:
Post(root);
break;
}
}
while(ch!=0);
}

View File

@ -0,0 +1,31 @@
#include<iostream>
using namespace std;
int main(){
int n,k;
cout<<"Enter size of array=\t";
cin>>n;
cout<<"Enter Number of indeces u want to rotate the array to left=\t";
cin>>k;
int a[n];
cout<<"Enter elements of array=\t";
for(int i=0;i<n;i++){
cin>>a[i];
}
int temp=0;
for(int i=0;i<k;i++){
temp=a[0];
for(int j=0;j<n;j++){
if(j==n-1){
a[n-1]=temp;
}
else{
a[j]=a[j+1];
}
}
}cout<<"Your rotated array is=\t";
for(int j=0;j<n;j++){
cout<<a[j]<<" ";
}
getchar();
return 0;
}

View File

@ -0,0 +1,31 @@
#include <iostream>
using namespace std;
int main(){
int n,k;
cout<<"Enter size of array=\t";
cin>>n;
cout<<"Enter Number of indices u want to rotate the array to right=\t";
cin>>k;
int a[n];
cout<<"Enter elements of array=\t";
for(int i=0;i<n;i++)
cin>>a[i];
int temp=0;
for(int i=0;i<k;i++){
temp=a[n-1];
for(int j=n-1;j>=0;j--){
if(j==0){
a[j]=temp;
}
else{
a[j]=a[j-1];}
}
}
cout<<"Your rotated array is=\t";
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
}

View File

@ -0,0 +1,109 @@
#include<iostream>
using namespace std;
struct node
{
int val;
node *next;
};
node *start;
void insert(int x)
{
node *t=start;
if (start!=NULL)
{
while(t->next!=start)
{
t=t->next;
}
node *n= new node;
t->next=n;
n->val=x;
n->next=start;
}
else
{
node *n= new node;
n->val=x;
start=n;
n->next=start;
}
}
void remove(int x)
{
node *t=start;
node *p;
while(t->val!=x)
{
p=t;
t=t->next;
}
p->next=t->next;
delete t;
}
void search(int x)
{
node *t= start;
int found =0;
while(t->next!=start)
{
if(t->val==x)
{
cout<<"\nFound";
found=1;
break;
}
t=t->next;
}
if(found==0)
{
cout<<"\nNot Found";
}
}
void show()
{
node *t=start;
do
{
cout<<t->val<<"\t";
t=t->next;
}
while(t!=start);
}
int main()
{
int choice, x;
do
{
cout<<"\n1. Insert";
cout<<"\n2. Delete";
cout<<"\n3. Search";
cout<<"\n4. Print";
cout<<"\n\nEnter you choice : ";
cin>>choice;
switch (choice)
{
case 1 : cout<<"\nEnter the element to be inserted : ";
cin>>x;
insert(x); break;
case 2 : cout<<"\nEnter the element to be removed : ";
cin>>x;
remove(x); break;
case 3 : cout<<"\nEnter the element to be searched : ";
cin>>x;
search(x); break;
case 4 : show(); break;
}
}
while(choice!=0);
return 0;
}

View File

@ -0,0 +1,77 @@
#include<iostream>
using namespace std;
int queue[10];
int front=0;
int rear=0;
int count=0;
void Enque(int x)
{
if(count==10)
{
cout<<"\nOverflow";
}
else
{
queue[rear]=x;
rear=(rear+1)%10;
count++;
}
}
void Deque()
{
if (front==rear)
{
cout<<"\nUnderflow";
}
else
{
cout<<"\n"<<queue[front]<<" deleted";
front=(front+1)%10;
count--;
}
}
void show()
{
for (int i = 0; i<count; i++)
{
cout<<queue[(i+front)%10]<<"\t";
}
}
int main()
{
int ch, x;
do
{
cout<<"\n1. Enque";
cout<<"\n2. Deque";
cout<<"\n3. Print";
cout<<"\nEnter Your Choice : ";
cin>>ch;
if (ch==1)
{
cout<<"\nInsert : ";
cin>>x;
Enque(x);
}
else if (ch==2)
{
Deque();
}
else if (ch==3)
{
show();
}
}
while(ch!=0);
return 0;
}

View File

@ -0,0 +1,30 @@
#include <iostream>
int main()
{
int i,j,m,n;
cout <<"Enter size of array 1:";
cin >> m;
cout <<"Enter size of array 2:";
cin >> n;
int a[m];
int b[n];
cout <<"Enter elements of array 1:";
for(i=0;i<m;i++)
cin >> a[i];
for(i=0;i<n;i++)
cin >> b[i];
i=0;j=0;
while((i<m)&&(j<n))
{
if(a[i]<b[j])
i++;
else if(a[i]>b[j])
j++;
else
{
cout << a[i++]<<" ";
j++;
}
}
return 0;
}

View File

@ -0,0 +1,80 @@
#include<iostream>
using namespace std;
struct node
{
int val;
node *next;
};
node *start;
void insert(int x)
{
node *t=start;
if (start!=NULL)
{
while(t->next!=NULL)
{
t=t->next;
}
node *n= new node;
t->next=n;
n->val=x;
n->next=NULL;
}
else
{
node *n= new node;
n->val=x;
n->next=NULL;
start=n;
}
}
void reverse(node *p, node *q)
{
if (q->next == NULL)
{
q->next=p;
p->next=NULL;
start=q;
return;
}
else
{
reverse(q, q->next);
q->next=p;
p->next=NULL;
}
}
void show()
{
node *t=start;
while(t!=NULL)
{
cout<<t->val<<"\t";
t=t->next;
}
}
int main()
{
insert(1);
insert(2);
insert(3);
insert(4);
insert(5);
insert(6);
reverse(start, start->next);
show();
return 0;
}

View File

@ -0,0 +1,33 @@
#include <iostream>
int main()
{
int m,n,i=0,j=0;
cout << "Enter size of both arrays:";
cin >> m >> n;
int a[m];
int b[n];
cout << "Enter elements of array 1:";
for(i=0;i<m;i++)
cin >>a[i];
cout << "Enter elements of array 2:";
for(i=0;i<n;i++)
cin >> b[i];
i=0;j=0;
while((i<m)&&(j<n))
{
if(a[i]<b[j])
cout << a[i++] <<" ";
else if(a[i]>b[j])
cout << b[j++] <<" ";
else
{
cout << a[i++];
j++;
}
}
while(i<m)
cout<< a[i++] <<" ";
while(j<n)
cout<< b[j++]<<" ";
return 0;
}

17
Others/Buzz_number.cpp Normal file
View File

@ -0,0 +1,17 @@
//A buzz number is a number that is either divisble by 7 or has last digit as 7.
#include <iostream>
using namespace std;
int main()
{
int n,t;
cin >> t;
while(t--)
{
cin >> n;
if((n%7==0)||(n%10==7))
cout << n << " is a buzz number" << endl;
else
cout << n << " is not a buzz number" << endl;
}
return 0;
}

View File

@ -0,0 +1,20 @@
#include<iostream>
using namespace std;
int main()
{
int number;
cin>>number;
int remainder,binary=0,var=1;
do{
remainder=number%2;
number=number/2;
binary=binary+(remainder*var);
var=var*10;
}
while(number>0);
cout<<binary;
}

View File

@ -0,0 +1,51 @@
#include <iostream>
using namespace std;
void main(void)
{
int valueToConvert = 0; //Holds user input
int hexArray[8]; //Contains hex values backwards
int i = 0; //counter
int lValue = 0; //Last Value of Hex result
cout << "Enter a Decimal Value" << endl; //Displays request to stdout
cin >> valueToConvert; //Stores value into valueToConvert via user input
while (valueToConvert > 0) //Dec to Hex Algorithm
{
lValue = valueToConvert % 16; //Gets remainder
valueToConvert = valueToConvert / 16;
hexArray[i] = lValue; //Stores converted values into an array
i++;
}
cout << "Hex Value: ";
while (i > 0)
{
//Displays Hex Letters to stdout
switch (hexArray[i - 1]) {
case 10:
cout << "A";
break;
case 11:
cout << "B";
break;
case 12:
cout << "C";
break;
case 13:
cout << "D";
break;
case 14:
cout << "E";
break;
case 15:
cout << "F";
break;
default:
cout << hexArray[i - 1]; //if not an int 10 - 15, displays int value
}
i--;
}
cout << endl;
}

View File

@ -0,0 +1,23 @@
//This program aims at calculating the GCD of n numbers by division method
#include <iostream>
using namepsace std;
int main()
{
cout <<"Enter value of n:"<<endl;
cin >> n;
int a[n];
int i,j,gcd;
cout << "Enter the n numbers:" << endl;
for(i=0;i<n;i++)
cin >> a[i];
j=1; //to access all elements of the array starting from 1
gcd=a[0];
while(j<n)
{
if(a[j]%gcd==0) //value of gcd is as needed so far
j++; //so we check for next element
else
gcd=a[j]%gcd; //calculating GCD by division method
}
cout << "GCD of entered n numbers:" << gcd;
}

26
Others/Happy_number.cpp Normal file
View File

@ -0,0 +1,26 @@
/* A happy number is a number whose sum of digits is calculated until the sum is a single digit,
and this sum turns out to be 1 */
#include <iostream>
using namespace std;
int main()
{
int n,k,s=0,d;
cout << "Enter a number:";
cin >> n;
s=0;k=n;
while(k>9)
{
while(k!=0)
{
d=k%10;
s+=d;
k/=10;
}
k=s;
s=0;
}
if(k==1)
cout << n << " is a happy number" << endl;
else
cout << n << " is not a happy number" << endl;
}

View File

@ -0,0 +1,65 @@
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
char stack[100];
int top=0;
void push(char ch)
{
stack[top++]=ch;
}
char pop()
{
return stack[--top];
}
bool check(char x, char y)
{
if ((x=='(' && y==')') || (x=='{' && y=='}') || (x=='[' && y==']') || (x=='<' && y=='>'))
{
return true;
}
else
{
return false;
}
}
int main()
{
char exp[100];
cout<<"Enter The Expression : ";
gets(exp);
for (int i = 0; i < strlen(exp); i++)
{
if (exp[i]=='(' || exp[i]=='{' || exp[i]=='[' || exp[i]=='<')
{
push(exp[i]);
}
else if (exp[i]==')' || exp[i]=='}' || exp[i]==']' || exp[i]=='>')
{
if(!check(pop(), exp[i]))
{
cout<<"\nWrong Expression";
exit(0);
}
}
}
if(top==0)
{
cout<<"Correct Expression";
}
else
{
cout<<"\nWrong Expression";
}
return 0;
}

28
Others/Sparse matrix.cpp Normal file
View File

@ -0,0 +1,28 @@
/*A sparse matrix is a matrix which has number of zeroes greater than (m*n)/2,
where m and n are the dimensions of the matrix.*/
#include <iostream>
int main()
{
int m,n,i,j,c=0;
cout << "Enter dimensions of matrix:";
cin >> m >> n;
int a[m][n];
cout << "Enter matrix elements:";
for(i=0;<m;i++)
{
for(j=0;j<n;j++)
cin >> a[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==0)
c++; //Counting number of zeroes
}
}
if(c>((m*n)/2)) //Checking for sparse matrix
cout << "Sparse matrix";
else
cout << "Not a sparse matrix";
}

View File

@ -0,0 +1,60 @@
#include <iostream>
using namespace std;
Multiply(int A[][], int B[][], int n)
{
if (n==2)
{
int p1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
int p2= (a[1][0]+a[1][1])*b[0][0];
int p3= a[0][0]*(b[0][1]-b[1][1]);
int p4= a[1][1]*(b[1][0]-b[0][0]);
int p5= (a[0][0]+a[0][1])*b[1][1];
int p6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
int p7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
int c[n][n];
c[0][0]=p1+p4-p5+p7;
c[0][1]=p3+p5;
c[1][0]=p2+p4;
c[1][1]=p1-p2+p3+p6;
return c[][];
}
else
{
}
}
int main()
{
int p,q,r,s;
cout<<"Enter the dimensions of Matrices";
cin>>n;
int A[n][n],;
int B[n][n],;
cout<<"Enter the elements of Matrix A";
for (int i = 0; i < n; i++)
{
for (int j = 0; j <n ; j++)
{
cin>>A[i][j];
}
}
cout<<"Enter the elements of Matrix B";
for (int i = 0; i < n; i++)
{
for (int j = 0; j <n ; j++)
{
cin>>B[i][j];
}
}
Multiply(A, B, n);
return 0;
}

88
Others/Tower of Hanoi.cpp Normal file
View File

@ -0,0 +1,88 @@
#include<iostream>
using namespace std;
struct tower
{
int values[10];
int top;
}F, U, T;
void show()
{
cout<<"\n\n\tF : ";
for(int i=0; i<F.top; i++)
{
cout<<F.values[i]<<"\t";
}
cout<<"\n\tU : ";
for(int i=0; i<U.top; i++)
{
cout<<U.values[i]<<"\t";
}
cout<<"\n\tT : ";
for(int i=0; i<T.top; i++)
{
cout<<T.values[i]<<"\t";
}
}
void mov(tower &From, tower &To)
{
--From.top;
To.values[To.top]=From.values[From.top];
++To.top;
}
void TH(int n, tower &From, tower &Using, tower &To)
{
if (n==1)
{
mov(From, To);
show();
}
else
{
TH(n-1, From, To, Using);
mov(From, To);
show();
TH(n-1, Using, From, To);
}
}
int main()
{
F.top=0;
U.top=0;
T.top=0;
int no;
cout << "\nEnter number of discs : " ;
cin >> no;
for (int i = no; i >0; i--)
{
F.values[F.top++]=i;
};
show();
TH(no, F, U, T);
return 0;
}

100
Others/fibonacci.cpp Normal file
View File

@ -0,0 +1,100 @@
#include <iostream>
using namespace std;
/*
Efficient method for finding nth term in a fibonacci number sequence.
Uses Dvide and conquer approach
Enter Values from 1
Eg-
1st term = 0;
2nd term = 1;
3rd term = 1;
.
.
.
.
*/
int a[2][2] = {{1,1},{1,0}};//fibonacci matrix
int ans[2][2] = {{1,1},{1,0}};//final ans matrix
/*
Working Principal:
[F(k+1) F(k)] [1 1]^k
| | = | |
[F(k) F(k-1)] [1 0]
where F(k) is the kth term of the Fibonacci Sequence.
*/
void product(int b[][2],int k[][2])
{
/*
Function for computing product of the two matrices b and k
and storing them into the variable ans.
Implementation :
Simple matrix multiplication of two (2X2) matrices.
*/
int c[2][2];//temporary stores the answer
c[0][0] = b[0][0]*k[0][0]+b[0][1]*k[1][0];
c[0][1] = b[0][0]*k[0][1]+b[0][1]*k[1][1];
c[1][0] = b[1][0]*k[0][0]+b[1][1]*k[1][0];
c[1][1] = b[1][0]*k[0][1]+b[1][1]*k[1][1];
ans[0][0] = c[0][0];
ans[0][1] = c[0][1];
ans[1][0] = c[1][0];
ans[1][1] = c[1][1];
}
void power_rec(int n)
{
/*
Function for calculating A^n(exponent) in a recursive fashion
Implementation:
A^n = { A^(n/2)*A^(n/2) if n is even
{ A^((n-1)/2)*A^((n-1)/2)*A if n is odd
*/
if((n == 1)||(n==0))
return;
else
{
if((n%2) == 0)
{
power_rec(n/2);
product(ans,ans);
}
else
{
power_rec((n-1)/2);
product(ans,ans);
product(ans,a);
}
}
}
int main()
{
//Main Function
cout <<"Enter the value of n\n";
int n;
cin >>n;
if(n == 1)
{
cout<<"Ans: 0"<<endl;
}
else
{
power_rec(n-1);
cout <<"Ans :"<<ans[0][1]<<endl;
}
return 0;
}

View File

@ -0,0 +1,60 @@
/*
* Sieve of Eratosthenes is an algorithm to find the primes
* that is between 2 to N (as defined in main).
*
* Time Complexity : O(N)
* Space Complexity : O(N)
*/
#include <iostream>
using namespace std;
#define MAX 10000000
int primes[MAX];
/*
* This is the function that finds the primes and eliminates
* the multiples.
*/
void sieve(int N)
{
primes[0] = 1;
primes[1] = 1;
for(int i=2;i<=N;i++)
{
if(primes[i] == 1) continue;
for(int j=i+i;j<=N;j+=i)
primes[j] = 1;
}
}
/*
* This function prints out the primes to STDOUT
*/
void print(int N)
{
for(int i=0;i<=N;i++)
if(primes[i] == 0)
cout << i << ' ';
cout << '\n';
}
/*
* NOTE: This function is important for the
* initialization of the array.
*/
void init()
{
for(int i=0;i<MAX;i++)
primes[i] = 0;
}
int main()
{
int N = 100;
init();
sieve(N);
print(N);
}

34
Search/Binary Search.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <iostream>
using namespace std;
int binary_search(int a[],int l,int r,int key){
while(l<=r){
int m = l+(r-l)/2;
if(key==a[m])
return m;
else if(key<a[m])
r = m-1;
else
l = m+1;
}
return -1;
}
int main(int argc, char const *argv[])
{
int n,key;
cout<<"Enter size of array: ";
cin>>n;
cout<<"Enter array elements: ";
int a[n];
for (int i = 0; i < n; ++i)
{
cin>>a[i];
}
cout<<"Enter search key: ";
cin>>key;
int res = binary_search(a,0,n-1,key);
if(res != -1)
cout<<key<<" found at index "<<res<<endl;
else
cout<<key<<" not found"<<endl;
return 0;
}

43
Search/Linear Search.cpp Normal file
View File

@ -0,0 +1,43 @@
#include<iostream>
using namespace std;
int LinearSearch(int *array, int key)
{
for (int i = 0; i < 10; ++i)
{
if (array[i]==key)
{
return key;
}
}
return -1;
}
int main()
{
int array[10];
int key;
//Input array
cout<<"\nEnter the Array of 10 numbers : ";
for (int i = 0; i < 10; i++)
{
cin>>array[i];
}
cout<<"\nEnter the number to be searched : ";
cin>>key;
int index=LinearSearch(array, key);
if (index!=-1)
{
cout<<"\nNumber found at index : "<<index;
}
else
{
cout<<"\nNot found";
}
return 0;
}

39
Search/searching.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
char paragraph;
int main()
{
string paragraph;
cout << "Please enter your paragraph: \n";
getline (cin,paragraph);
cout << "\nHello, your paragraph is:\n " << paragraph << "!\n";
cout << "\nThe size of your paragraph = " << paragraph.size() << " characters. \n\n";
if (paragraph.empty())
{
cout << "\nThe paragraph is empty" << endl;
}
else
{
while (true) {
string word;
cout << "Please enter the word you are searching for: ";
getline (cin,word);
cout << "Hello, your word is " << word << "!\n";
if (paragraph.find(word) == string::npos)
{
cout << word << " does not exist in the sentence" << endl;
}
else
{
cout << "The word " << word << " is now found at location " << paragraph.find(word) << endl << endl;
}
system("pause");
}
}
}

116
Search/ternary_search.cpp Normal file
View File

@ -0,0 +1,116 @@
/*
* This is a divide and conquer algorithm.
* It does this by dividing the search space by 3 parts and
* using its property (usually monotonic property) to find
* the desired index.
*
* Time Complexity : O(log3 n)
* Space Complexity : O(1) (without the array)
*/
#include <iostream>
using namespace std;
/*
* The absolutePrecision can be modified to fit preference but
* it is recommended to not go lower than 10 due to errors that
* may occur.
*
* The value of _target should be decided or can be decided later
* by using the variable of the function.
*/
#define _target 10
#define absolutePrecision 10
#define MAX 10000000
int N = 21;
int A[MAX] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,10};
/*
* get_input function is to receive input from standard IO
*/
void get_input()
{
// TODO: Get input from STDIO or write input to memory as done above.
}
/*
* This is the iterative method of the ternary search which returns the index of the element.
*/
int it_ternary_search(int left, int right, int A[],int target)
{
while (1)
{
if(left<right)
{
if(right-left < absolutePrecision)
{
for(int i=left;i<=right;i++)
if(A[i] == target) return i;
return -1;
}
int oneThird = (left+right)/3+1;
int twoThird = (left+right)*2/3+1;
if(A[oneThird] == target) return oneThird;
else if(A[twoThird] == target) return twoThird;
else if(target > A[twoThird]) left = twoThird+1;
else if(target < A[oneThird]) right = oneThird-1;
else left = oneThird+1, right = twoThird-1;
}
else return -1;
}
}
/*
* This is the recursive method of the ternary search which returns the index of the element.
*/
int rec_ternary_search(int left, int right, int A[],int target)
{
if(left<right)
{
if(right-left < absolutePrecision)
{
for(int i=left;i<=right;i++)
if(A[i] == target) return i;
return -1;
}
int oneThird = (left+right)/3+1;
int twoThird = (left+right)*2/3+1;
if(A[oneThird] == target) return oneThird;
if(A[twoThird] == target) return twoThird;
if(target < A[oneThird]) return rec_ternary_search(left, oneThird-1, A, target);
if(target > A[twoThird]) return rec_ternary_search(twoThird+1, right, A, target);
return rec_ternary_search(oneThird+1, twoThird-1, A, target);
}
else return -1;
}
/*
* ternary_search is a template function
* You could either use it_ternary_search or rec_ternary_search according to preference.
*/
void ternary_search(int N,int A[],int target)
{
cout << it_ternary_search(0,N-1,A,target) << '\t';
cout << rec_ternary_search(0,N-1,A,target) << '\t';
cout << '\n';
}
int main()
{
get_input();
ternary_search(N,A,_target);
return 0;
}