Life Hacks

Write a program to solve the 8 Queens Problem.

#include<stdio.h> #include<conio.h> #include<math.h> int a[30],count=0; int place(int pos) { int i; for(i=1;i<pos;i++) { // condition for placing queen. if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos)))) return 0; } return 1; } void print_sol(int n) { int i,j; count++; printf("\n\nSolution #%d:\n",count); // if placing is right then count inc. for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(a[i]==j) printf("Q\t"); else printf("*\t"); } printf("\n"); } } void […]

Write a program to solve the Knapsack problem using greedy technique.

#include<iostream.h> #include<conio.h> void knapsack01(int v[ ], int w[ ], int n, int capacity) { int x = 0, b[100][100], i = 0; for(x = 0; x <= capacity; x++) b[0][x] = 0; //set first column to zero for(i = 1; i <= n; i ++) { for(x = 0; x <= capacity; x ++) { […]

Write a program of the Merge Sort.

#include <iostream.h> #include<conio.h> int a[50]; /* local variable */ void merge(int, int, int); void merge_sort(int low, int high) { int mid; /* local variable */ if(low < high) { mid = (low + high) / 2; merge_sort(low, mid); merge_sort(mid + 1, high); merge(low, mid, high); } } void merge(int low, int mid, int high) { […]

Write a program of the Quick Sort.

#include<iostream.h> #include<conio.h> int a[10], l, u, i, j; /* local variable */ void quick(int *, int, int); void main( ) /* main start */ { clrscr( ); cout << “ Enter 10 elements : ”; for(i = 0; i < 10; i ++) { cin >> a[i]; } /* End of for loop */ l […]

Write a program of the Heap Sort.

#include<iostream.h> #include<conio.h> int arr[20], n; class heap { private: int loc, num, par; /* local variable */ public: void del_root(int); void create_heap( ); void display( ); void heap_sort( ); void insert(int, int); }; void heap :: insert(int num, int loc) { int par; /* local variable */ while(loc > 0) { par = (loc – […]

Write a program of the Binary Search.

#include<iostream.h> #include<conio.h> void main( ) /* main start */ { clrscr( ); int a[20], n, c, lb, ub, item, mid, loc; /* local variable */ cout << " \n Enter the number of elements in an array is: " << " \n "; cin>>n; // entering no. of elements for the array. cout << " […]

Write a program of the Insertion Sort.

#include<iostream.h> #include<conio.h> void main( ) /* main start */ { clrscr( ); int a[50], n, i, ptr, temp; /* local variable */ cout << " Enter the number of elements in an array is : ”; cin >> n; // Enter the no. of elements wants to be sorted. a[0] = 0; cout << " […]

Write a program of the Selection Sort.

#include<iostream.h> #include<conio.h> void main( ) /* main start */ { clrscr( ); int array[10], n, i, j, temp; /* local variable */ cout << " Enter the number of elements in an array is : ”; cin >> n; // inserting no. of elements in the array. cout << “ Enter “ << “ “ […]

Write a program of the Bubble Sort.

#include<iostream.h> #include<conio.h> void main( ) /* main start */ { clrscr( ); int num[56], n, k, c, ptr, temp; /* local variable */ cout<<"enter the number of elements in an array is : ”; cin>>n; // inserting no. of elements in the array. cout<<"enter the value of elements of array is : "; for(c = […]

Write a program of the deletion in an array.

#include<iostream.h> #include<conio.h> void main( ) /* main start */ { clrscr( ); int a[20], j, n, k, b, c, item; /* local variable */ cout << " \n Enter the number of elements in an array is : " << " \n "; cin >> n; cout << " \n Enter the value of elements […]