From b38473f36bf85f15071315c3f39c784c8c3016a8 Mon Sep 17 00:00:00 2001 From: Raza Khan Date: Sun, 18 Jan 2026 23:51:38 +0530 Subject: [PATCH] Add CPP programs and update contributor file --- CONTRIBUTOR.md | 4 + CPP/2Darray.cpp | 28 ++ CPP/Graph/BFSandDFS.cpp | 108 ++++++++ CPP/Graph/prims algo.cpp | 94 +++++++ CPP/Hanoi.cpp | 16 ++ CPP/LinkedList(Doubly).cpp | 49 ++++ CPP/LinkedList(circular).cpp | 108 ++++++++ CPP/LinkedList.cpp | 201 ++++++++++++++ CPP/Sorting Techniques/HashDp.cpp | 50 ++++ CPP/Sorting Techniques/HashSum.cpp | 35 +++ CPP/Sorting Techniques/Hashmatrix.cpp | 23 ++ CPP/Sorting Techniques/Hashsort.cpp | 37 +++ CPP/Sorting Techniques/bubble.cpp | 38 +++ CPP/Sorting Techniques/count.cpp | 45 +++ CPP/Sorting Techniques/insertion.cpp | 29 ++ CPP/Sorting Techniques/merge.cpp | 48 ++++ CPP/Sorting Techniques/quick.cpp | 43 +++ CPP/Sorting Techniques/selection.cpp | 37 +++ CPP/Tree/Queue.h | 53 ++++ CPP/Tree/avlTree.cpp | 133 +++++++++ CPP/Tree/bst(delete).cpp | 98 +++++++ CPP/Tree/bst.cpp | 120 ++++++++ CPP/Tree/main.cpp | 98 +++++++ CPP/array(merge).cpp | 49 ++++ CPP/array(set).cpp | 70 +++++ CPP/array++.cpp | 383 ++++++++++++++++++++++++++ CPP/array.cpp | 25 ++ CPP/arrayfn.cpp | 70 +++++ CPP/arrayinfun.cpp | 22 ++ CPP/arrayparameter.cpp | 14 + CPP/binarysearch(array).cpp | 47 ++++ CPP/callbyaddress.cpp | 18 ++ CPP/callbyreference.cpp | 18 ++ CPP/callbyvalue.cpp | 18 ++ CPP/class.cpp | 47 ++++ CPP/classwithfn.cpp | 40 +++ CPP/diagonalmatrix.cpp | 61 ++++ CPP/diamondinheritance.cpp | 39 +++ CPP/functions.cpp | 17 ++ CPP/get,set,max,min.cpp | 70 +++++ CPP/heap.cpp | 56 ++++ CPP/inheritance.cpp | 45 +++ CPP/linearsearch(array).cpp | 42 +++ CPP/operation.cpp | 62 +++++ CPP/pointer.cpp | 11 + CPP/queuearr.cpp | 57 ++++ CPP/recursion(factorial).cpp | 27 ++ CPP/recursion(fibonacci).cpp | 17 ++ CPP/recursion(indirect).cpp | 23 ++ CPP/recursion(nCr).cpp | 17 ++ CPP/recursion(nested).cpp | 20 ++ CPP/recursion(power).cpp | 26 ++ CPP/recursion(sum of n).cpp | 27 ++ CPP/recursion(taylorseries).cpp | 21 ++ CPP/recursion.cpp | 16 ++ CPP/refrences.cpp | 11 + CPP/reversearray.cpp | 35 +++ CPP/stack.cpp | 84 ++++++ CPP/staticglobalrecursion.cpp | 18 ++ CPP/structcallbyadd.cpp | 26 ++ CPP/structure.cpp | 20 ++ CPP/structureandfn.cpp | 30 ++ CPP/template.cpp | 39 +++ CPP/treerecursion.cpp | 16 ++ 64 files changed, 3249 insertions(+) create mode 100644 CPP/2Darray.cpp create mode 100644 CPP/Graph/BFSandDFS.cpp create mode 100644 CPP/Graph/prims algo.cpp create mode 100644 CPP/Hanoi.cpp create mode 100644 CPP/LinkedList(Doubly).cpp create mode 100644 CPP/LinkedList(circular).cpp create mode 100644 CPP/LinkedList.cpp create mode 100644 CPP/Sorting Techniques/HashDp.cpp create mode 100644 CPP/Sorting Techniques/HashSum.cpp create mode 100644 CPP/Sorting Techniques/Hashmatrix.cpp create mode 100644 CPP/Sorting Techniques/Hashsort.cpp create mode 100644 CPP/Sorting Techniques/bubble.cpp create mode 100644 CPP/Sorting Techniques/count.cpp create mode 100644 CPP/Sorting Techniques/insertion.cpp create mode 100644 CPP/Sorting Techniques/merge.cpp create mode 100644 CPP/Sorting Techniques/quick.cpp create mode 100644 CPP/Sorting Techniques/selection.cpp create mode 100644 CPP/Tree/Queue.h create mode 100644 CPP/Tree/avlTree.cpp create mode 100644 CPP/Tree/bst(delete).cpp create mode 100644 CPP/Tree/bst.cpp create mode 100644 CPP/Tree/main.cpp create mode 100644 CPP/array(merge).cpp create mode 100644 CPP/array(set).cpp create mode 100644 CPP/array++.cpp create mode 100644 CPP/array.cpp create mode 100644 CPP/arrayfn.cpp create mode 100644 CPP/arrayinfun.cpp create mode 100644 CPP/arrayparameter.cpp create mode 100644 CPP/binarysearch(array).cpp create mode 100644 CPP/callbyaddress.cpp create mode 100644 CPP/callbyreference.cpp create mode 100644 CPP/callbyvalue.cpp create mode 100644 CPP/class.cpp create mode 100644 CPP/classwithfn.cpp create mode 100644 CPP/diagonalmatrix.cpp create mode 100644 CPP/diamondinheritance.cpp create mode 100644 CPP/functions.cpp create mode 100644 CPP/get,set,max,min.cpp create mode 100644 CPP/heap.cpp create mode 100644 CPP/inheritance.cpp create mode 100644 CPP/linearsearch(array).cpp create mode 100644 CPP/operation.cpp create mode 100644 CPP/pointer.cpp create mode 100644 CPP/queuearr.cpp create mode 100644 CPP/recursion(factorial).cpp create mode 100644 CPP/recursion(fibonacci).cpp create mode 100644 CPP/recursion(indirect).cpp create mode 100644 CPP/recursion(nCr).cpp create mode 100644 CPP/recursion(nested).cpp create mode 100644 CPP/recursion(power).cpp create mode 100644 CPP/recursion(sum of n).cpp create mode 100644 CPP/recursion(taylorseries).cpp create mode 100644 CPP/recursion.cpp create mode 100644 CPP/refrences.cpp create mode 100644 CPP/reversearray.cpp create mode 100644 CPP/stack.cpp create mode 100644 CPP/staticglobalrecursion.cpp create mode 100644 CPP/structcallbyadd.cpp create mode 100644 CPP/structure.cpp create mode 100644 CPP/structureandfn.cpp create mode 100644 CPP/template.cpp create mode 100644 CPP/treerecursion.cpp diff --git a/CONTRIBUTOR.md b/CONTRIBUTOR.md index 6afa9db..b6d408c 100644 --- a/CONTRIBUTOR.md +++ b/CONTRIBUTOR.md @@ -33,3 +33,7 @@ #### Name: [Pratik Shirodkar](https://github.com/Pratik-Shirodkar) - Place: Goa, India - GitHub: [Pratik Shirodkar](https://github.com/Pratik-Shirodkar) + +### Name: [Raza Khan](https://github.com/raza-khan0108) +- Place: Mumbai, India +- Github: [raza-khan0108](https://github.com/raza-khan0108) diff --git a/CPP/2Darray.cpp b/CPP/2Darray.cpp new file mode 100644 index 0000000..1e11d96 --- /dev/null +++ b/CPP/2Darray.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; + +int main(){ + int A[3][4] = {{1,2,3,4},{2,4,6,8},{1,3,5,7}}; + for(int i=0;i<3;i++){ //for rows + for(int j=0;j<4;j++){ // for columns + cout< +#include +struct Node +{ + int data; + struct Node *next; + +}*front=NULL,*rear=NULL; +void enqueue(int x) +{ + struct Node *t; + t=(struct Node*)malloc(sizeof(struct Node)); + if(t==NULL) + printf("Queue is FUll\n"); + else + { + t->data=x; + t->next=NULL; + if(front==NULL) + front=rear=t; + else + { + rear->next=t; + rear=t; + } + } + +} +int dequeue() +{ + int x=-1; + struct Node* t; + + if(front==NULL) + printf("Queue is Empty\n"); + else + { + x=front->data; + t=front; + front=front->next; + free(t); + } + return x; +} +int isEmpty() +{ + return front==NULL; +} +#endif /* Queue_h */ +#include +void BFS(int G[][7],int start,int n) +{ + int i=start,j; + + int visited[7]={0}; + + printf("%d ",i); + visited[i]=1; + enqueue(i); + + while(!isEmpty()) + { + i=dequeue(); + for(j=1;j +#define V 8 +#define I 32767 + +using namespace std; + +void PrintMST(int T[][V-2], int G[V][V]){ + cout << "\nMinimum Spanning Tree Edges (w/ cost)\n" << endl; + int sum {0}; + for (int i {0}; i +#include +using namespace std; + +void TOH(int n,int A,int B,int C){ + if(n>0){ + TOH(n-1,A,C,B); + cout << "("< +#include +#include +using namespace std; + +struct Node +{ + int data; + struct Node *next; +}*first=NULL,*second=NULL,*third=NULL; + +void Display(struct Node *p) +{ + while(p!=NULL) + { + cout<data<<" "; + p=p->next; + } +} + +void create(int A[],int n) +{ + int i; + struct Node *t,*last; + first=new Node; + first->data=A[0]; + first->next=NULL; + last=first; + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t; + last=t; + } + +} +int main() +{ + + int A[]={10,20,40,50,60}; + create(A,5); + + Display(first); + + return 0; +} \ No newline at end of file diff --git a/CPP/LinkedList(circular).cpp b/CPP/LinkedList(circular).cpp new file mode 100644 index 0000000..fa53786 --- /dev/null +++ b/CPP/LinkedList(circular).cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + +struct Node +{ + int data; + struct Node *next; +}*Head; + +void create(int A[],int n) +{ + int i; + struct Node *t,*last; + Head= new (Node); + Head->data=A[0]; + Head->next=Head; + last=Head; + + for(i=1;idata=A[i]; + t->next=last->next; + last->next=t; + last=t; + } +}; + +void Display(struct Node *h) +{ + do + { + cout<data<<" "; + h=h->next; + }while(h!=Head); + cout<< endl; +}; + +//Insert in Circular linked list +void Insert(struct Node *p,int index,int x){ + struct Node *t; + if(index==0){ + t=new Node; + t->data=x; + if(Head==NULL){ + Head=t; + Head->next=Head; + } + else{ + p=Head; + while(p->next!=Head){ + p=p->next;} + } + p->next=t; + t->next=Head; + } + else{ + p=Head; + for(int i=0;inext;} + t=new Node; + t->data=x; + t->next=p->next; + p->next=t; + } + }; + +//Delete in circular LInked List +void Delete(struct Node *p,int index){ +struct Node *q; +int x; +if(index==0){ + while(p->next!=Head){ + p=p->next; + x=Head->data; + } + if(p==Head){ + delete Head; + Head=NULL; + } + else{ + p->next=Head->next; + delete Head; + Head=p->next; + } +} +else{ + p=Head; + for(int i=0;inext;} + q=p->next; + p->next=q->next; + x=q->data; + delete q; +} +}; + +int main() +{ + int A[]={2,3,4,5,6}; + create(A,5); + Insert(Head,3,10); + Delete(Head,5); + Display(Head); + return 0; +} \ No newline at end of file diff --git a/CPP/LinkedList.cpp b/CPP/LinkedList.cpp new file mode 100644 index 0000000..3ab0b48 --- /dev/null +++ b/CPP/LinkedList.cpp @@ -0,0 +1,201 @@ +#include +#include +#include +using namespace std; + +struct Node +{ + int data; + struct Node *next; +}*first=NULL; + +void create(int A[],int n) +{ + int i; + struct Node *t,*last; + first=(struct Node *)malloc(sizeof(struct Node)); + first->data=A[0]; + first->next=NULL; + last=first; + + for(i=1;idata=A[i]; + t->next=NULL; + last->next=t; + last=t; + } + +} +void Display(struct Node *p) //Using Loop +{ + while(p!=NULL) + { + cout<data<<" "; + p=p->next; + } +} + +void RDisplay(struct Node *p) //Using Recursion +{ + if(p!=NULL) + { + cout<< p->data<<" " ; + RDisplay(p->next); + } +} + +//counting nodes using loop +int count(struct Node *p){ + int count=0; + while(p!=0){ + count++; + p=p->next; + } + return count; +} + +//counting nodes using recursion +int rcount(struct Node *p){ + if(p==0){ + return 0; + } + else{ + return rcount(p->next)+1; + } +} + +//sum of all elements using loop +int sum(struct Node *p){ + int sum=0; + while(p!=0){ + sum=sum+p->data; + p=p->next; + } + return sum; +} + +//sum of all elements using recursion +int rsum(struct Node *p){ + if(p==0){ + return 0; + } + else{ + return rsum(p->next) + p->data ; + } +}; + +//finding max element using loop +int max(struct Node *p){ + int max=INT_MIN; + while(p) { + if(p->data>max){ + max=p->data; + p=p->next;} + } + return max; +}; + +//Linear search in linked list +Node * search(struct Node *p,int key){ + while(p!=0){ + if(p->data==key){ + cout<<"Key is found "<< p->data << endl; + } + else{ + p=p->next; + } + } + return nullptr; +}; + +//Insert element in linked list +void insert(struct Node *p,int index,int x){ + struct Node *t; + if(index==0){ + t=new Node; + t->data=x; + t->next=first; + first=t; + } + else if(index>0){ + p=first; + for(int i=0;inext; + } + if(p){ + t=new Node; + t->data=x; + t->next=p->next; + p->next=t; + } + } +} + +//Inserting element in sorted Linked List +void SortedInsert(struct Node *p,int x){ + struct Node *t,*q=NULL; + t=new Node; + t->data=x; + t->next=NULL; + if(first==NULL){ + first=t; + } + else{ + while(p && p->datanext; + } + if(p==first){ + t->next=first; + first=t;} + else{ + t->next=q->next; + q->next=t;} + } + }; + +//to check if linked list is sorted +int sorted(struct Node *p){ + int x=-9999; + while(p){ + if(p->datadata; + p=p->next; + } + } + return 1; +} + +//Reverse a linked list +void Reverse(struct Node *p){ + struct Node *q=NULL,*r=NULL; + while(p){ + r=q; + q=p; + p=p->next; + q->next=r; + } + first=q; +} + + +int main(){ + struct Node *temp; + int A[]={3,5,7,10,25,8}; + create(A,6); + + cout< +#include +#include +using namespace std; + +class Solution { +public: + vector dp; + + int racecar(int target) { + dp = vector(target + 1, -1); // Initialize memoization array + return helper(target); + } + + int helper(int target) { + if (dp[target] != -1) return dp[target]; // Memoization check + + int n = 1, pos = 1; + while (pos < target) { + pos = (1 << n) - 1; // Compute 2^n - 1 + n++; + } + + // Case 1: If we reach the target exactly + if (pos == target) return dp[target] = n; + + // Case 2: Overshoot and reverse + int overshoot = helper(pos - target) + n + 1; + + // Case 3: Stop before and reverse + int best = INT_MAX; + for (int m = 0; m < n - 1; ++m) { + int before = (1 << (n - 1)) - 1 - ((1 << m) - 1); + best = min(best, (n - 1) + 1 + m + 1 + helper(target - before)); + } + + return dp[target] = min(overshoot, best); + } +}; + +int main() { + Solution sol; + + int target; + cout << "Enter the target: "; + cin >> target; + + cout << "Minimum number of steps required to reach the target: " << sol.racecar(target) << endl; + return 0; +} diff --git a/CPP/Sorting Techniques/HashSum.cpp b/CPP/Sorting Techniques/HashSum.cpp new file mode 100644 index 0000000..fa5824b --- /dev/null +++ b/CPP/Sorting Techniques/HashSum.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +int longestZeroSumSubarray(int arr[],int n){ + int maxlen = 0; + + for (int i=0;i> T; + + while (T--) { + int n; + cin >> n; + + int arr[n]; + for (int i = 0; i < n; i++) + cin >> arr[i]; + + cout << "Longest subarray with sum 0 is: "<< longestZeroSumSubarray(arr, n) << endl; + } + + return 0; +} diff --git a/CPP/Sorting Techniques/Hashmatrix.cpp b/CPP/Sorting Techniques/Hashmatrix.cpp new file mode 100644 index 0000000..7e91738 --- /dev/null +++ b/CPP/Sorting Techniques/Hashmatrix.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +bool isCarBackToOrigin(string s){ + int x = 0, y = 0; + for(char move : s){ + if(move == 'r') x++; + else if(move == 'l') x--; + else if(move == 'u') y++; + else if(move == 'd') y--; + } + return x == 0 && y == 0; +} + +int main(){ + string s; + cout<< "Enter moves :"<>s; + + if(isCarBackToOrigin(s)) cout<< "Car is back to origin"< +using namespace std; + +void sortArray (int arr[],int n){ + int left = 0; + int right = n-1; + + while(left>T; + + while(T--){ + int n; + cin>>n; + int arr[n]; + for(int i=0;i>arr[i]; + } + sortArray(arr,n); + printArray(arr,n); + } + return 0; +} diff --git a/CPP/Sorting Techniques/bubble.cpp b/CPP/Sorting Techniques/bubble.cpp new file mode 100644 index 0000000..983cf66 --- /dev/null +++ b/CPP/Sorting Techniques/bubble.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; + +void swap(int *x,int *y){ + int temp = *x; + *x = *y; + *y =temp; +}; + +void Bubble(int A[],int n){ + int j; + int flag; + for(int i=0;iA[j+1]){ + swap(&A[j],&A[j+1]); + flag=1; + } + } + if(flag==0){ + break; + } + } +}; + +int main(){ + int A[] = {5,2,8,1,9}; + int n = 5; + Bubble(A,5); + + for(int i=0;i<5;i++){ + cout<< A[i]<<" "; + } + + return 0; +} \ No newline at end of file diff --git a/CPP/Sorting Techniques/count.cpp b/CPP/Sorting Techniques/count.cpp new file mode 100644 index 0000000..3537df3 --- /dev/null +++ b/CPP/Sorting Techniques/count.cpp @@ -0,0 +1,45 @@ +#include +#include +using namespace std; + +int FindMax(int A[],int n){ + int max = INT_MIN; + for (int i = 0; i < n; i++){ + max=A[i]; + } + return max; +}; + +void CountSort(int A[],int n){ + int i,j,*C; + int max = FindMax(A, n); + C = new int[max+1]; + for (i = 0; i < max+1; i++){ + C[i] = 0; + } + for(i=0;i0){ + A[j++]=i; + C[i]--;} + else{ + i++; + } + } +}; + +int main(){ + int A[] = {5,2,8,1,9,7,11}; + int n = 7; + CountSort(A,n); + + for(int i=0;i +#include +using namespace std; + +void Insertion(int A[],int n){ + int i,j,x; + for(i=1;i-1 && A[j]>x){ + A[j+1]=A[j]; + j-- ; + } + A[j+1]=x; + } +}; + +int main(){ + int A[] = {5,2,8,1,9}; + int n = 5; + Insertion(A,n); + + for(int i=0;i<5;i++){ + cout<< A[i]<<" "; + } + return 0; +}; + + \ No newline at end of file diff --git a/CPP/Sorting Techniques/merge.cpp b/CPP/Sorting Techniques/merge.cpp new file mode 100644 index 0000000..d594c1e --- /dev/null +++ b/CPP/Sorting Techniques/merge.cpp @@ -0,0 +1,48 @@ +#include +#include +using namespace std; + +void Merge(int A[],int l,int mid,int h){ + int i=l; + int j=mid+1; + int k=l; + int B[h-1+1]; + while(i<=mid && j<=h){ + if(A[i] +#include +using namespace std; + +void swap(int *x,int *y){ + int temp = *x; + *x = *y; + *y =temp; +}; + +int partition(int A[],int l,int h){ + int pivot = A[l]; + int i =l; + int j = h; + do{ + do{i++;}while(A[i]<=pivot); + do{j--;}while(A[j]>pivot); + if(i<=j){ + swap(&A[i],&A[j]); + } + }while(i +#include +using namespace std; + +void swap(int *x,int *y){ + int temp = *x; + *x = *y; + *y =temp; +}; + +void SelectionSort(int A[],int n){ + int i; + int j; + int k; + for(i=0;i +#include + +using namespace std; + +struct Node{ + struct Node *lchild; + int data; + struct Node *rchild; +}; + +struct Queue { + int size; + int front; + int rear; + Node **Q; +}; + +void create(struct Queue *q,int size){ + q->size = size; + q->front = q->rear = -1; + q->Q = new Node* [size]; +}; + + +void enqueue (struct Queue *q, Node* x){ + if (q->rear == q->size - 1){ + cout<<"Queue is full"<rear++; + q->Q[q->rear]=x; + } +}; + +Node* dequeue(struct Queue *q){ + Node* x = NULL; + if (q->front == q->rear){ + cout<<"Queue is empty"<front++; + x = q->Q[q->front]; + } + return x; +}; + +int isEmpty(struct Queue *q){ + if (q->front == q->rear){ + return 1;} + else{ + return 0;} +}; \ No newline at end of file diff --git a/CPP/Tree/avlTree.cpp b/CPP/Tree/avlTree.cpp new file mode 100644 index 0000000..58aa655 --- /dev/null +++ b/CPP/Tree/avlTree.cpp @@ -0,0 +1,133 @@ +#include +#include +using namespace std; + +struct Node{ + int data; + int height; + struct Node* lchild; + struct Node* rchild; +} *root=NULL; + +int NodeHeight(struct Node *p){ + int Hl; + int Hr; + if(p!=NULL && p->lchild){ + Hl = NodeHeight(p->lchild); + } + else{ + Hl = 0; + } + + if(p!=NULL && p->rchild){ + Hr = NodeHeight(p->rchild); + } + else{ + Hr = 0; + } + + if(Hl>Hr){ + return Hl+1; + } + else{ + return Hr+1; + } +}; + +int BalanceFactor(struct Node *p){ + int Hl; + int Hr; + if(p!=NULL && p->lchild){ + Hl = NodeHeight(p->lchild); + } + else{ + Hl = 0; + } + + if(p!=NULL && p->rchild){ + Hr = NodeHeight(p->rchild); + } + else{ + Hr = 0; + } + + return Hl-Hr; +}; + +struct Node *LLRotation(struct Node *p){ + struct Node *pl=p->lchild; + struct Node *plr=pl->rchild; + + pl->rchild=p; + p->lchild=plr; + p->height=NodeHeight(p); + pl->height=NodeHeight(pl); + + if(root==p){ + root=pl; + } + return pl; +}; + +struct Node *LRRotation(struct Node *p){ + struct Node *pl=p->lchild; + struct Node *plr=pl->rchild; + + pl->rchild=plr->lchild; + p->lchild=plr->rchild; + plr->rchild=p; + plr->lchild=pl; + + p->height=NodeHeight(p); + pl->height=NodeHeight(pl); + plr->height=NodeHeight(plr); + + if(root==p){ + root=plr; + } + return plr; +}; + +struct Node *Insert(struct Node *p,int key){ + struct Node *t=NULL; + if(p==NULL){ + t=new Node; + t->data=key; + t->height=1; + t->lchild=NULL; + t->rchild=NULL; + } + else{ + if(keydata){ + p->lchild=Insert(p->lchild,key); + } + else if(key>p->data){ + p->rchild=Insert(p->rchild,key); + } + else{ + return p; + } + } + + if (p != NULL) { + p->height = NodeHeight(p); +} + + if(BalanceFactor(p)==2 && BalanceFactor(p->lchild)==1){ + return LLRotation(p); + } + else if(BalanceFactor(p)==2 && BalanceFactor(p->lchild)==-1){ + return LRRotation(p); + } + return p; +}; + +int main(){ + root=Insert(root,10); + Insert(root,5); + Insert(root,2); + + return 0; + +}; + diff --git a/CPP/Tree/bst(delete).cpp b/CPP/Tree/bst(delete).cpp new file mode 100644 index 0000000..0bb248f --- /dev/null +++ b/CPP/Tree/bst(delete).cpp @@ -0,0 +1,98 @@ +#include +#include +using namespace std; + +struct Node{ + int data; + struct Node* lchild; + struct Node* rchild; +} *root=NULL; + +struct Node *Insert(struct Node *p,int key){ + struct Node *t=NULL; + if(p==NULL){ + t=new Node; + t->data=key; + t->lchild=t->rchild=NULL; + return t; + } + + else{ + if(keydata){ + p->lchild=Insert(p->lchild,key); + } + else if(key>p->data){ + p->rchild=Insert(p->rchild,key); + } + else{ + return p; + } + return p; + } + +}; + +void inorder(struct Node *p){ + if(p!=NULL){ + inorder(p->lchild); + cout<data<<" "; + inorder(p->rchild); + } +}; + +int height(struct Node *p){ + int x=0; + int y=0; + if(p==NULL){ + return 0; + } + else{ + x=height(p->lchild); + y=height(p->rchild); + if(x>y){ + return x+1;} + else{ + return y+1; + } + } +}; + +int InorderPre(struct Node*p){ + while(p&&p->rchild!=NULL){ + p=p->rchild; + } + return p->data; +}; + +int InorderSucc(struct Node*p){ + while(p&&p->lchild!=NULL){ + p=p->lchild; + } + return p->data; +}; + + + +void preorder(struct Node *p){ + if(p!=NULL){ + cout<data<<" "; + preorder(p->lchild); + preorder(p->rchild); + } +}; + +int main(){ + root=Insert(root, 10); + Insert(root, 1); + Insert(root, 3); + Insert(root, 5); + Insert(root, 15); + Insert(root, 12); + Insert(root, 16); + + inorder(root); + cout< +#include +using namespace std; + +struct Node{ + int data; + struct Node* lchild; + struct Node* rchild; +} *root=NULL; + +void insert(int key){ +// pointer T on root, pointer R on node before P and pointer P where new node to create. + struct Node* t=root; + struct Node* r=NULL; + struct Node* p=NULL; + +// Creation of root node + if(root==NULL){ + p=new Node; + p->data=key; + p->lchild=NULL; + p->rchild=NULL; + root=p; + return; + } + +//Search if element is already present in the BST + while(t!=NULL){ + r=t; + if(key==t->data){ + return; + } + else if(keydata){ + t=t->lchild; + } + else{ + t=t->rchild;} + } + +//If not then T becomes null and P is where new node is to be created. + p=new Node; + p->data=key; + p->lchild=NULL; + p->rchild=NULL; + +//Link the new node with the help of R pointer in the BSt + if(p->datadata){ + r->lchild=p; + } + else{ + r->rchild=p; + } +}; + +void inorder(struct Node *p){ + if(p!=NULL){ + inorder(p->lchild); + cout<data<<" "; + inorder(p->rchild); + } +}; + +struct Node * Search(int key){ + struct Node* t=root; + while(t!=NULL){ + if(key==t->data){ + return t; + } + else if(keydata){ + t=t->lchild; + } + else{ + t=t->rchild;} + } + return NULL ; +}; + +struct Node * Rinsert(struct Node *p,int key){ + //Creation of new node using t + struct Node *t=NULL; + if(p==NULL){ //If root is null + t=new Node; + t->data=key; + t->lchild=NULL; + t->rchild=NULL; + return t; + } + else{ + if(keydata){ + p->lchild=Rinsert(p->lchild,key); + } + else if(key>p->data){ + p->rchild=Rinsert(p->rchild,key); + } + else{ + return p; + } + } +}; + +int main(){ + struct Node *temp; + insert(10); + insert(20); + insert(8); + insert(5); + insert(15); + + + inorder(root); + cout<< endl; + + temp=Search(15); + if(temp){ + cout<<"Element found in BST : "<< temp->data < +#include "Queue.h" +using namespace std; + +struct Node *root=NULL; +void Treecreate(){ + struct Node *t,*p; + int x; + struct Queue q; + create(&q,100); + cout<<"Enter root value"<>x; + root=new Node; + root->data=x; + root->lchild=root->rchild=NULL; + enqueue(&q,root); + + while(!isEmpty(&q)){ + p=dequeue(&q); + + cout<<"Enter left child of "<data<>x; + if(x!=-1){ + t=new Node; + t->data=x; + t->lchild=t->rchild=NULL; + p->lchild=t; + enqueue(&q,t); + + } + cout<<"Enter right child of "<data<>x; + if(x!=-1){ + t=new Node; + t->data=x; + t->lchild=t->rchild=NULL; + p->rchild=t; + enqueue(&q,t); + } + } +}; + +void preorder(struct Node *p){ + if(p!=NULL){ + cout<data<<" "; + preorder(p->lchild); + preorder(p->rchild); + } +}; + +void inorder(struct Node *p){ + if(p!=NULL){ + inorder(p->lchild); + cout<data<<" "; + inorder(p->rchild); + } +}; + +void postorder(struct Node *p){ + if(p!=NULL){ + postorder(p->lchild); + postorder(p->rchild); + cout<data<<" "; + } +}; + +int count(struct Node *p){ + if(p!=NULL){ + return 1+count(p->lchild)+count(p->rchild); + } + else{ + return 0; + } +}; + +int height(struct Node *root){ + int x=0,y=0; + if(root==NULL){ + return 0; + } + else{ + x=height(root->lchild); + y=height(root->lchild); + if(x>y){ + return x+1; + } + else{ + return y+1; + } + + } +}; + +int main(){ + Treecreate(); + cout<<"Count: "< +#include +using namespace std; + +struct Array { + int A[10]; + int size = 10; + int length = 5; +}; + +struct Array* Merge(struct Array *arr1,struct Array *arr2){ + int i,j,k; + i=j=k=0 ; + struct Array *arr3 = new Array ; + while(ilength && jlength){ + if(arr1->A[i]A[j]){ + arr3->A[k] = arr1->A[i]; + i++; + k++; } + else{ + arr3->A[k] = arr2->A[j]; + j++; + k++; } + } + while(ilength){ + arr3->A[k++] = arr1->A[i++];} + while(jlength){ + arr3->A[k++] = arr2->A[j++];} + + arr3->length = arr1->length+arr2->length; + return arr3; +} +void Display(struct Array arr) + { + int i; + cout<<"Elements are:" ; + for(i=0;i +#include +using namespace std; + +struct Array { + int A[10]; + int size = 10; + int length = 5; +}; + +struct Array* Intersection(struct Array *arr1,struct Array *arr2){ + int i,j,k; + i=j=k=0 ; + struct Array *arr3 = new Array ; + while(ilength && jlength){ + if(arr1->A[i]A[j]){ + i++; + k++; } + else if(arr2->A[j]A[i]){ + j++; + k++; } + else if(arr1->A[i]==arr2->A[j]){ + arr3->A[k++] = arr1->A[i++]; + j++; + } + } + arr3->length = k ; + arr3->size = 10; + return arr3; +} + +struct Array* Union(struct Array *arr1,struct Array *arr2){ + int i,j,k; + i=j=k=0 ; + struct Array *arr3 = new Array ; + while(ilength && jlength){ + if(arr1->A[i]A[j]){ + arr3->A[k++] = arr1->A[i++]; + } + else if(arr2->A[j]A[i]){ + arr3->A[k++] = arr2->A[j++]; + } + else { + arr3->A[k++] = arr1->A[i++]; + j++; + } + } + arr3->length = k ; + return arr3; +} + + + +void Display(struct Array arr) + { + int i; + cout<<"Elements are:" ; + for(i=0;i +using namespace std; + +class Array +{ +private: + int *A; + int size; + int length; + void swap(int *x,int *y); + +public: + Array() + { + size=10; + length=0; + A=new int[size]; + } + Array(int sz) + { + size=sz; + length=0; + A=new int[size]; + } + ~Array() + { + delete []A; + } + void Display(); + void Append(int x); + void Insert(int index,int x); + int Delete(int index); + int LinearSearch(int key); + int BinarySearch(int key); + int Get(int index); + void Set(int index,int x); + int Max(); + int Min(); + int Sum(); + float Avg(); + void Reverse(); + void Reverse2(); + void InsertSort(int x); + int isSorted(); + void Rearrange(); + Array* Merge(Array arr2); + Array* Union(Array arr2); + Array* Diff(Array arr2); + Array* Inter(Array arr2); +}; +void Array::Display() +{ + int i; + cout<<"\nElements are\n"; + for(i=0;i=0 && index <=length) + { + for(i=length;i>index;i--) + A[i]=A[i-1]; + A[index]=x; + length++; + + } +} +int Array::Delete(int index) +{ + int x=0; + int i; + + if(index>=0 && index=0 && index=0 && index< length) + A[index]=x; +} +int Array::Max() +{ + int max=A[0]; + int i; + for(i=1;imax) + max=A[i]; + } + return max; +} +int Array::Min() +{ + int min=A[0]; + int i; + for(i=1;i=0;i--,j++) + B[j]=A[i]; + for(i=0;i=0 && A[i]>x) + { + A[i+1]= A[i]; + i--; + } + A[i+1]=x; + length++; + +} +int Array::isSorted() +{ + int i; + for(i=0;iA[i+1]) + return 0; + } + return 1; +} +void Array::Rearrange() +{ + int i,j; + i=0; + j= length-1; + + while(i=0)j--; + if(iA[k++]=A[i++]; + else + arr3->A[k++]=arr2.A[j++]; + } + for(;iA[k++]=A[i]; + for(;jA[k++]=arr2.A[j]; + arr3->length=length+arr2.length; + + return arr3; +} +Array* Array::Union(Array arr2) +{ + int i,j,k; + i=j=k=0; + + Array *arr3=new Array(length+arr2.length); + + while(iA[k++]=A[i++]; + else if(arr2.A[j]A[k++]=arr2.A[j++]; + else + { + arr3->A[k++]=A[i++]; + j++; + } + } + for(;iA[k++]=A[i]; + for(;jA[k++]=arr2.A[j]; + + arr3->length=k; + + return arr3; +} +Array* Array::Inter(Array arr2) +{ + int i,j,k; + i=j=k=0; + + Array *arr3=new Array(length+arr2.length); + + while(iA[k++]=A[i++]; + j++; + } + } + + arr3->length=k; + + return arr3; +} +Array* Array::Diff(Array arr2) +{ + int i,j,k; + i=j=k=0; + + Array *arr3=new Array(length+arr2.length); + + while(iA[k++]=A[i++]; + else if(arr2.A[j]A[k++]=A[i]; + + + arr3->length=k; + + return arr3; +} +int main() +{ + Array *arr1; + int ch,sz; + int x,index; + + cout<<"Enter Size of Array"; + scanf("%d",&sz); + arr1=new Array(sz); + + do + { + cout<<"\n\nMenu\n"; + cout<<"1. Insert\n"; + cout<<"2. Delete\n"; + cout<<"3. Search\n"; + cout<<"4. Sum\n"; + cout<<"5. Display\n"; + cout<<"6.Exit\n"; + + cout<<"enter you choice "; + cin>>ch; + + switch(ch) + { + case 1: cout<<"Enter an element and +index "; + cin>>x>>index; + arr1->Insert(index,x); + break; + case 2: cout<<"Enter index "; + cin>>index; + x=arr1->Delete(index); + cout<<"Deleted Element is"<>x; + index=arr1->LinearSearch(x); + cout<<"Element index "<Sum(); + break; + case 5:arr1->Display(); + + } + }while(ch<6); + return 0; +} \ No newline at end of file diff --git a/CPP/array.cpp b/CPP/array.cpp new file mode 100644 index 0000000..d906aaf --- /dev/null +++ b/CPP/array.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +int main(){ + int A[5]={2,4,6,8,10}; + for (int i=0;i<5;i++){ + cout< +#include +using namespace std; + +struct Array { + int A[10]; + int size = 10; + int length = 9; +}; + +void swap(struct Array *arr, int x,int y){ + int temp = arr->A[x]; + arr->A[x] = arr->A[y]; + arr->A[y] = temp; +} + +//to insert element in sorted array +void InsertSort(struct Array *arr, int x){ + int i = arr->length - 1; + if(arr->length == arr->size){ + return ; } + + while(i>=0 && arr->A[i]>x){ + arr->A[i+1] = arr->A[i]; + i--; + arr->A[i+1]=x; + } + + +}; + +//to check if array is sorted or not +int isSorted(struct Array arr){ + int i; + for(i=0;iarr.A[i+1]){ + return 0;} + return 1; + +} + +//to arrange +ve and -ve numbers +void Rearrange(struct Array *arr){ + int i = 0, j = arr->length - 1; + while(iA[i]<0) i++; + while(arr->A[j]>0) j--; + if(iA[i], arr->A[j]); + } + } +} + +void Display(struct Array arr) + { + int i; + cout<<"Elements are:" ; + for(i=0;i +#include +using namespace std; + +int * fun(int size){ + int *p ; + p = new int[size]; + + for(int i = 0;i +#include +using namespace std; + +void fun(int A[], int n){ + for(int i=0;i +#include +using namespace std; + +struct Array { + int A[10]; + int size = 10; + int length = 10; +}; + +int search(struct Array arr, int key){ + int low = 0; + int high = arr.length-1; + int mid; + + while(low<=high){ + mid = (low+high)/2 ; + if(key == arr.A[mid]){ + return mid; + } + else if(key < arr.A[mid]){ + high = mid - 1; + } + else { + low = mid + 1; + } + + } +return -1; +} + + +void Display(struct Array arr) + { + int i; + cout<<"Elements are:" ; + for(i=0;i +#include +using namespace std; + +void swap (int *x,int *y){ + int temp; + temp = *x; + *x = *y; + *y = temp; +} + +int main(){ + int a = 10; + int b = 20; + swap(&a, &b); + cout< +#include +using namespace std; + +void swap (int &x,int &y){ + int temp; + temp = x; + x = y; + y = temp; +} + +int main(){ + int a = 10; + int b = 20; + swap(a, b); + cout< +#include +using namespace std; + +void swap (int x,int y){ + int temp; + temp = x; + x = y; + y = temp; +} + +int main(){ + int a = 10; + int b = 20; + swap(a,b); + cout< +#include +using namespace std; + +class rectangle { +private: + int length; + int breadth; +public: + rectangle(){ //Default constructor + length = 0; + breadth = 0; + } + rectangle(int l,int b){ //Parameterized constructor + length = l; + breadth = b; + } + int area(){ //Facilitators + return length * breadth; + } + int perimeter(){ //Facilitators + return 2*(length+breadth); + } + int getLength(){ //Accessors + return length; + } + int getBreadth(){ //Accessors + return breadth; + } + void setLength(int l){ //Mutuators + length = l; + } + void setBreadth(int b){ //Mutuators + breadth = b; + } + ~rectangle(){ //Destructor + cout<<"destructor"< +#include +using namespace std; + +class rectangle { + public : + int length; + int breadth; + + void initialize(int l , int b){ + length = l; + breadth =b; + + } + +int area(){ + return length*breadth; +}; + +int perimeter(){ + int p = 2*(length+breadth); + return p; +}; +}; + +int main(){ + class rectangle r = {0,0}; + int l; + int b; + cout<<"enter length and breadth"<>l>>b; + + r.initialize(l,b); + int a = r.area(); + int p = r.perimeter(); + + cout<<"Area is "< +#include +using namespace std; + +class Diagonal{ + private: + int *A; + int n; + public: + Diagonal(int n){ + this->n=n; + A=new int[n]; + } + + ~Diagonal(){ + delete []A; + } + + void set(int i,int j,int x); + int get(int i,int j); + void display(); +}; + +void Diagonal:: set(int i ,int j,int x){ + if(i==j){ + A[i-1]= x; + } +}; + +int Diagonal::get(int i,int j){ + if(i==j){ + return A[i-1]; } + else{ + return 0; + } +}; + +void Diagonal::display(){ + int i,j; + for(i=0;i<=n;i++){ + for(j=0;j<=n;j++){ + if(i==j){ + cout<< A[i]<<" "; + } + else{ + cout<<"0 "; + } + } + cout< + +class Base { +public: + void print() { + std::cout << "Base class" << std::endl; + } +}; + +class Derived1 : virtual public Base { +public: + void derived1Print() { + std::cout << "Derived1 class" << std::endl; + } +}; + +class Derived2 : virtual public Base { +public: + void derived2Print() { + std::cout << "Derived2 class" << std::endl; + } +}; + +class Derived3 : public Derived1, public Derived2 { +public: + void derived3Print() { + std::cout << "Derived3 class" << std::endl; + } +}; + +int main() { + Derived3 d3; + d3.print(); // Now, there is no ambiguity in calling the base class function + d3.derived1Print(); + d3.derived2Print(); + d3.derived3Print(); + + return 0; +} \ No newline at end of file diff --git a/CPP/functions.cpp b/CPP/functions.cpp new file mode 100644 index 0000000..9cdc26e --- /dev/null +++ b/CPP/functions.cpp @@ -0,0 +1,17 @@ +#include +#include +using namespace std; + +int add(int a , int b){ + int c = a + b ; + return c; +} + +int main(){ + int x = 10; + int y = 15; + + int sum = add(x,y); + cout<<"Sum is "< +#include +using namespace std; + +struct Array { + int A[10]; + int size = 10; + int length = 10; +}; + +int get(struct Array arr,int index){ + if(index>=0 && index< arr.length){ + return arr.A[index]; + } + return -1; +}; + +void set(struct Array *arr, int index , int x){ + if(index>=0 && index< arr->length){ + arr->A[index] = x; + } +}; + +int max(struct Array arr){ + int max = arr.A[0]; + for(int i=1; imax){ + max = arr.A[i]; + return max; + } + } +}; + +int min(struct Array arr){ + int min = arr.A[0]; + for(int i=1; i +#include +using namespace std; + +void Insert(int A[],int n){ + int i=n; + int temp; + temp=A[i]; + while(i>1 && temp>A[i/2]){ + A[i]=A[i/2]; + i=i/2; + } + A[i]=temp; +}; + +int Delete(int A[],int n){ + int i,j,x,temp,val; + val=A[1]; + x=A[n]; + A[1]=A[n]; + A[n]=val; + i=1; + j=i*2; + while(j<=n-1){ + if(A[j+1]>A[j]){ + j=j+1;} + if(A[j]>A[i]){ + temp=A[i]; + A[i]=A[j]; + A[j]=temp; + i=j; + j=j*2; + } + else{ + break; + + } } + return val; +}; + +int main(){ + int A[]={0,10,20,30,25,5,40,35}; + Insert(A,1); + Insert(A,2); + Insert(A,3); + Insert(A,4); + Insert(A,5); + Insert(A,6); + Insert(A,7); + + cout<<"Deleted value is "< + +// Base class 1 +class Animal +{ +public: + void eat() + { + std::cout << "I can eat!" << std::endl; + } +}; + +// Base class 2 +class Mammal +{ +public: + void breath() + { + std::cout << "I can breathe!" << std::endl; + } +}; + +// Derived class inheriting from both Animal and Mammal +class Dog : public Animal, public Mammal +{ +public: + void bark() + { + std::cout << "I can bark! Woof woof!" << std::endl; + } +}; + +int main() +{ + Dog myDog; + + // Calling members from both base classes + myDog.eat(); + myDog.breath(); + + // Calling a member from the derived class + myDog.bark(); + + return 0; +} \ No newline at end of file diff --git a/CPP/linearsearch(array).cpp b/CPP/linearsearch(array).cpp new file mode 100644 index 0000000..83d603e --- /dev/null +++ b/CPP/linearsearch(array).cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +struct Array { + int A[10]; + int size = 10; + int length = 10; +}; + +//improving linear search +void swap( int *x,int *y){ + int temp = *x ; + *x = *y ; + *y = temp ; +} + +int search(struct Array *arr, int key){ //call by value because we only search not modify + for(int i=0; i < arr->length; i++){ //call by add can also be used to improve LS + if (key== arr->A[i]){ + swap(&arr->A[i] , &arr->A[0]) ; // Move to front and transposition &arr->A[i-1] + return i ; + } + } + return -1; +}; + +void Display(struct Array arr) + { + int i; + cout<<"Elements are:" ; + for(i=0;i +#include +using namespace std; +#include + +struct Array +{ + int A[10]; + int size; + int length; +}; + + void Display(struct Array arr) + { + int i; + cout<<"Elements are:" ; + for(i=0;ilengthsize){ //length(5) = size(10) ya greater hogi toh value add nhi krskte in last + arr->A[arr->length++]=x; + } + }; + + void Insert(struct Array *arr,int index,int x) + { + int i; + if(index>=0 && index <=arr->length) // + { + for(i=arr->length;i>index;i--) + arr->A[i]=arr->A[i-1]; + arr->A[index]=x; + arr->length++; + } + } + +int Delete(struct Array *arr,int index){ + int x=0; + if(index>=0 && indexlength){ + x=arr->A[index]; + for(int i=index;ilength;i++){ + arr->A[i]= arr->A[i+1]; + arr->length-- ; + } + return x; + } +} + +int main() +{ +struct Array arr1={{2,3,4,5,6},10,5}; + +Append(&arr1,10); +Insert(&arr1,0,12); +Delete(&arr1,4); +Display(arr1); + return 0; +} \ No newline at end of file diff --git a/CPP/pointer.cpp b/CPP/pointer.cpp new file mode 100644 index 0000000..c6f6f92 --- /dev/null +++ b/CPP/pointer.cpp @@ -0,0 +1,11 @@ +#include +#include +using namespace std; + +int main() { + int a = 10; + int *p ; + p = &a; + + cout< +#include +using namespace std; + +struct Queue { + int size; + int front; + int rear; + int *Q; +}; + +void enqueue (struct Queue *q, int x){ + if (q->rear == q->size - 1){ + cout<<"Queue is full"<rear++; + q->Q[q->rear]=x; + } +}; + +int dequeue(struct Queue *q){ + int x = -1; + if (q->front == q->rear){ + cout<<"Queue is empty"<front++; + x = q->Q[q->front]; + } + return x; +}; + +void display(struct Queue q){ + for(int i=q.front+1;i<=q.rear;i++){ + cout<size = size; + q->front = q->rear = -1; + q->Q = new int[size]; +}; + +int main(){ + struct Queue q; + create(&q,5); + enqueue(&q,10); + enqueue(&q,20); + enqueue(&q,30); + display(q); + cout< +#include +using namespace std; + +int fact(int n){ + if (n==0){ + return 1; + } + else { + return fact(n-1)*n; + } +} + +int Ifact(int n){ + int i; + int s = 1; + for (i=1;i<=n;i++) + s = s * i; + return s; +} + +int main(){ + int r; + r = Ifact(5); + cout << r ; + return 0; +} \ No newline at end of file diff --git a/CPP/recursion(fibonacci).cpp b/CPP/recursion(fibonacci).cpp new file mode 100644 index 0000000..30e859c --- /dev/null +++ b/CPP/recursion(fibonacci).cpp @@ -0,0 +1,17 @@ +#include +#include +using namespace std; + +int fib(int n){ + if (n<=1){ + return n; + } + else { + return fib(n-2)+fib(n-1); + } +} + +int main(){ + cout< +#include +using namespace std; + +void funB(int n); +void funA(int n){ + if (n>0){ + cout<< n<1){ + cout<< n< +#include +using namespace std; + +int C(int n,int r){ + if(n==r || r == 0){ + return 1; + } + else { + return C(n-1,r-1) + C(n-1,r); + } +} + +int main(){ + cout< +#include +using namespace std; + +int fun(int n){ + if (n>100){ + return n-10; + } + else{ + return (fun(n+11)); + } + +} + +int main(){ + int r; + r = fun(94); + cout<< r ; + return 0; +} \ No newline at end of file diff --git a/CPP/recursion(power).cpp b/CPP/recursion(power).cpp new file mode 100644 index 0000000..22d427f --- /dev/null +++ b/CPP/recursion(power).cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; + +int power(int m,int n){ + if(n==0){ + return 1; + } + else { + return power(m,n-1)*m; + } +} + +int power1(int m,int n){ + int a ; + int i; + for (i=1;i<=n;i++) + a = m * m; + return s; +} + +int main(){ + int r = power1(2,9); + cout << r ; + return 0; +} \ No newline at end of file diff --git a/CPP/recursion(sum of n).cpp b/CPP/recursion(sum of n).cpp new file mode 100644 index 0000000..b48de3b --- /dev/null +++ b/CPP/recursion(sum of n).cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +int sum(int n){ //using recursion + if (n==0){ + return 0; + } + else{ + return sum(n-1)+n; + } +} + +int Isum(int n){ //using for loop + int s = 0; + int i; + for (i=1;i<=n;i++) + s = s + i; + return s; +} + +int main(){ + int r; + r=Isum(5); + cout< +#include +using namespace std; + +double e(int x, int n) +{ + static double p=1,f=1; + double r; + + if(n==0) + return 1; + r=e(x,n-1); + p=p*x; + f=f*n; + return r+p/f; +} +int main() +{ + cout << e(1,10); + return 0; +} \ No newline at end of file diff --git a/CPP/recursion.cpp b/CPP/recursion.cpp new file mode 100644 index 0000000..f542414 --- /dev/null +++ b/CPP/recursion.cpp @@ -0,0 +1,16 @@ +#include +#include +using namespace std; + +void fun(int n){ + + if(n>0){ + cout< +#include +using namespace std; + +int main(){ + int a = 10; + int &r = a; + + cout< +#include +using namespace std; + +struct Array { + int A[10]; + int size = 10; + int length = 10; +}; + +void reverse(struct Array *arr){ + int i, j; + int temp; + for(i=0,j=arr->length-1;iA[i]; + arr->A[i] = arr->A[j]; + arr->A[j] = temp; + } +} + +void Display(struct Array arr) + { + int i; + cout<<"Elements are:" ; + for(i=0;i +#include +using namespace std; + + struct Stack{ + int size; + int top; + int *s; +}; + +void display(struct Stack st){ + for(int i=st.top;i>=0;i--){ + cout<top==st->size-1){ + cout<<"Stack is full"<top++; + st->s[st->top]=x; + } +} + +int pop(struct Stack *st){ + int x=-1; + if(st->top==-1){ + cout<<"Stack is empty"<s[st->top]; + st->top--; } + + return x; +}; + +int peek(struct Stack st, int pos){ + int x=-1; + if(st.top-pos+1<0){ + cout<<"Invalid position"<> st.size; + cout << endl; + st.s=new int [st.size]; + st.top=-1; + + push(&st,5); + push(&st,10); + push(&st,15); + push(&st,40); + cout< +#include +using namespace std; + +int fun(int n){ + static int x; + if(n>0){ + x++; + return fun(n-1)+x; + } + return 0; +} + +int main(){ + int a = fun (5); + cout< +#include +using namespace std; + +struct rectangle{ + int length; + int breadth; +}; + +void changelength (struct rectangle *p, int l ){ + p->length = l; +} + +int area (struct rectangle r1){ + int a = r1.length * r1.breadth; + return a; +} + +int main(){ ;pppppppp;pp;;p;;;; + struct rectangle r = {10,5}; + changelength(&r,20); + int p = area(r); + cout< +#include +using namespace std; + +struct rectangle{ + int length; + int breadth; +}; + +int area (struct rectangle &r1){ + r1.length = 20; + int a = r1.length * r1.breadth; + return a; +} + +int main(){ + struct rectangle r = {10,5}; + int p = area(r); + cout< +#include +using namespace std; + +struct rectangle { + int length; + int breadth; +}; + +int area(struct rectangle r){ + return r.length*r.breadth; +}; + +int perimeter(struct rectangle r){ + int p = 2*(r.length+r.breadth); + return p; +}; + +int main(){ + struct rectangle r = {0,0}; + cout<<"enter length and breadth"<>r.length>>r.breadth; + + int a = area(r); + int p = perimeter(r); + + cout<<"Area is "< +#include +using namespace std; + +template +class arithmetic{ + private: + T a; + T b; + + public: + arithmetic(T a,T b); + T add(); + T subtract(); +}; + +template +arithmetic::arithmetic(T a,T b){ + this->a=a; + this->b =b; +} + +template +T arithmetic::add(){ + T c = a + b; + return c; +} + +template +T arithmetic::subtract(){ + T c = a - b; + return c; +} + +int main(){ + arithmetic ar(5.6 ,4.9 ); + cout<<"Addition is "< +#include +using namespace std; + +void fun(int n){ + if(n>0){ + cout<