facebook like button

21 January, 2014

this is a blinking christmas tree.

#include<stdio.h>
#include <time.h>
void sleep(time_t delay)
 {
  time_t timer0,timer1;
  time(&timer0);
  do{
  time(&timer1);
  }while((timer1-timer0)<delay);
 }
int main()
{

int i=5,j,k,s=0;

printf("your christmas tree is here\n");
for(k=0;k<5;k++)
printf(" ");
sleep(1);
printf("@@\n");
for(j=0;j<10;j++)
{
for(k=i;k<10;k++)
printf(" ");
i++;
if(i>10)
{
for(k=0;k<3;k++)
printf(" ");
}
sleep(1);
printf("*");
for(k=0;k<2*s;k++)
{if(2*s<10){
sleep(1);
printf("*");
}
}
if(2*s>=10)
{for(k=0;k<4;k++)
{sleep(1);
printf("*");}
}
sleep(1);
printf("*");
s++;

printf("\n");
}
return 0;
}

program that prints inorder traversal of tree and count the leaf nodes.

#include<stdio.h>
struct x{
int data;
struct x *right;
struct x *left;
};
typedef struct x node;
node* createTree(node * ,int );
node* createNode(int x);
void inorder(node** );
int countLeaf(node** );
int main()
{
int i,x,j,n;
node *root=NULL;
printf("how many nodes you want to create\n");
scanf("%d",&n);
if(n>0)
{
printf("enter the data for nodes\n");
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=createTree(root,x);
}
}
inorder(&root);
j=countLeaf(&root);
printf("\nthe leaf nodes are %d\n",j);
return 0;

}
node* createNode(int x)
{
node* a;
a=(node*)malloc(sizeof(node));
a->data=x;
a->right=NULL;
a->left=NULL;
return a;
}
node* createTree(node* root,int x)
{
if(root==NULL)
{
root=createNode(x);
return root;
}
else if(root->data<x)
root->right=createTree(root->right,x);
else
root->left=createTree(root->left,x);
return root;
}
void inorder(node **root)
{
if(*root==NULL)
return;
else{
inorder(&((*root)->left));
printf("%d->",(*root)->data);
inorder(&((*root)->right));
}
}
int countLeaf(node **root)
{
static int count=0;
if(*root==NULL)
return count;
  else if((*root)->left==NULL && (*root)->right==NULL)
{
count++;
return count;
}
else{
countLeaf(&((*root)->left));
countLeaf(&((*root)->right));
}
}

28 November, 2013

conversion of an infix expression to a prefix expression using stack.

THE CODE:
#include<stdio.h>
#include<ctype.h>
#define max 50
struct x{
char data[max];
int top;
};
typedef struct x stack;
void infix_prefix(char [],char []);
void infix_postfix(char [],char []);
void push(stack *,char );
char pop(stack *);
int precedence(char );
int main()
{
char infix[max],prefix[max];
printf("\nenter an infix expression\n");
gets(infix);
infix_prefix(infix,prefix);
printf("\n prefix:\n %s ",prefix);
return 0;
}
void infix_prefix(char infix[],char prefix[])
{
int i,j;
char a[max],temp;
for(j=strlen(infix)-1,i=0;j>=0;j--,i++)
a[i]=infix[j];
a[i]='\0';
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='(')
a[i]=')';
else if(a[i]==')')
a[i]='(';
}
infix_postfix(a,prefix);
for(i=0,j=strlen(prefix)-1;i<j;i++,j--)
{
temp=prefix[j];
prefix[j]=prefix[i];
prefix[i]=temp;
}
}


void infix_postfix(char infix[],char postfix[])
{
int i,j;
stack s;
char x,token;
s.top=-1;
j=0;
for(i=0;infix[i]!='\0';i++)
{
token=infix[i];
if(isalnum(token)){
postfix[j]=token;
j++;
}
else if(token=='(')
push(&s,'(');
else if(token==')')
{
while((x=pop(&s))!='('){
postfix[j]=x;
j++;
}
}
else
{
while( s.top!=-1 && precedence(token)<=precedence(s.data[s.top]))
{
x=pop(&s);
postfix[j++]=x;
}
push(&s,token);
}

}
while(s.top!=-1)
{
x=pop(&s);
postfix[j]=x;
j++;
}
postfix[j]='\0';
}
int precedence(char x)
{
if(x=='(')
return 0;
else if(x=='+'||x=='-')
return 1;
else if(x=='*'||x=='/'||x=='%')
return 2;
else
return 3;
}
void push(stack *s,char x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
char pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return x;
}

24 October, 2013

sorting using quicksort method(divide and conqour method ).

THE NEED:
quicksort is the fastest sort in best average case of complexity O(n logn) in worst average case it is O(n2).
the method is to choose pivot, which in my program is the index such that all number in the left of array[pivot],should be less than array[pivot]. And all number in right should be greater than array[pivot].
Rest will be done by recursion.

CODE:
#include<stdio.h>
#define max 10
void quicksort(int a[],int ,int );
int choosepivot(int a[],int ,int );
int main()
{
int a[max],first=0,last,i,n;
printf("enter lenght of array you want to sort\n");
scanf("%d",&last);
printf("enter the array\n");
for(i=0;i<last;i++){
scanf("%d",&a[i]);
}
quicksort(a,first,last-1);
for(i=0;i<last;i++)
{
printf("%d ",a[i]);
}
}
void quicksort(int a[max],int first,int last)
{
int pivot;
if(first<last)
{
pivot=choosepivot(a,first,last);
quicksort(a,first,pivot-1);
quicksort(a,pivot+1,last);
}
}
int choosepivot(int a[max],int first,int last)
{
int j,temp;
int pivot=first;
j=last;
while(j>pivot)
{
if(a[pivot]<a[pivot+1]){
pivot++;
}
else if(a[j]<a[pivot]){
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
}
else
j--;
}
return pivot;
}





APPROACH:
the function quicksort() sorts the array by dividing them on the basis of index(pivot) that's why this is called as divide and conquer approach.
the function choose pivot ,takes the pivot as first element of the array that is being called.And then check  weather the next to it is greater than the number in a[pivot],if not pivot(index) will move to the next number else it will rotate the next number with the last and moves pivot to the next index.
at last the function returns the pivot .

05 September, 2013

Vector & Matrix Algebra


#include<iostream.h>  
#include<conio.h>
#include <cstdlib>
#include <math.h>  
class vector  
 {   
 public:  
 int x,y,z;  
 void read()  
 {  
 cout<<"\n\nEnter the magnitude of i : ";  
 cin>>x;  
 cout<<"\n\nEnter the magnitude of j : ";  
 cin>>y;  
 cout<<"\n\nEnter the magnitude of k : ";  
 cin>>z;  
 }  
 vector operator -()  
 {  
 x=-x;  
 y=-y;  
 z=-z;  
 }  
 vector operator +(vector b)  
 {  
 vector c;  
 c.x=x+b.x;  
 c.y=y+b.y;  
 c.z=z+b.z;  
 return c;  
 }  
 vector operator *(vector b)  
 {  
 int prod;  
 vector c;  
 c.x=x*b.x;  
 c.y=y*b.y;  
 c.z=z*b.z;  
 prod=c.x+c.y+c.z;
 cout<<"\nInner Product = ";  
 cout<<prod;
 vector d;  
 d.x=(y*b.z)-(z*b.y);  
 d.y=(x*b.z)-(z*b.x);  
 d.z=(x*b.y)-(y*b.x);  
 return d;  
 }  
 void display()  
 {  
 cout<<x<<"i"<<"+"<<y<<"j"<<"+"<<z<<"k";  
 }  
 }; 

using namespace std;

void vecmat_mult()
{
    int mat[10][10];
    int vec[3];
    int out[10];
    int c,d,m,n,i,j;
cout<<"Enter the number of rows and columns of matrix"<<endl;
  cin>>m;
  cin>>n;
  cout<<"Enter the elements of matrix"<<endl;
  for ( c = 0 ; c < m ; c++ )
    {
 for ( d = 0 ; d < n ; d++ )
  {
 cout<<"Enter elements mat"<<c+1<<d+1<<" ";
 cin>>mat[c][d];
  }
    }

    cout<<"Enter Vector Component"<<endl;
    
  for(i=0;i<3;i++)
{
cin>>vec[i];
}

 
    for (i = 0; i < m; i++ )
{
out[i] = 0;
      for ( int j = 0; j < n; j++ )
  {
 out[i] += mat[i][j] * vec[j];
  }
      }
cout<<"Result Product is\n\n";
    for (i=0;i<m;i++)
{
cout<<out[i]<<"  "<<endl;
    }
}

void matrixmult()
{
    int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;
    cout<<"Enter number of rows and columns of first matrix\n";
    cin>>r1>>c1;
    cout<<"Enter number of rows and columns of second matrix\n";
    cin>>r2>>c2;
    if(r2==c1)
    {
        cout<<"Enter rows and columns of First matrix \n";
        for(i=0;i<r1;i++)
            for(j=0;j<c1;j++)
                cin>>m1[i][j];
        cout<<"First Matrix is :\n";
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                cout<<m1[i][j]<<"\t";
            cout<<"\n";
        }
        cout<<"Enter rows and columns of Second matrix \n";
        for(i=0;i<r2;i++)
            for(j=0;j<c2;j++)
                cin>>m2[i][j];
        cout<<"Second Matrix is:\n";
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                cout<<m2[i][j]<<"\t";
            cout<<"\n";
        }
        cout<<"Multiplication of the Matrices:\n";
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<r1;k++)
                    mult[i][j]+=m1[i][k]*m2[k][j];
                cout<<mult[i][j]<<"\t";
            }
            cout<<"\n";
        }
    }
    else
    {
        cout<<"Matrix multiplication cannot be done";
    }
}
void matrixtranspose()
{
int a[10][10], trans[10][10], r, c, i, j;
cout<<"Enter rows and column of matrix: "<<endl;
cin>>r>>c;
cout<<"\nEnter elements of matrix:\n";
for(i=0; i<r; ++i)
{
 for(j=0; j<c; ++j)
  {
 cout<<"Enter elements a"<<i+1<<j+1<<" ";
 cin>>a[i][j]; 
}
}
 /*Displaying the matrix a[][] */
 cout<<"\nEntered Matrix: \n"; 
 for(i=0;i<r; ++i)
  {
 for(j=0; j<c; ++j)
     
 cout<<a[i][j]<<"\t";
 if(j==c-1)
 cout<<"\n\n"; 
    }
  }
 
/* Finding transpose of matrix a[]
[] and storing it in array trans[][]. */ 

for(i=0; i<r; ++i)
{
 for(j=0;j<c; ++j)
  { 
    trans[j][i]=a[i][j];
       }
    }
/* Displaying the transpose,i.e, Displaying array trans[][].*/ 

cout<<"\nTranspose of Matrix:\n"; 
for(i=0; i<c; ++i)
{
 for(j=0; j<r; ++j)
  {
 printf("%d ",trans[i][j]); if(j==r-1) printf("\n\n");
  }
}
}

int main()  
 {  
 vector v1,v2,v3;  
 int choice,cont;  
 do  
 {  
 cout<<"\n\tVECTORS & MaTRIX Algebra\n\n1.Vector Addition\n\n2.Vector Product\n\n3.Product Matrix-Vector\n\n4.Product Matrix-Matrix\n\n5.Transpose Matrix\n\n6.Exit";  
 cout<<"\n\nEnter your choice : ";  
 cin>>choice;  
 switch(choice)  
 {  

 case 1 : cout<<"\n\nEnter the First Vector : ";  
            v1.read();  
            cout<<"\n\nEnter the Second Vector : ";  
            v2.read();  
            v3=v1+v2;  
            cout<<"\n\nThe Sum of Vectors : ";  
            v3.display();  
            break;  
 case 2 : cout<<"\n\nEnter the First Vector : ";  
            v1.read();  
            cout<<"\n\nEnter the Second Vector : ";  
            v2.read();  
            v3=v1*v2;  
            cout<<"\n\nCross Product = ";  
            v3.display();
            break;  
 case 3 : vecmat_mult();
   break; 

 case 4 : matrixmult();
     break;

 case 5 : matrixtranspose();
     break;

 case 6 : exit(0);
 default : cout<<"\n\nInvalid Choice";  
 }  
 cout<<"\n\n\nDo you want to continue?(1-YES,0-NO)\n";  
 cin>>cont;  
 }while(cont==1);  
 return 0;
 }