Saturday, 2 September 2017
Thursday, 31 August 2017
BFS Traversal using <Vector> in C++
/** Node Starts from 0 not 1.
This program will traverse all the nodes from a starting point*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int node;
while(cin>>node)
{
int edge;
cin>>edge;
vector<int> Graph[node];
int Visit[node];
memset(Visit,-1,sizeof(Visit));
int u,v;
while(edge--)
{
cin>>u>>v;
Graph[u].push_back(v);
Graph[v].push_back(u);
}
queue<int> bi;
int startNode;
cin>>startNode;
bi.push(startNode);Visit[startNode]=1;
while(!bi.empty())
{
int f=bi.front();
cout<<f<<"-->";
bi.pop();
for(vector<int>::iterator it=Graph[f].begin();it!=Graph[f].end();it++)
{
if(Visit[*it]==-1)
{
Visit[*it]=1;
bi.push(*it);
}
}
}
cout<<"end\n";
}
return 0;
}
DFS Traversal using <Vector> in C++
/*************
!done
**************/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int nodes,edges;
while(cin>>nodes>>edges){
vector<int> Graph[nodes];
int u,v;
while(edges--)
{
cin>>u>>v;
Graph[u].push_back(v);
//Graph[v].push_back(u);
}
int Visit[nodes];
memset(Visit,-1,sizeof(Visit));
stack<int> st;
int start;
cin>>start;
st.push(start);
Visit[start]=1;
while(!st.empty())
{
int top=st.top();
st.pop();
cout<<top<<"-->";
for(vector<int>::iterator i=Graph[top].begin();i!=Graph[top].end();i++)
{
if(Visit[*i]==-1) {
st.push(*i);
Visit[*i]=1;
}
}
}
cout<<"-->end\n";
}
return 0;
}
Thursday, 5 January 2017
Using Divide And Conqure Technique finding maximum and minimum value of an array
Language C++
/************done!
************/
#include<bits/stdc++.h>
using namespace std;
int a[1000];
void DAndCMaxMin(int i,int j,int &maxx,int &minx)
{
int min1,max1;
if(i==j){/**smallp*/
maxx=a[i];
minx=a[i];
return;
}
if( i==j-1 )/**smallp*/
{
if(a[i]<a[j]){
maxx=a[j];
minx=a[i];
}
else
{
maxx=a[i];
minx=a[j];
}
return;
}
else
{
int mid=(i+j)/2;
/************
dividing part
************/
DAndCMaxMin(i,mid,maxx,minx);
DAndCMaxMin(mid+1,j,max1,min1);
/*************
combining part
*************/
if(maxx<max1)
maxx=max1;
if(minx>min1)
minx=min1;
}
}
int main()
{
int n;
while(cin>>n)
{
int maxx=0,minx=0;
for(int i=0;i<n;i++)
cin>>a[i];
DAndCMaxMin(0,n-1,maxx,minx);
cout<<"Maximum value is "<<maxx<<endl;
cout<<"Minimum value is "<<minx<<endl;
}
return 0;
}
Sunday, 1 January 2017
Java:Sorting an array
//This program will take some random values and sort them in ascending order
import static java.lang.Math.abs;
import java.util.*;
import java.util.*;
public class SortingAnArray {
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int n;
n=obj.nextInt();
int a[]=new int[n];
Random ob=new Random();
for(int i=0;i<n;i++)
{
a[i]=ob.nextInt()%9;
if(a[i]<0)
a[i]=abs(a[i]);
}
System.out.println("Elements before sorting");
for(int i:a)
{
System.out.printf("%d ",i);
}
System.out.println();
Arrays.sort(a);
System.out.println("Elements after sorting");
for(int i:a)
{
System.out.printf("%d ",i);
}
System.out.println();
}
}
Saturday, 29 October 2016
STL:<stack> container
For using stack container we have to include <stack> in our program.We already know that stack is last in first out system.Mainly stack has two operations 1.push operation & 2.pop operation.
There are 5 member functions in the <stack> container.They are:
Suppose x is a stack
1.x.push(value);//for inserting a value in stack.
2.x.pop();//for removing the top element.
3.x.top();returns the top element of the stack.
4.x.empty();returns a boolean value that the is empty or not.
5.x.clear();for removing the entire stack.
Thursday, 27 October 2016
SORTING A LINKED LIST
/*******************
linked list
bubble sort
complexity:O(n^2)
*******************/
#include <bits/stdc++.h>
using namespace std;
class linked_list
{
public:
int data;
class linked_list *next;
};
typedef linked_list node;
void show(node *p){
while(p){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
return;
}
void sort(node *p)
{
while(p->next){
node *test=p->next;
while(test){
if(test->data<p->data)
swap(test->data,p->data);
test=test->next;
}
p=p->next;
}
}
void create(node *p){
char c;
scanf("%d%c", &p->data, &c);
if(c=='\n')
p->next=NULL;
else
{
p->next=new node;
create(p->next);
}
return;
}
int main(){
bool x;
while(cin>>x){
node *head;
head=new node;
create(head);
cout<<"list before sorting\n";
show(head);
sort(head);
cout<<"list after sorting\n";
show(head);
}
return 0;
}
linked list
bubble sort
complexity:O(n^2)
*******************/
#include <bits/stdc++.h>
using namespace std;
class linked_list
{
public:
int data;
class linked_list *next;
};
typedef linked_list node;
void show(node *p){
while(p){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
return;
}
void sort(node *p)
{
while(p->next){
node *test=p->next;
while(test){
if(test->data<p->data)
swap(test->data,p->data);
test=test->next;
}
p=p->next;
}
}
void create(node *p){
char c;
scanf("%d%c", &p->data, &c);
if(c=='\n')
p->next=NULL;
else
{
p->next=new node;
create(p->next);
}
return;
}
int main(){
bool x;
while(cin>>x){
node *head;
head=new node;
create(head);
cout<<"list before sorting\n";
show(head);
sort(head);
cout<<"list after sorting\n";
show(head);
}
return 0;
}
Saturday, 27 August 2016
Stack by linked list
//here is a program that will create a linked stack
#include<iostream>
#include<stdio.h>
using namespace std;
struct linked_lists
{
int data;
struct linked_lists *next;
};
typedef struct linked_lists node;
void create(node *p);
void show(node *p);
node* push(node *p);
node* pop(node *p);
int main()
{
node *head;
head=new node;
create(head);
show(head);
int c;
bool x=true;
while(x)
{
cout<<"1 push"<<endl;
cout<<"2 pop"<<endl;
cout<<"3 exit"<<endl;
cin>>c;
switch(c)
{
case 1:
head=push(head);
show(head);
break;
case 2:
head=pop(head);
show(head);
break;
case 3:
x=false;
break;
}
}
return 0;
}
void create(node *p)
{
char c;
scanf("%d%c",&p->data,&c);
if(c=='\n')
{
p->next=NULL;
}
else
{
p->next=new node;
create(p->next);
}
return;
}
void show(node *p)
{
if(p==NULL)
{
cout<<"empty"<<endl;
}
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
return;
}
node* push(node *p)
{
cout<<"value:";
node *n,*h;
h=p;
n=new node;
cin>>n->data;
n->next=NULL;
if(p==NULL){
p=n;
return p;
}
while(p->next)
{
p=p->next;
}
p->next=n;
return h;
}
node* pop(node *p)
{
node *h;
h=p;
//if(p==NULL)
//{
// return NULL;
//}
if(p->next==NULL)
{
p=p->next;
return p;
}
while(p->next->next!=NULL)
{
p=p->next;
}
delete p->next;
p->next=NULL;
return h;
}
#include<stdio.h>
using namespace std;
struct linked_lists
{
int data;
struct linked_lists *next;
};
typedef struct linked_lists node;
void create(node *p);
void show(node *p);
node* push(node *p);
node* pop(node *p);
int main()
{
node *head;
head=new node;
create(head);
show(head);
int c;
bool x=true;
while(x)
{
cout<<"1 push"<<endl;
cout<<"2 pop"<<endl;
cout<<"3 exit"<<endl;
cin>>c;
switch(c)
{
case 1:
head=push(head);
show(head);
break;
case 2:
head=pop(head);
show(head);
break;
case 3:
x=false;
break;
}
}
return 0;
}
void create(node *p)
{
char c;
scanf("%d%c",&p->data,&c);
if(c=='\n')
{
p->next=NULL;
}
else
{
p->next=new node;
create(p->next);
}
return;
}
void show(node *p)
{
if(p==NULL)
{
cout<<"empty"<<endl;
}
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
return;
}
node* push(node *p)
{
cout<<"value:";
node *n,*h;
h=p;
n=new node;
cin>>n->data;
n->next=NULL;
if(p==NULL){
p=n;
return p;
}
while(p->next)
{
p=p->next;
}
p->next=n;
return h;
}
node* pop(node *p)
{
node *h;
h=p;
//if(p==NULL)
//{
// return NULL;
//}
if(p->next==NULL)
{
p=p->next;
return p;
}
while(p->next->next!=NULL)
{
p=p->next;
}
delete p->next;
p->next=NULL;
return h;
}
Sunday, 21 August 2016
Sunday, 14 August 2016
SHOWING THE SECOND LAST ELEMENT OF A LINKED LISTS
Language=C
#include<stdio.h>
#include<stdlib.h>
struct linked_lists
{
int data;
struct linked_lists *next;
};
typedef struct linked_lists node;
void create( node *list );
void show_before_last( node *list );
int main()
{
node *head;
head = ( node* ) malloc( sizeof( node ) );
create( head );
show_before_last( head );
return 0;
}
void create( node *list )
{
char c;
scanf( "%d%c",&list->data,&c );
if(c == '\n')
list->next = NULL;
else
{
list->next = ( node* ) malloc ( sizeof( node ) );
create( list->next );
}
return;
}
void show_before_last( node *list )
{
while( list->next->next )
list = list->next;
printf( "%d\n",list->data );
return;
}
Wednesday, 3 August 2016
SWAPPING TO NUMBERS WITHOUT TAKING ANY TEMPORARY VARIABLE
/*We generally swap or interchange to variables by taking a temporary variable.Here is a simple program for swapping to numbers without using any intermediate variable*/
Language=C
#include<stdio.h>
int main()
{
int a,b;
printf( "Enter the value of a and b=" );
scanf( "%d%d",&a,&b );
printf( "a and b before swapping\n" );
printf( "%d %d\n",a,b );
a = a+b;
b = a-b;
a = a-b;
printf( "a and b after swapping\n" );
printf( "%d %d\n",a,b );
return 0;
}
Saturday, 30 July 2016
ADDING TWO NUMBERS UP TO 10000 DIGITS
/*We know that by using int ( integer type ) variables we can store it a number up to 9 or 10 digits.Even long int type can store up to 18 or 19 digits.So we can not store or add two numbers greater than this digits.No problem.Here is a simple program that could add two numbers up to 10000 digits.*/
#include <iostream>
using namespace std;
int main()
{
string x,y;
int a[10000],b[10000],C[10000],k = 0;
int counter,counter2;
cin >> x >> y;
if( x.length() >= y.length() ){
for( counter = 0; counter < x.length(); counter++ )
a[counter] = x[counter] - 48;
for( counter2 = 0; counter2 < y.length(); counter2++ )
b[counter2] = y[counter2] - 48;
}
if( x.length() < y.length() ){
for( counter = 0; counter < y.length(); counter++)
a[counter] = y[counter] - 48;
for( counter2 = 0; counter2 < x.length(); counter2++)
b[counter2] = x[counter2] - 48;
}
int rem = 0;
for( int i = counter-1, j = counter2-1; i >=0 || j>=0; i--, j--, k++ )
{
if( i>=0 && j>=0 )
{
int c = a[i]+b[j] + rem;
if(c < 10)
{
rem = 0;
C[k] = c;
}
else
{
c = c-10;
rem = 1;
C[k] = c;
}
}
else
{
int c = a[i]+rem;
if(c<10)
{
rem = 0;
C[k] = c;
}
else
{
c = c-10;
rem = 1;
C[k] = c;
}
}
}
if(rem == 1)
C[k++] = rem;
for(int i = k-1; ;i--)
{
if(C[i] == 0){
k--;
}
else
{
break;
}
}
for(int i = k-1; i >= 0; i--)
cout << C[i];
cout << endl;
return 0;
}
Thursday, 28 July 2016
Friday, 22 July 2016
Thursday, 21 July 2016
OBJECT ORIENTED PROGRAMMING EXAMPLE
Basic logic gates operations in C++
// there is a simple object oriented program that will read the input values of different logic gates and show their output values.
#include <iostream>
using namespace std;
class basic_gate
{
//member variables
bool input1;
bool input2;
bool output;
public :
basic_gate()//base class constructor for initializing member variables to false
{
input1 = false;
input2 = false;
output = false;
}
void set_input(bool a,bool b)//for setting the input
{
input1 = a;
input2 = b;
}
void set_input(bool a)//overloading set_input()
{
input1 = a;
}
void set_output(bool x)//for setting the output
{
output = x;
}
bool get_input1()//for returning 1st input
{
return input1;
}
bool get_input2()//for returning 2nd input
{
return input2;
}
bool get_output()//for returning the output
{
return output;
}
virtual void operation() = 0;//declaring a pure virtual member function
};
class AND_gate : virtual public basic_gate//here base class is inherited as virtual
{
public :
void operation()//a new definition of operation function
{
bool o;
o = this -> get_input1() * this -> get_input2();//processing output
this -> set_output(o);//setting the output
}
};
class OR_gate : virtual public basic_gate//here the base class is inherited as virtual
{
public :
void operation()//another definition of operation function
{
bool o;
o = this -> get_input1() + this -> get_input2();//processing output
this -> set_output(o);//setting the output
}
};
class NOT_gate : virtual public basic_gate//base class is inherited as virtual
{
public :
void operation()//another definition of operation function
{
if(this -> get_input1() == false)
this -> set_output(true);//setting the output
else
this -> set_output(false);
}
};
class NAND_gate : public AND_gate , public NOT_gate//this derived class derived from two base classes
{
void operation()//another definition of operation function
{
AND_gate :: operation();
this -> set_input( this -> get_output() );
NOT_gate :: operation();
}
};
class NOR_gate : public OR_gate , public NOT_gate//this derived class also derived from two base classes
{
void operation()
{
OR_gate :: operation();
this -> set_input( this -> get_output() );
NOT_gate::operation();
}
};
int main()
{
bool X,Y;
basic_gate *ptr;//declare a base type pointer
AND_gate A;
ptr = &A;//point the derived class
cout << "Enter the inputs of AND gate:";
cin >> X >> Y;
ptr -> set_input(X,Y);
ptr -> operation();
cout << ptr -> get_output() << endl;
OR_gate B;
ptr = &B;//point the derived class
cout << "Enter the inputs of OR gate:";
cin >> X >> Y;
ptr -> set_input(X,Y);
ptr -> operation();
cout << ptr -> get_output() << endl;
NOT_gate c;
ptr = &c;//point the derived class
cout << "Enter the inputs of NOT gate:";
cin >> X;
ptr -> set_input(X);
ptr -> operation();
cout << ptr -> get_output() << endl;
NAND_gate r;
ptr = &r;//point the derived class
cout << "Enter the inputs of NAND gate:";
cin >> X >> Y;
ptr -> set_input(X,Y);
ptr -> operation();
cout << ptr->get_output() << endl;
NOR_gate z;
ptr = &z;//point the derived class
cout << "Enter the inputs of NOR gate:";
cin >> X >> Y;
ptr -> set_input(X,Y);
ptr -> operation();
cout << ptr -> get_output() << endl;
return 0;
}
Friday, 15 July 2016
Monday, 11 July 2016
Monday, 4 July 2016
Introduction:
What is Computer Programming?This is the burning question for all who are interested in the fact Computer.Textbook definition of programming is that "some finite sets of work for doing a particular job".But I dont think so.Before learning programming you have to learn about the history of computer,how it works,how it is related to humanlife,mainly you have to understand Computer.Programming is not only for solving some problems on different online judges or participating in programming contestes or like that.I believe that programming is like talking to a computer.So,programmers forget about the definition of programming,sense it,realize it,love it and start programming.
Subscribe to:
Posts (Atom)
Kruskal Algorithm
Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...
-
STL:<stack> container For using stack container we have to include <stack> in our program.We already know that stack is last...
-
ADDING TWO NUMBERS UP TO 10000 DIGITS /*We know that by using int ( integer type ) variables we can store it a number up to 9 or 10 dig...
-
Searching Algorithms