Support Me...........

Please support me by giving donation

contact: aswinavofficial@gmail.com


Total Pageviews

Monday, September 1, 2014

C++ program to solve Hanoi Tower

/* Tower Of Hanoi */
 #include using namespace std;
 void hanoi(int n);
 void moveTower(int ht, char f, char t, char i); 
 void moveRing(int d, char f, char t); 
 int main() { cout << "How many disks? ";
 int x;
 cin >> x;
 hanoi (x);
 return 0; }
 void hanoi(int n) 
{
 moveTower(n, 'A', 'B', 'C');
 } void moveTower(int ht, char f, char t, char i)
 { if (ht > 0) { moveTower(ht-1, f, i, t);
 moveRing(ht, f, t);
 moveTower(ht-1, i, t, f);
 } } void moveRing (int d, char f, char t)
 { cout << "Move ring " << d << " from ";
 cout << f << " to " << t << endl; } // aiuto

C++ program to perform Stack

// Program to implement a Stack #include #include #include using namespace std; class element { public: int value; element* next; }; //class element class stack { public: int size; element* current; stack() { size=0; current=NULL; } //default constructor bool push(int,element*); bool pop(); bool isEmpty(); int getStackSize(); void printStackSize(); void printStackElements(element*); void printStackMenu(); }; bool stack::push(int ele, element* temp) { temp = new element; if(current == NULL) { temp->next = NULL; } else { temp->next = current; } temp->value=ele; current=temp; printf("%d inserted\n\n", ele); size++; return false; } bool stack::pop() { if(isEmpty()) { cout<<"\nStack is Empty\n"; return false; } else { cout << "\n Element To POP :" << current->value; cout << "\n Before POP"; printStackElements(current); current = current->next; cout << "\n After POP"; printStackElements(current); size = size--; } return true; } bool stack::isEmpty() { if(getStackSize()==0) return true; return false; } int stack::getStackSize() { return size; } //returns size of the stack void stack::printStackSize() { cout << "\nThe Size of the Stack:" << size << "\n"; } //print the stack size void stack::printStackElements(element* base) { element* curr2; curr2 = base; cout << "\n-----\n"; cout << "STACK\n"; cout << "-----\n"; while(curr2 != NULL) { cout << " |" << curr2->value<<"|\n"; curr2 = curr2->next; } } // print the stack void stack::printStackMenu() { cout << "Welcome to Stack \n"; cout << "1.Push an element\n"; cout << "2.Pop an element\n"; cout << "3.Display Stack\n"; cout << "4.Size Of Stack\n"; cout << "5.Exit\n"; } int main() { stack st; char Option=0; int val; while(1) { st.printStackMenu(); cin >> Option; switch(Option) { case '1': cout << "Enter a Number \n"; cin >> val; st.push(val, st.current); break; case '2': st.pop(); break; case '3': st.printStackElements (st.current); break; case '4': st.printStackSize(); break; case '5': exit(0); break; } } return 0; } // aiuto

C++ program to perform Radix Sort

#include using namespace std; int main() { cout << "How many values to sort? "; int n; cin >> n; int a[n]; for(int i=0;i> a[i]; } int e=1, d=3; int i, j, k, m, digit, row, col; int length = sizeof(a)/sizeof(int); int bmat[length][10]; int c[10]; for(m=1;m<=d;m++) { for(i=0;i<10;i++) { c[i]=-1; } for(i=0;i

C++ program to perform QuickSort

// QuickSort #include #include using namespace std; void print(int a[]) { for (int i = 0; i < 10; i++) { cout << a[i] << " "; } cout << endl << endl; } int partition(int a[], int p, int r) { int x = a[r]; int j = p - 1; for (int i = p; i < r; i++) { if (x <= a[i]) { j = j + 1; int temp = a[j]; a[j] = a[i]; a[i] = temp; } } a[r] = a[j + 1]; a[j + 1] = x; return (j + 1); } void quickSort(int a[], int p, int r) { if (p < r) { int q = partition(a, p, r); quickSort(a, p, q - 1); quickSort(a, q + 1, r); } } int main() { int a[] = { 1, 9, 0, 5, 6, 7, 8, 2, 4, 3}; cout << "Original array: " << endl; print(a); quickSort(a, 0, 9); cout << "Sorted array: " << endl; print(a); return 0; } // aiuto

C++ program to perform Merge Sort

// Merge Sort #include using namespace std; //Function Declarations void mergeSort(int numbers[], int temp[], int array_size); void m_sort(int numbers[], int temp[], int left, int right); void merge(int numbers[], int temp[], int left, int mid, int right); int main() { int arrayOne[10] = {15, 17, 56, 24, 37, 21, 47}; int arrayTwo[5]; cout << "Original Array: " << endl; for (int i = 0; i < 7; i++) { cout << arrayOne[i] << " "; } //end for cout << endl << endl; mergeSort(arrayOne, arrayTwo, 7); cout << "Sorted Array: " << endl; for (int i = 0; i < 7; i++) { cout << arrayOne[i] << " "; } //end for return 0; } //end main // "Main" function of the sequence // From here on out everything is called recursively void mergeSort(int numbers[], int temp[], int array_size) { m_sort(numbers, temp, 0, array_size - 1); } void m_sort(int numbers[], int temp[], int left, int right) { int mid; if (right > left) { mid = (right + left) / 2; m_sort(numbers, temp, left, mid); m_sort(numbers, temp, (mid+1), right); merge(numbers, temp, left, (mid+1), right); } } void merge(int numbers[], int temp[], int left, int mid, int right) { int i, left_end, num_elements, tmp_pos; left_end = (mid - 1); tmp_pos = left; num_elements = (right - left + 1); while ((left <= left_end) && (mid <= right)) { if (numbers[left] <= numbers[mid]) { temp[tmp_pos] = numbers[left]; tmp_pos += 1; left += 1; } else { temp[tmp_pos] = numbers[mid]; tmp_pos += 1; mid += 1; } } while (left <= left_end) { temp[tmp_pos] = numbers[left]; left += 1; tmp_pos += 1; } while (mid <= right) { temp[tmp_pos] = numbers[mid]; mid += 1; tmp_pos += 1; } //modified for (i=0; i < num_elements; i++) { numbers[right] = temp[right]; right -= 1; } } // aiuto

C++ program to perform Heap sort

// Heap sort #include #include using namespace std; int heapSize = 10; void print(int a[]) { for (int i = 0; i <= 9; i++) { cout << a[i] << " "; } cout << endl << endl; } int parent(int i) { if(i==1) return 0; if(i%2==0) return ( (i / 2)-1); else return ( (i / 2)); } int left(int i) { return (2 * i) + 1; } int right(int i) { return (2 * i) + 2; } void heapify(int a[], int i) { int l = left(i), great; int r = right(i); if ( (a[l] > a[i]) && (l < heapSize)) { great = l; } else { great = i; } if ( (a[r] > a[great]) && (r < heapSize)) { great = r; } if (great != i) { int temp = a[i]; a[i] = a[great]; a[great] = temp; heapify(a, great); } } void BuildMaxHeap(int a[]) { for (int i = 0 / 2; i <= (heapSize - 1); i++) { heapify(a, i); cout << "Pass " << i+1 << endl; print(a); } } void HeapSort(int a[]) { BuildMaxHeap(a); for (int i = heapSize; i > 0; i--) { int temp = a[0]; a[0] = a[heapSize - 1]; a[heapSize - 1] = temp; heapSize = heapSize - 1; heapify(a, 0); } } int main() { int arr[] = { 2, 9, 3, 6, 1, 4, 5, 7, 0, 8}; HeapSort(arr); cout << "Sorted array: " << endl; print(arr); return 0; } // aiuto

C++ program to perform Bubble Sort

//Bubble sort #include using namespace std; int compare(int, int); void sort(int[], const int); void swap(int *, int *); int compare(int x, int y) { return(x > y); } void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } void display(int array[], int n) { for (int i=0; i> quantity; tab = new int [quantity]; cout << "Input numbers: \n\n"; for (int i = 0; i < quantity; i++) { int x = i; cout << "#" << ++x << ": "; cin >> tab[i]; } cout << "\nBefore sorting: "; display(tab, quantity); cout << endl; sort(tab, quantity); cout << "\nAfter sorting: "; display(tab, quantity); cout << endl; return 0; } // aiuto

C++ Program to find the second largest value in a sequence of n numbers

/* Program to find the second largest value in a sequence of n numbers */ #include using namespace std; int main() { int x[10]; int largest, secondLargest; cout << "Enter 10 random integers..." << endl; for (int i=1; i<=10; i++) { cout << "Enter value " << i << ": "; cin >> x[i-1]; } largest = secondLargest = x[0]; for (int i=0; i<10; i++) if (x[i]>largest) { secondLargest = largest; largest = x[i]; } cout << "This is the array... " << endl << " [ "; for (int i=0; i<10; i++) { cout << x[i] << " "; } cout << "] " << endl; cout << "Second largest value is "; cout << secondLargest << endl; return 0; } // aiuto

C++ program to find roots of a quadratic equation

// Program to find the roots of a quadratic equation #include #include using namespace std; int main() { int a, b, c, d; double x1, x2; cout << "Program to find roots of quadratic eq" << endl; cout << "Enter values for ax^2 + bx + c..." << endl; cout << "Enter value for a: "; cin >> a; cout << "Enter value for b: "; cin >> b; cout << "Enter value for c: "; cin >> c; cout << endl; if (a==0) cout << "Not a quadratic equation." << endl; else { d = (b*b)-(4*a*c); if (d > 0) { cout << "Real and distinct roots" << endl; x1=(-b + sqrt(d))/(2*a); x2=(-b - sqrt(d))/(2*a); cout << "Root 1 = " << x1 << endl; cout << "Root 2 = " << x2 << endl; } else if (d == 0) { cout << "Real and equal roots." << endl; x1 = x2 = -b/(2*a); cout << "Root 1 = " << x1 << endl; cout << "Root 2 = " << x2 << endl; } else { cout << "Imaginary Roots." << endl; x1 = -b/(2*a); x2 = sqrt(-d)/(2*a); cout << "Root 1 = " << x1 << endl; cout << "Root 2 = " << x2 << endl; } } return 0; } // aiuto

C++ program to perform Binary Search

// Iterative Binary Search #include #include #include using namespace std; /** This is the important part **/ int binarySearch(int haystack[], int needle, int length) { int low = 0; int high = length; int mid = (low+high)/2; while (high >= low) { if (haystack[mid] == needle) return mid; else { if (needle > haystack[mid]) low = mid + 1; else high = mid - 1; mid = (low+high)/2; } } return -1; // not found } /** *** **/ int main() { // Replace haystack with user input if needed // Haystack MUST BE SORTED int haystack[10] = {10, 11, 20, 22, 30, 33, 40, 44, 50, 55}; int length = sizeof(haystack)/4; srand(time(NULL)); int needle = haystack[rand()%10]; cout << "This is the array: "; for (int i=0; i

C++ program to implement Queue

// Implementation of Queue #include using namespace std; static int front, rear, size; static int q[100]; void pop(); void push(int n); void showQueue(); void push(int n) { if (rear==(size-1)){ cout << "Queue full." << endl; } else if ((front==-1) && (rear == -1)){ front = rear = 0; q[front] = n; } else { q[++rear] = n; } showQueue(); } void pop() { if (rear==-1 && front==-1) cout << "Queue empty." << endl; else if (front==rear && front!=-1) front = rear = -1; else for (int i=0; i> choice; switch(choice) { case 1: { cout << "Enter number to add: "; int n; cin >> n; push(n); break; } case 2: { pop(); break; } case 3: { exit = 1; break; } default: break; } } } // aiuto

C++ program to perform Prim's Algorithm

// Prim's Algorithm #include using namespace std; int main() { int v, i, j, min, c, current, nv; // Assume maxium weight as 1000 int INF = 1000; // Create graph of v vertices cout << "Enter the number of vertices in the graph: "; cin >> v; int adj[v+1][v+1]; for (i=1; i<=v; i++) { for (j=1; j<=v; j++) { adj[i][j] = -1; } } bool visited[v+1]; int path[v+1]; int distance[v+1]; for (i=1; i<=v; i++) { for (j=1; j<=v; j++) { if (j==i) { adj[i][j] = 0; } else { if (adj[i][j] >= 0) { // do nothing } else { cout << "If vertex " << i << " is adjacent to vertex " << j << endl; cout << "Enter weight of edge, else enter 0: "; cin >> adj[i][j]; adj[j][i] = adj[i][j]; cout << endl; } } } } // Initializing the 3 arrays for (i=1; i<=v; i++) { visited[i] = false; path[i] = 0; distance[i] = INF; } current = 1; visited[current] = true; nv = 1; while ( nv != v ) { // Processing the adjacent vertices for (i=1; i<=v; i++) if (adj[current][i] != 0) if(visited[i] != true) if(distance[i] > adj[current][i]) { distance[i] = adj[current][i]; path[i] = current ; } // Finding the closest vertex min = INF ; for (i = 1; i <= v; i++) if(visited[i] != true) if(distance[i] < min) { min = distance[i]; current = i; } visited[current] = true; nv = nv + 1; } c=0; for (i=2; i<=v; i++) { c = c + distance[i]; cout << "Vertex " << i << " is connected to vertex " << path[i]; cout << endl; } cout << "Minimum cost is " << c; cout << endl; return 0; } // aiuto

C++ program to solve n Queens Problem

// n Queens problem #include #include using namespace std; void nQueens(int r, int n, int column[]); bool place(int r, int c, int column[]); int main() { int n; cout << "Enter number of queens: "; cin >> n; // Creating n+1 because we'll count from 1 int column[n+1]; // Place the 1st queen in the row nQueens(1, n, column); return 0; } void nQueens(int r, int n, int column[]) { int i, c; for (c=1; c<=n; c++) if (place(r, c, column)) { // placing Queen r in Row r & Column c. column[r] = c; if (r == n) // all Queens are placed. { for (i = 1; i <= n; i++) cout << "Queen no. " << i << " is placed in Row no. " << i << " and column no. " << column [i] << endl; exit(0); } else nQueens (r+1, n, column); // placing the next Queen. } } bool place(int r, int c, int column[]) { int j; for (j=1; j<=r-1; j++) if ((column[j] == c) || ((abs(column[j] - c)) == (abs(j - r)))) return false ; return true ; } // aiuto

C++ program to perform Matrix Multiplication

// Matrix Multiplication #include #include using namespace std; int main() { int a[10][10],b[10][10],c[10][10],row,col,i,j; cout<<"Enter number of rows of A: "; cin>>row; cout<<"Enter number of columns of A: "; cin>>col; cout<>a[i][j]; } cout<>b[i][j]; } cout<