Showing posts with label source code. Show all posts
Showing posts with label source code. Show all posts

Sunday, 6 December 2015

Diamond shape Source code example in c/c++ source code, Diamond shape code example source code, source code,

Diamond shape Source code example in c/c++ source code, Diamond shape code example source code, source code,// LCM CODE Example.cpp : Defines the entry point for the console application.


// diamond2.cpp : Defines the entry point for the console application.
// 








#include "stdafx.h"
#include <iostream.h>
int main()
{
int Max_DiamondRange = 100, MainCounter = 1;
int LoopRange = ((Max_DiamondRange - 2)/4); //For Running Upper and Lower Loop Seprate.


int StartRange = 1;
int EndRange = 0;


//Print 1st *
cout<<"Diamond Structure with "<<Max_DiamondRange<<" '*'."<<endl<<endl<<endl;
while((LoopRange+1) >= MainCounter){
cout<<" ";
MainCounter++;
}
cout<<"*"<<endl;


//Generate Upper Body of the diamond
MainCounter = 1;
while(LoopRange >= MainCounter){

StartRange = 1;
EndRange = (LoopRange - MainCounter) + 1;
while(EndRange >= StartRange){
cout<<" ";
StartRange++;
}

cout<<"*";

StartRange = 1;
EndRange = (2 * MainCounter)-1;
while(EndRange >= StartRange){
cout<<" ";
StartRange++;
}

cout<<"*"<<endl;

MainCounter++;
}

//Generate lower Body of the diamond
MainCounter = 1;
while(LoopRange >= MainCounter){

StartRange = 1;
EndRange = MainCounter;
while(EndRange >= StartRange){
cout<<" ";
StartRange++;
}

cout<<"*";

StartRange = 1;
EndRange = ((LoopRange * 2) - (MainCounter * 2)+1);
while(EndRange >= StartRange){
cout<<" ";
StartRange++;
}

cout<<"*"<<endl;

MainCounter++;
}


//Print last *
MainCounter = 1;
while((LoopRange+1) >= MainCounter){
cout<<" ";
MainCounter++;
}
cout<<"*"<<endl<<endl;



cout<<"HeHeHe !"<<endl;
return 0;
}

Diamond shape example in c/c++ source code, Diamond shapeexample source code, source code,
Diamond shape example in c/c++ source code, Diamond shapeexample source code, source code,
Diamond shape example in c/c++ source code, Diamond shapeexample source code, source code,
Diamond shape example in c/c++ source code, Diamond shapeexample source code, source code,

Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com/2015/12/lcm-source-code-example-in-cc-source.html#sthash.oxxMKP7j.dpuf

LCM Source code example in c/c++ source code, LCM code example source code, source code,

LCM example in c/c++ source code, LCM example source code, source code,// LCM CODE Example.cpp : Defines the entry point for the console application.












# include <iostream>
# include <string >
using namespace std;
int main()
{
         int a,b,c;
         cout<< "Enter two nos :"<<endl;
         cout<<endl;
         cout<< "Enter first no. : ";
         cin>>a;
         cout<< "Enter sec. no. : ";
         cin>>b;
         c=a*b;
         while(a!=b)
              {
                    if(a>b)
                    a=a-b;
                    else
                    b=b-a;
               }
          cout<< "HCF = " << a<<endl;
          cout<< "LCM = " << c/a<<endl;
return 0;
}

LCM example in c/c++ source code, LCM example source code, source code,
LCM example in c/c++ source code, LCM example source code, source code,
LCM example in c/c++ source code, LCM example source code, source code,
LCM example in c/c++ source code, LCM example source code, source code,

Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends.

Saturday, 10 October 2015

if else example in c/c++ source code, if else example source code, source code,

if else example in c/c++ source code, if else example source code, source code,
// if else Example.cpp : Defines the entry point for the console application.
//











#include "stdafx.h"
#include<iostream.h>

int main(int argc, char* argv[])
{
char s;
cin>>s;
if(s!=1)
{
cout<<"if";
}
else
{
cout<<"else";
}


return 0;
}

if else example in c/c++ source code, if else example source code, source code,
if else example in c/c++ source code, if else example source code, source code,
if else example in c/c++ source code, if else example source code, source code,
if else example in c/c++ source code, if else example source code, source code,

Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com  

hash table in c/c++ source code, hash table source code, source code,

hash table in c/c++ source code, hash table source code, source code,
// hash table.cpp : Defines the entry point for the console application.
//  //


For More:>  Hash table......, general tree..., binary tree....., Graph in c++....., General Tree.... and many more...... 










#include "stdafx.h"
#include<iostream>
#include<string>

using namespace std;
struct daata
{
int id;
int data;
};
struct hasher
{
daata dt[11];
int name1;

hasher();
int hash(int &id);
int rehash(int &id);
int add(daata &d);
int remove(daata &d);
void output();
};

int hasher::hash(int &id)
{
return (id%11);
}
int hasher::rehash(int &id)
{
return ((id+1)/11);
}

hasher::hasher()
{
int i;
for(i=0; i=11; i++)
{
dt[i].id=-1;
dt[i].data=0;
}
int numel=0;
}
int i=0;
int hasher::add(daata &d)
{
if(i < 11)
{
int hashed=hash(d.id);
if(hashed>=0 && hashed<=10 && dt[hashed].id==-1)
{
dt[hashed].id=d.id;
dt[hashed].data=d.data;
return 0;
}
else
{
int i=0;
while(i<=10)
{
hashed=rehash(hashed);
if(dt[hashed].id==-1)
{
dt[hashed].id=d.id;
dt[hashed].data=d.data;
return 0;
}
else
{
if(i==11)
{
return -1;
}
i++;
}
}
}i++;

}
else
{
return (-1);
}
}

int hasher::remove(daata &d)
{
int hashed=hash(d.id);
if(dt[hashed].id==d.id)
{
dt[hashed].id=-1;
i=-1;
return 0;
}
else
{
int i=0;
while(i<=10)
{
hashed=rehash(hashed);
if(dt[hashed].id==d.id)
{
dt[hashed].id=-1;
i=-1;
return 0;
}
else
{
if(i==11)
{
return -1;
}
i++;
}
}
}
}

void hasher::output()
{
int i;
for(i=0; i<11; i++)
{
cout<<i<<"->"<<dt[i].data<<endl;
}
}

int main(int argc, char* argv[])
{

int id=100;
int ret;
daata d;
d.data=52005;
hasher h1;

while(ret != 1)
{
d.id=id;
ret=h1.add(d);
id += (id/2);
}
d.id=271861;
h1.remove(d);
h1.output();
return 0;

}

Graph in c/c++ source code, Graph source code, source code,

Graph in c/c++ source code, Graph source code, source code,
// graph.cpp : Defines the entry point for the console application.
//
//


For More:>  Sorting......, selection sort..., merge sort....., insertion Sort....., General Tree.... and many more......










#include "stdafx.h"
#include<iostream>
#include <list>

using namespace std;

// This class represents a directed graph using adjacency list representation
struct Graph
{
    int V;    // No. of vertices
    list<int> *adj; // Pointer to an array containing adjacency lists
    Graph(int V);  // Constructor
    void addEdge(int v, int w); // function to add an edge to graph
    void BFS(int s);  // prints BFS traversal from a given source s
};

Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}

void Graph::addEdge(int v, int w)
{
   adj[v].push_back(w); // Add w to v’s list.
}

void Graph::BFS(int s)
{
    // Mark all the vertices as not visited
    bool *visited = new bool[V];
    for(int i = 0; i < V; i++)
        visited[i] = false;

    // Create a queue for BFS
    list<int> queue;

    // Mark the current node as visited and enqueue it
    visited[s] = true;
    queue.push_back(s);

    // "i" will be used to get all adjacent vertices of a vertex
    list<int>::iterator i;

    while(!queue.empty())
    {
        // Dequeue a vertex from queue and print it
        s = queue.front();
        cout << s << " ";
        queue.pop_front();

        // Get all adjacent vertices of the dequeued vertex s
        // If a adjacent has not been visited, then mark it visited
        // and enqueue it
        for(i = adj[s].begin(); i != adj[s].end(); ++i)
        {
            if(!visited[*i])
            {
                visited[*i] = true;
                queue.push_back(*i);
            }
        }
    }
}

int main()
{
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
    g.addEdge(1, 2);
    g.addEdge(2, 0);
    g.addEdge(2, 3);
    g.addEdge(3, 3); 


    int c;
    cout<<"enter values to starting vertex for traversal";
    cin>>c;
    cout << "Following is Breadth First Traversal (starting from vertex "<<c<<") \n";
    g.BFS(c);

    return 0;

}

Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com 

Saturday, 3 October 2015

Binary search Tree in c/c++ source code, Binary search tree, source code,

Binary search Tree in c/c++ source code, Binary search tree, source code,
// bin search 22.cpp : Defines the entry point for the console application.
//



For More:>  Sorting......, selection sort..., merge sort....., insertion Sort....., General Tree.... and many more......







#include "stdafx.h"

#include <iostream.h>
#include <process.h> //for exit(1)
#include <conio.h>

struct node{
    int data;
    struct node *left;
    struct node *right;
};

class BST{
    public:
    node *tree;
    BST(){
        tree=NULL;
    }
    void createTree(node **,int item);    //For Building Tree
void preOrder(node *);     //For Tree Traversal
void inOrder(node *);
    void postOrder(node *);

    void determineHeight(node *,int *);
    int totalNodes(node *);
    int internalNodes(node *); //no. of non-leaf nodes
int externalNodes(node *); //no. of leaf nodes.
void removeTree(node **); //Remove tree from memory.

    node **searchElement(node **,int);
    void findSmallestNode(node *);
    void findLargestNode(node *);
    void deleteNode(int);
};


//it is used for inseting an single element in//a tree, but if calls more than once will create tree.
void BST :: createTree(node **tree,int item){
    if(*tree == NULL){
        *tree = new node;
        (*tree)->data = item;
        (*tree)->left = NULL;
        (*tree)->right = NULL;
    }
    else{
        if( (*tree)->data > item)
            createTree( &((*tree)->left),item);
        else
            createTree( &((*tree)->right),item);
    }
}


void BST :: preOrder(node *tree){
    if( tree!=NULL){
        cout<<"   "<< tree->data;
        preOrder(tree->left);
        preOrder(tree->right);
    }
}

void BST :: inOrder(node *tree){
    if( tree!=NULL){
        inOrder( tree->left);
        cout<<"   "<< tree->data;
        inOrder(tree->right);
    }
}


void BST :: postOrder(node *tree){
    if( tree!=NULL){
        postOrder( tree->left);
        postOrder( tree->right);
        cout<<"   "<<tree->data;
    }
}


void BST :: determineHeight(node *tree, int *height){
    int left_height, right_height;
    if( tree == NULL)
        *height = 0;
    else{
        determineHeight(tree->left, &left_height);
        determineHeight(tree->right, &right_height);
        if( left_height > right_height)
            *height = left_height + 1;
        else
            *height = right_height + 1;
    }
}


int BST :: totalNodes(node *tree){
    if( tree == NULL)
        return 0;
    else return( totalNodes(tree->left) + totalNodes(tree->right) + 1 );
}

int BST :: internalNodes(node *tree){
    if( (tree==NULL)  || (tree->left==NULL  && tree->right==NULL))
        return 0;
    else return( internalNodes(tree->left) + internalNodes(tree->right) + 1 );
}

int BST :: externalNodes(node *tree){
    if( tree==NULL )
        return 0;
    else 
if( tree->left==NULL  && tree->right==NULL)
        return 1;
    else return( externalNodes(tree->left) + externalNodes(tree->right));
}

void BST :: removeTree(node **tree){
    if( (*tree) != NULL)
{
        removeTree( &(*tree)->left );
        removeTree( &(*tree)->right );
        delete( *tree );
    }
}

node ** BST :: searchElement(node **tree, int item){
    if( ((*tree)->data == item) || ( (*tree) == NULL) )
        return tree;
    else if( item < (*tree)->data);
        return searchElement( &(*tree)->left, item)
    else return searchElement( &(*tree)->right, item)
}

void BST :: findSmallestNode(node *tree){
    if( tree==NULL || tree->left==NULL)
        cout<< tree->data;
    else
        findSmallestNode( tree->left);
}


//Finding In_order Successor of given node..//for Delete Algo.
node * find_Insucc(node *curr)
{
    node *succ=curr->right; //Move to the right sub-tree.
if(succ!=NULL){
        while(succ->left!=NULL)    //If right sub-tree is not empty.
            succ=succ->left; //move to the left-most end.
    }
    return(succ);
}


void BST :: findLargestNode(node *tree){
    if( tree==NULL || tree->right==NULL)
        cout<<tree->data;
    else
        findLargestNode(tree->right);
}


void BST :: deleteNode(int item){
    node *curr=tree,*succ,*pred;
    int flag=0,delcase;
    //step to find location of node
while(curr!=NULL && flag!=1)
    {
        if(item < curr->data){
            pred = curr;
            curr = curr->left;
        }
        elseif(item > curr->data){
            pred = curr;
            curr = curr->right;
        }
        else{ //curr->data = item
            flag=1;
        }
    }

    if(flag==0){
        cout<<"\nItem does not exist : No deletion\n";
        getch();
        goto end;
    }

    //Decide the  case of deletion
if(curr->left==NULL && curr->right==NULL)
        delcase=1; //Node has no child
else
if(curr->left!=NULL && curr->right!=NULL)
        delcase=3; //Node contains both the child
else
        delcase=2; //Node contains only one child//Deletion Case 1
if(delcase==1){
        if(pred->left == curr) //if the node is a left child
            pred->left=NULL; //set pointer of its parent
else
            pred->right=NULL;
        delete(curr); //Return deleted node to the memory bank.
    }

    //Deletion Case 2
if(delcase==2){
        if(pred->left==curr){ //if the node is a left child
if(curr->left==NULL)
                pred->left=curr->right;
            else
                pred->left=curr->left;
        }
        else{ //pred->right=curr
if(curr->left==NULL)
                pred->right=curr->right;
            else
                pred->right=curr->left;
        }
        delete(curr);
    }

    //Deletion case 3
if(delcase==3){
        succ = find_Insucc(curr); //Find the in_order successor//of the node.
int item1 = succ->data;
        deleteNode(item1);  //Delete the inorder successor
        curr->data = item1; //Replace the data with the data of//in order successor.
    }
end:
}



void main(){
    BST obj;
    int choice;
    int height=0,total=0,n,item;
    node **tmp;

    while(1){
        clrscr();
        cout<<"*****BINARY SEARCH TREE OPERATIONS*****\n\n";
        cout<<"--Binary Tree and Binary Search Tree common operations--\n";
        cout<<"1) Create Tree\n";
        cout<<"2) Traversal\n";
        cout<<"3) Height of Tree\n";
        cout<<"4) Total Nodes\n";
        cout<<"5) Internal Nodes \n";
        cout<<"6) External Nodes \n";
        cout<<"7) Remove Tree\n";
        cout<<"\n--Only Binary Search Tree Operations--\n";
        cout<<"8)  Insert Node\n";
        cout<<"9)  Search Node\n";
        cout<<"10) Find Smallest Node\n";
        cout<<"11) Find Largest Node\n";
        cout<<"12) Delete Node\n";
        cout<<"13) Exit\n";
        cout<<"Enter your choice : ";
        cin>>choice;
        switch(choice){
            case 1 : //Create Tree
                cout<<"\n\n--Creating Tree--";
                cout<<"\nHow many nodes u want to enter : ";
                cin>>n;
                for(int i=0;i<n;i++){
                    cout<<"Enter value : ";
                    cin>>item;
                    obj.createTree(&obj.tree,item);
                }
                break;

            case 2 : //All Traversals
                cout<<"\n\nInorder Traversal : ";
                obj.inOrder(obj.tree);

                cout<<"\n\nPre-order Traversal : ";
                obj.preOrder(obj.tree);

                cout<<"\n\nPost-order Traversal : ";
                obj.postOrder(obj.tree);
                getch();
                break;

            case 3 : //Determining Height of Tree
                obj.determineHeight(obj.tree,&height);
                cout<<"\n\nHeight of Tree : "<<height;
                getch();
                break;

            case 4 : //Total nodes in a tree
                total=obj.totalNodes(obj.tree);
                cout<<"\n\nTotal Nodes : "<<total;
                getch();
                break;

            case 5 : //Internal nodes in a tree
                total=obj.internalNodes(obj.tree);
                cout<<"\n\nInternal Nodes : "<<total;
                getch();
                break;

            case 6 : //External nodes in a tree
                total=obj.externalNodes(obj.tree);
                cout<<"\n\nExternal Nodes : "<<total;
                getch();
                break;

            case 7 : //Remove Tree from memory
                obj.removeTree(&obj.tree);
                cout<<"\n\nTree is removed from Memory";
                getch();
                break;

            case 8 : //Inserting a node in a tree
                cout<<"\n\n--Inserting Node in a tree--\n";
                cout<<"Enter value : ";
                cin>>item;
                obj.createTree(&obj.tree,item);
                cout<<"\nItem is inserted\n";
                getch();
                break;

            case 9 : //Search element
                cout<<"\n\n--Search Element--\n";
                cout<<"Enter item to searched : ";
                cin>>item;
                &(*tmp) = obj.searchElement(&obj.tree,item);
                if( (*tmp) == NULL)
                    cout<<"\nSearch Element Not Found";
                else
                    cout<<"\nSearch Element was Found";
                getch();
                break;

            case 10 : //Find Smallest Node
                cout<<"\n\nSmallest Node is :  ";
                obj.findSmallestNode(obj.tree);
                getch();
                break;

            case 11 : //Find Largest Node
                cout<<"\n\nLargest Node is :  ";
                obj.findLargestNode(obj.tree);
                getch();
                break;

            case 12 : //Deleting a node from a tree
                cout<<"\n\n--Deleting a Node from a tree--\n";
                cout<<"Enter value : ";
                cin>>item;
                obj.deleteNode(item);
                break;

            case 13 : exit(1);
        }//end of switch
    }
}
Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com 

LinkWithin