Skip to main content

Posts

Binary Search algorithm implementation in c++

Binary Search algorithm implementation in c++ #include <iostream> using   namespace   std ; int   recursive_binary_srch ( int   Arr [], int   firstIndex , int   lastIndex , int   item ) {      if (lastIndex >= firstIndex)     {          int  mid = firstIndex + (lastIndex - firstIndex) / 2 ;          if ( Arr [mid] == item)         {              return  mid;         }          if (item < Arr [mid])         {              return   recursive_binary_src...

How to replace a digit/letter into a string in C++ ?? ||Source code||

Open Source #include using namespace std; int main() { std::string s = "abc"; s[1] = '5'; //replacing std::cout Quote: “There is nothing more dreadful than the habit of doubt. Doubt separates people. It is a poison that disintegrates friendships and breaks up pleasant relations. It is a thorn that irritates and hurts; it is a sword that kills.” ― Buddha Siddhartha Guatama Shakyamuni

How to take integer as input into an array and decorate them in increasing order in C++ language??? // Sorting an Array

First taking integers randomly as input into an array then decorate those in increasing order #include using namespace std; int main() { int n; cin>>n; int arr[n]; for(int i=0;i >arr[i]; } cout arr[j+1]) { swap(arr[j],arr[j+1]); } } } for(int i=0;i Quote: “There is nothing more dreadful than the habit of doubt. Doubt separates people. It is a poison that disintegrates friendships and breaks up pleasant relations. It is a thorn that irritates and hurts; it is a sword that kills.” ― Buddha Siddhartha Guatama Shaky...

Using Backtracking Algorithm implement N-Queen Problem.

Free Discussion #include #include #include #define MAX 10 class nqueen { public : void placequeen(int); int place(int [MAX], int); }; int nqueen :: place(int x[MAX], int k) { int i; for (i = 1; i > n; nq.placequeen(n); getch(); return 0; } Quote: “It is hard to fail, but it is worse never to have tried to succeed.” -Theodore Roosevelt

Write a Program for Coloring graph.

Free Discussion #include #include int a[20][20],x[20],n,m; void next(int k) { int j; while(1) { x[k]=(x[k]+1)%(m+1); if(x[k]==0) return; for(j=1;j Quote: “It is hard to fail, but it is worse never to have tried to succeed.” -Theodore Roosevelt

Write a Program to solve the 0/1 Knapsack problem using Greedy method.

Free Discussion #include #include #define MAX 10 class knapsack { private : int n, capacity, value[MAX][MAX]; struct object { int weight; int value; } obj[MAX]; public : void getdetails(int &, int &); void initialise(); int mfknapsack(int, int); void display(int); }; void knapsack :: getdetails(int &no, int &cap) { int i; cout > n; no = n; cout > capacity; cap = capacity; cout > obj[i].weight; cout > obj[i].value; } } void knapsack :: initialise() { int i, j; for (i = 0; i temp) val = temp1; ...

Write a Program to find the Minimum cost spanning tree using Prim’s Algorithm.

Free Discussion #include #include #define MAX 10 class prims { private : int cost[MAX][MAX], tree[MAX][MAX]; int n; public : void readmatrix(); int spanningtree(int); void display(int); }; void prims :: readmatrix() { int i, j; cout > n; cout > cost[i][j]; } int prims :: spanningtree(int src) { int visited[MAX], d[MAX], parent[MAX]; int i, j, k, min, u, v, stcost; for (i = 1; i > source; treecost = pri.spanningtree(source); pri.display(treecost); getch(); return 0; } Quote: “Would you like me to give you a formula for success? I...

Write a Program to solve the Fractional Knapsack problem using Greedy method.

Free Discussion #include #include #include int knapsack(int n,int W); int n,i,w,W; int weight[50],v[50]; int C[50][50]; int main() { clrscr(); cout >n; cout >W; cout >weight[i]; } cout >v[i]; } knapsack(n,W); getch(); return 0; } int knapsack(int n,int W) { for(int c = 0; c C[i-1][w]) C[i][w]=v[i]+C[i-1][w-weight[i]]; else C[i][w]=C[i-1][w]; else C[i][w]=C[i-1][w]; // w[i]>w } cout Quote: “Great minds discuss ideas; average minds discuss events; small minds di...

Write a program to measure the performance using time function between bubble sort and quick sort algorithm.

Free Discussion #include #include void quick ( int arr[], int begin, int end ); int count=0, count_q=0; void bubble(int barr[],int n) { int i,j,t; for(i=n-2;i>=0;i--) { for(j=0;j barr[j+1]) { t=barr[j]; barr[j]=barr[j+1]; barr[j+1]=t; count=count+3; } count++; } count++; } } void main() { int barr[100],qarr[100],n,i; clrscr(); printf("\n\n Enter integer value for total no.s of elements to be sorted: "); scanf("%d",&n); for( i=0;i 2 ) { int left,right; left = begin + 1; count_q++; right = end; count_q++; while ( left qarr[begin] ) ...

Write A program to find the maximum and minimum number using Divide and Conquer method .

Free Discussion #include #include int max, min; int a[100]; void maxmin(int i, int j) { int max1, min1, mid; if(i==j) { max = min = a[i]; } else { if(i == j-1) { if(a[i] min1) min = min1; } } } void main () { int i, num; clrscr(); printf ("\n\t\t\tMAXIMUM & MINIMUM\n\n"); printf ("\nEnter the total number of numbers : "); scanf ("%d",&num); printf ("Enter the numbers : "); for (i=1;i OutPut: MAXIMUM &MINIMUM Enter the total number of numbers : 5 Enter the numbers:34 56 23 74 68 Maximum element in an array : 74 Minimum element in an array : 23 Quote: ...

Write a program to search an element using Binary search algorithm

Free Discussion #include #include int binary(int a[],int n,int m,int l,int u); int main() { clrscr(); int a[10],i,n,m,c,l,u; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: " ); for(i=0;i output: Enter the size of an array: 5 Enter the elements of the array: 8 9 10 11 12 Enter the number to be search: 8 Number is found. Quote: "My mission in life is not merely to survive, but to thrive; and to do so with some passion, some compassion, some humor, and some style..."

How to take input in lowercase or convert input string into lowercase in c++ ???

Free Discussion At first you should take input . then you will convert it into lowercase. Its so easy..! string myinput_string; getline(cin,myinput_string); for(int i=1;i so the total sample program will be: #inclue using namespace std; int main() { string myinput_string; getline(cin,myinput_string); for(int i=1;i Quote: "My mission in life is not merely to survive, but to thrive; and to do so with some passion, some compassion, some humor, and some style..."

Sum of Sub Set problem solution in C++

  #include <iostream> using   namespace   std ; bool   Subset ( int   set [], int   n , int   sum ) {     //Base Case      if (sum == 0 )     {          return   true ;     }      else   if (n == 0   &&  sum != 0 )     {          return   false ;     }      else   if ( set [n - 1 ] > sum)     {    //when the element is greater than Sum          return   Subset (set,n - 1 ,sum);     }      else     {      ...

Example of Friend function (2)

#include<iostream> using namespace std; class Rectangle; class Cost { private:     int costRate; public:     void setValue(int a)     {         costRate=a;     }     int totalCost(Rectangle A);     /*ekhane amra totalcost function create     korar pore bujhlam etar vetor jesob kaj korbo tar jonno rectangle     class er value er dorkar porbe,tai paramter hisabe oi class er object     pass korsi,,and function ta just declare kore rakhsi */ }; class Rectangle { private: ...

Example of Friend function (1)

#include<iostream> using namespace std; class Rectangle { private:     int height,weight ; public:     void getValue(int ,int );     int area()     {         return height*weight;     } }; void Rectangle::getValue(int a,int b) {     height=a;     weight=b; } class Cost { private:     int tk; public:     void getvalue(int c)     {         tk=c;     }     int totalCost(Rectangle A)     {         return tk*A.area();     } }; int main() {     int a,b;     cin>>a>>b;     Rectangle rct;     rct.getValue(a,b);     cout<< "Total area = "<<rct.area()<<endl;     int i;     Cost cst;     cout<<"enter cost per meter:...