Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Monday, December 26, 2022

Tuesday, December 20, 2022

Wednesday, September 21, 2016

BFS (breadth first search) in BST (binary search tree)

BFS code in red color highlighted.  
1) Insertion in BST
2) Normal Searching in BST.
3) BFS in BST


#include<iostream.h>
#include<conio.h>
class Node{
private:
 int data;
 Node* right;
 Node* left;
public:
 Node(){
  root=NULL;
  right=left=NULL;
 }Node *root;

 void insert(int data){
  Node* newnode = new Node();
  newnode->data = data;
   if (root == NULL){
   root=newnode;
   return;
  }
  Node *p,*q;
  p=q=root;
  while (q!=NULL){
   p=q;
   if (newnode->data> q->data)
    q = q->right;
   else q = q->left;
  }
  if (newnode->data> p->data)
   p->right = newnode;
  else p->left = newnode;
 }


 void inorder(Node *root){
  if (root == NULL) return;
  else {
   cout << root->data <<" ";
   inorder(root->left);
  // cout << root->data <<"  ";
   inorder(root->right);
   }
 }



void search(Node *root,int v)
{
int flag=0;
Node *q=root;
while(v != q->data)
{
if(v < q->data && q->left != NULL)
{
flag=2;
q=q->left;
}
else if( v > q->data && q-right !=NULL)
{
flag=2;
q=q->right;
}else{
flag=1;
cout<<"\n Node value not found . "<<endl;
break;
}
}
if(flag == 0 || flag ==2){
cout<<"\nNode is  found= "<<q->data<<endl;
 }
}

void BFS(Node* root,int value){
 Node **arry=NULL;
 int i=0,j=0;
 arry[i]=root;
 if(root->data == value){
 cout<<root->data<<" ";
 }
 else{           i++;
 while(arry[j]->data != value){
 Node *a=arry[j];
 if(a->left != NULL)
 {
  arry[i]=a->left;i++;
if( a->left->data==value)
{break;}
 }
 if(a->right !=NULL)
{
 arry[i]=a->right;i++;
if(a->right->data==value)
{break;}
 }
   j++;}//end of loop
for(int x=0;x<=(sizeof(arry)/sizeof(*arry));x++){
cout<<arry[x]->data<<" ";
    }//end of for
  }//end of else
}//end of function

};

void main()
{
int c,A[]={8,4,5,1,12,11,13,3,14};
 Node b;
 clrscr();
 for(int i=0;i<=8;i++){
 b.insert(A[i]);
 }

 b.inorder(b.root);
 getch();

cout<<"\n\nEnter Value For Search ";cin>>c;
b.BFS(b.root,c);
getch();

// b.search(b.root,c);
//getch();

// cout<<"\n\nInsert a New Value ";cin>>c;
// b.insert(c);
// b.inorder(b.root);
// getch();

}


Thursday, June 25, 2015

Graph Adjacency Matrix using 2D-Array

#include <iostream.h>
#include<conio.h>

void  main( )
 {
 int i, j ,siz;
 clrscr();
 int G[100][100];

cout<<" Enter the size of sequre Metrix  \n";
cout<<" Enter Rows siz :";
 cin>>i;
 cout<<" \n Enter Colums siz : "   ;
 cin>>j;
 siz=j;

 for(i=0 ; i < siz  ; i++)
 for(j=0 ; j< siz ; j++)
 {
 G[i][j]=0;
 }
 int a;
 cout<<" \n Enter the No of Edges: ";
 cin>>a;
 cout<<" \n Enter the exact location where  do you want  make edge  : "<<endl;
 for (int j1=1 ; j1 <= a ; j1++)
 {
 cout<<" Enter Row Number :";
 cin>>i;
 cout<<" Enter colum Number : ";
 cin>>j;
 G[i][j]=1;
 }
 cout<< " Adjancy Matrix ";
 for(i=0; i < siz; i++)
 {
 cout<< " \n ";
 for( j=0; j < siz ; j++)
 {
 cout<<"    "<< G[i][j];
 }
 cout<< " \n ";
 }
getch();
}




Graph Adjacency List using Link list

#include<iostream.h>
#include<conio.h>

struct Adj_list
{

int dest;
struct Adj_list *next;

};
struct Adjlist
{
struct Adj_list *head;

};


class Graph
{
int v;
int i;
public:
struct Adjlist *array;

Graph(int v)
{
this->v=v;
array=new Adjlist[v];
array[i].head=NULL;
}

Adj_list *newAdj_list(int dest)
{

Adj_list *newnode =new Adj_list;
newnode->dest=dest;
newnode->next=NULL;
return newnode;
}



void addedge(int src,int dest)
{

Adj_list *newnode=newAdj_list(dest);

newnode->next=array[src].head;
array[src].head=newnode;
newnode=newAdj_list(src);
newnode->next=array[dest].head;
array[dest].head=newnode;
}

void display()
{

int i;
for(i=0; i< v; i++)
{
Adj_list *pcrawl=array[i].head;
cout<<" \n Adjancy List of vertex  "<<i<<" head";

while(pcrawl)
{
cout<<" -> "<<pcrawl->dest;
pcrawl=pcrawl->next;

}
cout<<endl;
}



}
};
void main()
{
clrscr();
Graph obj(5);
obj.addedge(0,1);
obj.addedge(0,4);
obj.addedge(1,2);
obj.addedge(1,3);
obj.addedge(1,4);
obj.addedge(2,3);
obj.addedge(3,4);

obj.display();
getch();
}

Tuesday, June 9, 2015

HAng MaN Game in C++

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include <stdlib.h>
int a;
void main()
{

char ch[50],new1[50],word[50];
cout<<"              ------------------------------------------------"<<endl;
cout<<"                        Welcome To the HAng MaN Game "<<endl;
cout<<"              ------------------------------------------------"<<endl<<endl;
cout<<"                         |(((( Player OnE )))) | "<<endl<<endl ;
cout<<" HOW many character U Want to ENter == ";
cin>>a;
int g=11;

 ch[a],new1[a],word[a];
cout<<" \n \n";
cout<<"\n \n PLZ Insert Yr COmplete Words ==== ";

for(int b1=0;b1<a;b1++)
  { new1[b1]='_';

    cin>>ch[b1];
  }
f:
system("cls");

cout<<"                         ------------------------- \n ";
cout<<"                              PLayer Two  \n";
cout<<"                         ------------------------- \n\n ";
cout<<"                      ****************************** \n";
cout<<"                             GUess The characters  \n ";
cout<<"                      ****************************** \n \n \n";


cout<<"YOu have "<<g<<" tries Other wise your man will Hang \n\n";

cout<<"your Enter Charater Are === ";
for(int t=0;t<a;t++)
{
cout <<new1[t];
}
cout<<"\n";
if(g==10)
{
cout<<"                                                     /       "<<endl;
}
if(g==9)
{
cout<<"                                                     /       "<<endl;
cout<<"                                                    /        "<<endl;
}
if(g==8)
{
cout<<"                                                    __       "<<endl;
cout<<"                                              *-*-* /        "<<endl;
cout<<"                                                   /         "<<endl;
}
if(g==7)
{

cout<<"                                                    __        "<<endl;
cout<<"                                              *-*-* /         "<<endl;
cout<<"                                              ('_')/          "<<endl;
}
if(g==6)
{
cout<<"                                                    __        "<<endl;
cout<<"                                              *-*-* /         "<<endl;
cout<<"                                              ('_')/          "<<endl;
cout<<"                                                |             "<<endl;
}
if(g==5)
{

cout<<"                                                    __        "<<endl;
cout<<"                                              *-*-* /         "<<endl;
cout<<"                                              ('_')/          "<<endl;
cout<<"                                                |             "<<endl;
cout<<"                                                |             "<<endl;
}

if(g==4)
{
cout<<"                                                    __        "<<endl;
cout<<"                                              *-*-* /         "<<endl;
cout<<"                                              ('_')/          "<<endl;
cout<<"                                                |             "<<endl;
cout<<"                                               /|             "<<endl;


}
if(g==3)
{
cout<<"                                                    __        "<<endl;
cout<<"                                              *-*-* /         "<<endl;
cout<<"                                              ('_')/          "<<endl;
cout<<"                                                |             "<<endl;
cout<<"                                               /|\\           "<<endl;
}


if(g==2)
{
cout<<"                                                    __        "<<endl;
cout<<"                                              *-*-* /         "<<endl;
cout<<"                                              ('_')/          "<<endl;
cout<<"                                                |             "<<endl;
cout<<"                                               /|\\           "<<endl;
cout<<"                                                |             "<<endl;
}
if(g==1)
{
cout<<"                                                    __        "<<endl;
cout<<"                                              *-*-* /         "<<endl;
cout<<"                                              ('_')/          "<<endl;
cout<<"                                                |             "<<endl;
cout<<"                                               /|\\           "<<endl;
cout<<"                                                |             "<<endl;
cout<<"                                               / \\           "<<endl;

}
cout<<"\n\n";
cout<<" Enter charater ====" ;

int c,m=-1,l=0;
 for(int b=1;b<=a+a;b++)
 {
    cin>>word[b];
    for( c=0;c<a;c++)
      {
if(word[b]==ch[c])
{

new1[c]=word[b];

m=c;
}

       }
 if(m==-1)
g--;
 break;
 }


 for(int p=0;p<a;p++)
if(ch[p]==new1[p])
{
l++;
}
else
{ if(g>0)
 goto f;
system("cls");
cout<<"\n\n";
cout<<"                             -------------------         \n ";
cout<<"                             (((  You LOst ))))           \n ";
cout<<"                            -------------------         \n\n\n ";
cout<<"                                          __        "<<endl;
cout<<"                                    *-*-* /         "<<endl;
cout<<"                                    ('_')/          "<<endl;
cout<<"                                      |             "<<endl;
cout<<"                                     /|\\           "<<endl;
cout<<"                                      |             "<<endl;
cout<<"                                     / \\           "<<endl;

cout<<"Right Answer is ====== ";
for(int t=0;t<a;t++)
{
cout <<ch[t];
}
cout<<"\n\n\n\n\n";
}

if(l==a)
{
system("cls");

for(int t=0;t<a;t++)
{
cout <<new1[t];
}
cout<<"\n\n\n\n\n\n";
cout<<"                             -------------------         \n ";
cout<<"                              (((  You Won ))))           \n ";
cout<<"                            -------------------         \n\n\n\n\n ";

}
 getche();
}


Thursday, May 28, 2015

Typedef in C++

The typedef-names are aliases for existing types, and are not declarations of new types. Typedef cannot be used to change the meaning of an existing type name (including a typedef-name). Once declared, a typedef-name may only be redeclared to refer to the same type again. Typedef names are only in effect in the scope where they are visible: different functions or class declarations may define identically-named types with different meaning..



#include<iostream.h>
#include<conio.h>

typedef int usman;

typedef char name[10];

typedef struct fatima{
usman ID;
name nam;
}obj;

obj fatima1;
obj fatima2;
usman func1(usman b,usman b1)
{
name result="\nSum is= ";
usman sum;
sum=b+b1;
cout<<result<<sum;
return 0;
}

usman main(){
clrscr();
cout<<"\n Enter Two No: ";
cin>>fatima1.ID;
cin>>fatima2.ID;
cout<<"\n Enter Two Name: ";
cin>>fatima1.nam;
cin>>fatima2.nam;
cout<<"\n"<<fatima1.nam<<" "<<fatima2.nam<<"\n";
func1(fatima1.ID,fatima2.ID);
getch();
return 0;
}


Saturday, May 16, 2015

Heart ♥ in C++ .. ☻

#include<stdio.h>
#include<conio.h>
#include <iostream.h>

void main()
{
clrscr();
int num;
cout<<"Please Enter Number : ";
cin>>num;
cout<<"\n\n";
int g=num*2, x=num/2, v=0, m=num,n=0;

for(int q=0;q<(num/2);q++)
{
for(int u=0;u<x;u++)
printf(" ");
x--;
cout<<char(3);
for(int o=0;o<v;o++)
printf(" ");
v+=2;
cout<<char(3);
for(int l=0;l<m-1;l++)
printf(" ");
m-=2;
cout<<char(3);
for(int a=0;a<n;a++)
printf(" ");
n+=2;
cout<<char(3);
printf("\n");

}

for(int i=0;i<num;i++)
{
int s,j;
for(s=0;s<=i;s++)
{
printf(" ");
}
cout<<char(3);
for(j=0;j<g-1;j++)
{
printf(" ");
}
g-=2;
cout<<char(3);
printf("\n");
if(i==(num-1))
{
for(int f=0;f<(num+1);f++)
printf(" ");
cout<<char(3);
}
}
getch();
}


Saturday, May 9, 2015

Turbo c++ for android { Complete guide of installation }

Hello guys, today in this post I will explain how to download and install turbo c++ in android. I am sure you are aware about for turbo c++. Well turbo c++ is one type of compiler which is specially designed for c/c++ .One can easily perform most of the c/c++ programs with help of turbo c++! We already provided turbo c++ for windows xp/7/8/8.1! Well this post is especially for those who want to do programming in c or c++ in their android phones! So I will provide you guide How to install and use turbo c++ in your android phone! Just read full guide of turbo c++ for android!

Turbo c++ for android

Steps to install turbo c++ for android 


1.  First of all you need to download one zip in your android phone

  Click here to download the zip file for turbo c++ for android


2.  Now download one apk file Easy unrar from playstore! This apk file is needed for extracting the downloaded file of turbo c++ in your android phone! You can use other apks also for this operation

3.  Now open Easy unrar file and search the downloaded file (Given in step 1) and select it for extract it to the desired location

4. All the files will be there. You will find to files there .One is "Tc" folder and one is DOSbox apk.

5. You need to install DOSbox by clicking on it

6. After installing the DOSbox you need to move all extracted file in other location of SD card

7.  Now time to open DOSbox. Open DOSbox and click start emulator

8. You will see black screen like this.

9. Write following commands there

  cd tc
cd bin
tc

10. You will find the blue screen of turbo c++ on android

That’s it! You have successfully installed turbo c++ in your android phone!

Please note: This is tested by me and its working on any android device

So this is the complete process to install turbo c++ in android device! You can easily write the program there and run it on your android device. Enjoy programming on android device! Enjoy c/c++ programming! If you have still have any doubts or any difficulty in this post turbo c++ for  android device, Fill free to ask! I will help you!

Monday, March 9, 2015

Sunday, February 8, 2015

C Program to find the determinant of a Matrix

C Program to find the determinant of a Matrix  Size id Define By User

#include<conio.h>
#include<stdio.h>

int a[20][20],m;
int determinant(int f[20][20],int a);
int main()
{
  clrscr();
  int i,j;
  printf("----------------------------------------------------------------------\n");
  printf("-----------------CODE BY PROGRAMMING SEEKERZZ ------------------------\n");
  printf("----------------------------------------------------------------------\n");
  printf("\n\nEnter order of matrix : ");
  scanf("%d",&m);
  printf("\nEnter the elements of matrix\n");
  for(i=1;i<=m;i++)
  {
  for(j=1;j<=m;j++)
  {
  printf("a[%d][%d] = ",i,j);
  scanf("%d",&a[i][j]);
  }
  }
  printf("\n\n---------- Matrix A is --------------\n");
  for(i=1;i<=m;i++)
     {
 printf("\n");
 for(j=1;j<=m;j++)
 {
      printf("\t%d \t",a[i][j]);
 }
     }
  printf("\n \n");
  printf("\n Determinant of Matrix A is %d .",determinant(a,m));
  getch();
}

int determinant(int f[20][20],int x)
{
  int pr,c[20],d=0,b[20][20],j,p,q,t;
  if(x==2)
  {
    d=0;
    d=(f[1][1]*f[2][2])-(f[1][2]*f[2][1]);
    return(d);
   }
  else
  {
    for(j=1;j<=x;j++)
    {
      int r=1,s=1;
      for(p=1;p<=x;p++)
{
 for(q=1;q<=x;q++)
   {
     if(p!=1&&q!=j)
     {
b[r][s]=f[p][q];
s++;
if(s>x-1)
{
  r++;
  s=1;
 }
      }
    }
}
     for(t=1,pr=1;t<=(1+j);t++)
     pr=(-1)*pr;
     c[j]=pr*determinant(b,x-1);
     }
     for(j=1,d=0;j<=x;j++)
     {
       d=d+(f[1][j]*c[j]);
      }
     return(d);
   }

}


Sunday, January 4, 2015

Sound Function in C or C++

In Both C and C++:
--------------------------------------
#include<dos.h>
#include<iostream.h>
 void main()
{
   int a;
   for ( a = 200 ; a <= 1000 ; a = a + 20 )
   {
      sound(a);
      delay(25);
   }
   nosound();
}

------------------------------------------
You can also do like that in C++ :

#include<iostream.h>
#include<conio.h>
void main()
{
cout<<"\a --------- \a -------- \a";
getch();
}

--------------------------------------
You can also do like that in C Language :

#include<stdio.h>
#include<conio.h>
void main()
{
printf("\a --------- \a -------- \a");
getch();
}










Saturday, December 13, 2014

Shell Sort code in C++

 Other Type Of Sorting : Click here :: most important ::


#include<iostream.h>
#include<conio.h>
 void display(int *arr,int n)
{
 cout<<endl;
 for(int i=0;i<=n;i++){
 cout<<arr[i]<<"\t"; }
 cout<<endl;
}

void shellsort(int *arr,int n)
{
 int gap,i,j,temp;
 for(gap=n/2;gap>0;gap/=2)
 {
 for(i=gap;i<=n;i++)
 {
 for(j=i-gap;j>=0 && arr[j]>arr[j+gap];j-=gap)
 {
 display(arr,n);
 temp=arr[j];
 arr[j]=arr[j+gap];
 arr[j+gap]=temp;
 }
 }
 }
}

void main()
{
int x[]={12,7,3,8,1,5,2};
clrscr();
display(x,6);
shellsort(x,6);
display(x,6);
getch();
}

Saturday, September 20, 2014

Data Structure Programs

Read Also:  


"All Data Structure Programs up-till now"

To get code Click on that program heading :) ☺ ☻ 

 ALL Type Of Sorting Click here :: most important :: 

-------------------

`Queues 

----------------

`Single Link-List:

*  How to create single Link-list in C++

*  Insertion at any point in link-list in C++

*  Concatenate or Merge two Link-lists in C++

Deletion At any point in Circular Link List in C++
------------------------------------

`Double Link-list:

=================

Stacks Data Structure:


Decimal to binary conversion using stack in c++
============ 

How to add two matrix size is define by use






Program to print counting 10 to 1 using Recursion

--------------------------------------------------