Get Latest Greetings,Scraps
Thursday, December 30, 2010
<<<<<<<<<......Happy New Year.......>>>>>>>>>>
A very HAPPY NEW YEAR to all the visitors.
Its been a pleasure for me working in C.
I am very happy to see the visitors from different countries all over the world.
I am very optimistic towards this and hope more people follow my blog and get benefitted..........
Once again ..............
Have a Prosperous NEW YEAR 2011.
New Year Spl: "GUESS it RIGHT" - The Game
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
/* This program plays simple guessing game . The computer picks a
random number from 0 to 100 , and the user tries to guess the number */
const int totchan = 7;
void main()
{
int number ; // computers rabdom number
int guess; // the users guess number
int chances=0,score=0,chanscor;// chanscor stores score for 1 successful chance
char ans;
do
{
clrscr();
chances=score=0;
printf("\n\t\t\t\t Welcome to the HIGH/LOW GAME ");
printf("\n\t\t\t\t I will pick a random number from 0 to 100 ");
printf("\n\t\t\t\t You must try to guess the number ");
randomize();
number =(int)(rand()%100);
chanscor=100/totchan;
do
{
printf("\n\t What is your GUESS ?? ");
scanf("%d",&guess);
if((guess<0) || (guess>100))
{
printf(" Sorry but your guess %d must be from 0 to 100 ");
}
else if (guess < number )
{
printf(" %d is LOW . Try a higher number ",guess);
}
else if(guess>number)
{
printf("%d is HIGH . Try a lower number ",guess);
}
else
{
printf("%d is correct . Congratulations !!!! ");
score = chanscor*(totchan-chances);
printf("\n\t Your score is %d \n",score);
break;
}
chances++;
if(guess!=number)
printf("\n\n Now you have %d chances left \n",totchan-chances);
if(chances == totchan)
{
printf("\n\n Only %d chances are allowed . Better luck next time",totchan);
printf("\n The actual number was %d \n",number);
break;
}
}while(guess!=number);
printf("\n\n Thank You for playing this game !!!!!!!!! ");
printf("\n\n Want to play it again ?(y/n)..");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y' || ans=='Y');
}
#include<conio.h>
#include<stdlib.h>
/* This program plays simple guessing game . The computer picks a
random number from 0 to 100 , and the user tries to guess the number */
const int totchan = 7;
void main()
{
int number ; // computers rabdom number
int guess; // the users guess number
int chances=0,score=0,chanscor;// chanscor stores score for 1 successful chance
char ans;
do
{
clrscr();
chances=score=0;
printf("\n\t\t\t\t Welcome to the HIGH/LOW GAME ");
printf("\n\t\t\t\t I will pick a random number from 0 to 100 ");
printf("\n\t\t\t\t You must try to guess the number ");
randomize();
number =(int)(rand()%100);
chanscor=100/totchan;
do
{
printf("\n\t What is your GUESS ?? ");
scanf("%d",&guess);
if((guess<0) || (guess>100))
{
printf(" Sorry but your guess %d must be from 0 to 100 ");
}
else if (guess < number )
{
printf(" %d is LOW . Try a higher number ",guess);
}
else if(guess>number)
{
printf("%d is HIGH . Try a lower number ",guess);
}
else
{
printf("%d is correct . Congratulations !!!! ");
score = chanscor*(totchan-chances);
printf("\n\t Your score is %d \n",score);
break;
}
chances++;
if(guess!=number)
printf("\n\n Now you have %d chances left \n",totchan-chances);
if(chances == totchan)
{
printf("\n\n Only %d chances are allowed . Better luck next time",totchan);
printf("\n The actual number was %d \n",number);
break;
}
}while(guess!=number);
printf("\n\n Thank You for playing this game !!!!!!!!! ");
printf("\n\n Want to play it again ?(y/n)..");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y' || ans=='Y');
}
New Year Spl: The "CROSS & NOT ' Game
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
char matrix[3][3];
void cou(void);
void cou(void)
{
printf("\n\t\t 1 2 3 \n\n");
printf("\t\t 1 %c | %c | %c \n",matrix[0][0],matrix[0][1],matrix[0][2]);
printf("\t\t ---|---|---\n");
printf("\t\t 2 %c | %c | %c \n",matrix[1][0],matrix[1][1],matrix[1][2]);
printf("\t\t ---|---|---\n");
printf("\t\t 3 %c | %c | %c \n",matrix[2][0],matrix[2][1],matrix[2][2]);
}
void main()
{
int m,n;
int i,j,sum=0;
char ch='y';
while(ch=='Y' || ch=='y')
{
for(m=0;m<3;m++)
for(n=0;n<3;n++)
matrix[m][n]='\0';
while(sum<10)
{
if(sum==0)
cou();
printf(" Player1 is 'X' : choose the row and column \n");
printf(" Row : ");
scanf("%d",&i);
printf(" Column :L ");
scanf("%d",&j);
for(;i>3||i<1||j>3||j<1||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);)
{
printf(" Sorry but you gotta choose another place .\n");
printf(" ROW :");
scanf("%d",&i);
printf(" COLUMN : ");
scanf("%d",&j);
}
matrix[i-1][j-1]='X';
sum++;
cou(); // calling function to display game setup
//check if player 1 wins
if(matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(sum==9) // because there are only 9 boxes
{
printf(" The game is over and no one wins , haha lols \n");
break;
}
//player 2 's turn
printf(" Pkayer2 is 'O' : choose the row and column \n");
printf(" Row ");
scanf("%d",&i);
printf(" Column : ");
scanf("%d",&j);
for(;i>3||i<1||j>3||j<1||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);)
{
printf(" Sorry but you gotta choose another place .\n");
printf(" ROW :");
scanf("%d",&i);
printf(" COLUMN : ");
scanf("%d",&j);
}
matrix[i-1][j-1]='O';
sum++;
cou(); // calling function to display game setup
//check if player 1 wins
if(matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
printf(" \n Would you like to play again ??? (Y-N)\n");
scanf("%c",&ch);
}
system("PAUSE");
}
}
#include<stdio.h>
#include<stdlib.h>
char matrix[3][3];
void cou(void);
void cou(void)
{
printf("\n\t\t 1 2 3 \n\n");
printf("\t\t 1 %c | %c | %c \n",matrix[0][0],matrix[0][1],matrix[0][2]);
printf("\t\t ---|---|---\n");
printf("\t\t 2 %c | %c | %c \n",matrix[1][0],matrix[1][1],matrix[1][2]);
printf("\t\t ---|---|---\n");
printf("\t\t 3 %c | %c | %c \n",matrix[2][0],matrix[2][1],matrix[2][2]);
}
void main()
{
int m,n;
int i,j,sum=0;
char ch='y';
while(ch=='Y' || ch=='y')
{
for(m=0;m<3;m++)
for(n=0;n<3;n++)
matrix[m][n]='\0';
while(sum<10)
{
if(sum==0)
cou();
printf(" Player1 is 'X' : choose the row and column \n");
printf(" Row : ");
scanf("%d",&i);
printf(" Column :L ");
scanf("%d",&j);
for(;i>3||i<1||j>3||j<1||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);)
{
printf(" Sorry but you gotta choose another place .\n");
printf(" ROW :");
scanf("%d",&i);
printf(" COLUMN : ");
scanf("%d",&j);
}
matrix[i-1][j-1]='X';
sum++;
cou(); // calling function to display game setup
//check if player 1 wins
if(matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(sum==9) // because there are only 9 boxes
{
printf(" The game is over and no one wins , haha lols \n");
break;
}
//player 2 's turn
printf(" Pkayer2 is 'O' : choose the row and column \n");
printf(" Row ");
scanf("%d",&i);
printf(" Column : ");
scanf("%d",&j);
for(;i>3||i<1||j>3||j<1||('X'==matrix[i-1][j-1]||'O'==matrix[i-1][j-1]);)
{
printf(" Sorry but you gotta choose another place .\n");
printf(" ROW :");
scanf("%d",&i);
printf(" COLUMN : ");
scanf("%d",&j);
}
matrix[i-1][j-1]='O';
sum++;
cou(); // calling function to display game setup
//check if player 1 wins
if(matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2])
{
printf(" Player 1 wins \n");
break;
}
if(matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2])
{
printf(" Player 1 wins \n");
break;
}
printf(" \n Would you like to play again ??? (Y-N)\n");
scanf("%c",&ch);
}
system("PAUSE");
}
}
C Program to insert and delete elements in an AVL Tree
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define FALSE 0
#define TRUE 1
struct AVLNode
{
int data;
int balfact;
struct AVLNode *left;
struct AVLNode *right;
};
struct AVLNode * insert(struct AVLNode *,int,int *);
struct AVLNode * deldata(struct AVLNode *,int,int *);
struct AVLNode * del(struct AVLNode *,struct AVLNode *,int *);
struct AVLNode * balr(struct AVLNode *,int *);
struct AVLNode * ball(struct AVLNode *,int *);
void display(struct AVLNode *);
void deltree(struct AVLNode *);
void main()
{
struct AVLNode *avl=NULL;
int h;
clrscr();
avl=insert(avl,20,&h);
avl=insert(avl,6,&h);
avl=insert(avl,29,&h);
avl=insert(avl,5,&h);
avl=insert(avl,12,&h);
avl=insert(avl,25,&h);
avl=insert(avl,32,&h);
avl=insert(avl,10,&h);
avl=insert(avl,15,&h);
avl=insert(avl,27,&h);
// avl=insert(avl,13,&h);
printf("\n AVL Tree : \n");
display(avl);
avl=deldata(avl,5,&h);
avl=deldata(avl,12,&h);
printf("\n AVL tree after deletion of a node : \n");
display(avl);
deltree(avl);
getch();
}
/* Inserts an element intp tree */
struct AVLNode * insert(struct AVLNode *root,int data,int *h)
{
struct AVLNode *node1,*node2;
if(!root)
{
root=(struct AVLNode *)malloc(sizeof(struct AVLNode));
root->data=data;
root->left=NULL;
root->right=NULL;
root->balfact=0;
*h=TRUE;
return(root);
}
if(data < root->data)
{
root->left=insert(root->left,data,h);
/* If left subtree is higher */
if(*h)
{
switch(root->balfact)
{
case 1:
node1=root->left;
if(node1->balfact==1)
{
printf("\n Right Rotation alond %d. ",root->data);
root->left=node1->right;
node1->right=root;
root->balfact=0;
root=node1;
}
else
{
printf("\n Double rotation , left along %d",node1->data);
node2=node1->right;
node1->right=node2->left;
printf(" then right along %d. \n",root->data);
node2->left=node1;
root->left=node2->right;
node2->right=root;
if(node2->balfact==1)
root->balfact=-1;
else
root->balfact=0;
if(node2->balfact==-1)
node1->balfact=1;
else
node1->balfact=0;
root=node2;
}
root->balfact=0;
*h=FALSE;
break;
case 0:
root->balfact=1;
break;
case -1:
root->balfact=0;
*h=FALSE;
}
}
}
if(data > root->data)
{
root->right=insert(root->right,data,h);
/* If the right subtree is higher */
if(*h)
{
switch(root->balfact)
{
case 1:
root->balfact=0;
*h=FALSE;
break;
case 0:
root->balfact=-1;
break;
case -1:
node1=root->right;
if(node1->balfact==-1)
{
printf("\n Left rotation along %d. ",root->data);
root->right=node1->left;
node1->left=root;
root->balfact=0;
root=node1;
}
else
{
printf("\n Double rotation , right along %d",node1->data);
node2=node1->left;
node1->left=node2->right;
node2->right=node1;
printf(" then left along %d. \n",root->data);
root->right=node2->left;
node2->left=root;
if(node2->balfact==-1)
root->balfact=1;
else
root->balfact=0;
if(node2->balfact==1)
node1->balfact=-1;
else
node1->balfact=0;
root=node2;
}
root->balfact=0;
*h=FALSE;
}
}
}
return(root);
}
/* Deletes an item from the tree */
struct AVLNode * deldata(struct AVLNode *root,int data,int *h)
{
struct AVLNode *node;
if(!root)
{
printf("\n No such data. ");
return (root);
}
else
{
if(data < root->data)
{
root->left=deldata(root->left,data,h);
if(*h)
root=balr(root,h);
}
else
{
if(data > root->data)
{
root->right=deldata(root->right,data,h);
if(*h)
root=ball(root,h);
}
else
{
node=root;
if(node->right==NULL)
{
root=node->left;
*h=TRUE;
free(node);
}
else
{
node->right=del(node->right,node,h);
if(*h)
root=ball(root,h);
}
}
}
}
return(root);
}
struct AVLNode * del(struct AVLNode *succ,struct AVLNode *node,int *h)
{
struct AVLNode *temp=succ;
if(succ->left!=NULL)
{
succ->left=del(succ->left,node,h);
if(*h)
succ=balr(succ,h);
}
else
{
temp=succ;
node->data=succ->data;
succ=succ->right;
free(temp);
*h=TRUE;
}
return(succ);
}
/* Balance the tree , if right subtree is higher */
struct AVLNode * balr(struct AVLNode *root,int *h)
{
struct AVLNode *node1,*node2;
switch(root->balfact)
{
case 1:
root->balfact=0;
break;
case 0:
root->balfact=-1;
*h=FALSE;
break;
case -1:
node1=root->right;
if(node1->balfact <= 0)
{
printf("\n Left rotation along %d. ",root->data);
root->right=node1->left;
node1->left=root;
if(node1->balfact==0)
{
root->balfact=-1;
node1->balfact=1;
*h=FALSE;
}
else
{
root->balfact=node1->balfact=0;
}
root=node1;
}
else
{
printf("\n Double rotation , right along %d ",node1->data);
node2=node1->left;
node1->left=node2->right;
node2->right=node1;
printf(" then left along %d. \n",root->data);
root->right=node2->left;
node2->left=root;
if(node2->balfact==-1)
root->balfact=1;
else
root->balfact=0;
if(node2->balfact==1)
node1->balfact=-1;
else
node1->balfact=0;
root=node2;
node2->balfact=0;
}
}
return (root);
}
/* Balances the tree , if the left subtree is higher */
struct AVLNode * ball(struct AVLNode * root,int *h)
{
struct AVLNode *node1,*node2;
switch(root->balfact)
{
case -1:
root->balfact=0;
break;
case 0:
root->balfact=1;
*h=FALSE;
break;
case 1:
node1=root->left;
if(node1->balfact >= 0)
{
printf("]n Right rotation along %d. ",root->data);
root->left=node1->right;
node1->right=root;
if(node1->balfact==0)
{
root->balfact=1;
node1->balfact=-1;
*h=FALSE;
}
else
{
root->balfact=node1->balfact=0;
}
root=node1;
}
else
{
printf("\n Double rotation , left along %d ",node1->data);
node2=node1->right;
node1->right=node2->left;
node2->left=node1;
printf(" then right along %d .\n",root->data);
root->left=node2->right;
node2->right=root;
if(node2->balfact==1)
root->balfact=-1;
else
root->balfact=0;
if(node2->balfact==-1)
node1->balfact=1;
else
node1->balfact=0;
root=node2;
node2->balfact=0;
}
}
return (root);
}
/*n Displays the tree in-order */
void display(struct AVLNode *root)
{
if(root!=NULL)
{
display(root->left);
printf("%d\t",root->data);
display(root->right);
}
}
/* Deletes the tree */
void deltree(struct AVLNode * root)
{
if(root!=NULL)
{
deltree(root->left);
deltree(root->right);
}
free(root);
}
#include<conio.h>
#include<alloc.h>
#define FALSE 0
#define TRUE 1
struct AVLNode
{
int data;
int balfact;
struct AVLNode *left;
struct AVLNode *right;
};
struct AVLNode * insert(struct AVLNode *,int,int *);
struct AVLNode * deldata(struct AVLNode *,int,int *);
struct AVLNode * del(struct AVLNode *,struct AVLNode *,int *);
struct AVLNode * balr(struct AVLNode *,int *);
struct AVLNode * ball(struct AVLNode *,int *);
void display(struct AVLNode *);
void deltree(struct AVLNode *);
void main()
{
struct AVLNode *avl=NULL;
int h;
clrscr();
avl=insert(avl,20,&h);
avl=insert(avl,6,&h);
avl=insert(avl,29,&h);
avl=insert(avl,5,&h);
avl=insert(avl,12,&h);
avl=insert(avl,25,&h);
avl=insert(avl,32,&h);
avl=insert(avl,10,&h);
avl=insert(avl,15,&h);
avl=insert(avl,27,&h);
// avl=insert(avl,13,&h);
printf("\n AVL Tree : \n");
display(avl);
avl=deldata(avl,5,&h);
avl=deldata(avl,12,&h);
printf("\n AVL tree after deletion of a node : \n");
display(avl);
deltree(avl);
getch();
}
/* Inserts an element intp tree */
struct AVLNode * insert(struct AVLNode *root,int data,int *h)
{
struct AVLNode *node1,*node2;
if(!root)
{
root=(struct AVLNode *)malloc(sizeof(struct AVLNode));
root->data=data;
root->left=NULL;
root->right=NULL;
root->balfact=0;
*h=TRUE;
return(root);
}
if(data < root->data)
{
root->left=insert(root->left,data,h);
/* If left subtree is higher */
if(*h)
{
switch(root->balfact)
{
case 1:
node1=root->left;
if(node1->balfact==1)
{
printf("\n Right Rotation alond %d. ",root->data);
root->left=node1->right;
node1->right=root;
root->balfact=0;
root=node1;
}
else
{
printf("\n Double rotation , left along %d",node1->data);
node2=node1->right;
node1->right=node2->left;
printf(" then right along %d. \n",root->data);
node2->left=node1;
root->left=node2->right;
node2->right=root;
if(node2->balfact==1)
root->balfact=-1;
else
root->balfact=0;
if(node2->balfact==-1)
node1->balfact=1;
else
node1->balfact=0;
root=node2;
}
root->balfact=0;
*h=FALSE;
break;
case 0:
root->balfact=1;
break;
case -1:
root->balfact=0;
*h=FALSE;
}
}
}
if(data > root->data)
{
root->right=insert(root->right,data,h);
/* If the right subtree is higher */
if(*h)
{
switch(root->balfact)
{
case 1:
root->balfact=0;
*h=FALSE;
break;
case 0:
root->balfact=-1;
break;
case -1:
node1=root->right;
if(node1->balfact==-1)
{
printf("\n Left rotation along %d. ",root->data);
root->right=node1->left;
node1->left=root;
root->balfact=0;
root=node1;
}
else
{
printf("\n Double rotation , right along %d",node1->data);
node2=node1->left;
node1->left=node2->right;
node2->right=node1;
printf(" then left along %d. \n",root->data);
root->right=node2->left;
node2->left=root;
if(node2->balfact==-1)
root->balfact=1;
else
root->balfact=0;
if(node2->balfact==1)
node1->balfact=-1;
else
node1->balfact=0;
root=node2;
}
root->balfact=0;
*h=FALSE;
}
}
}
return(root);
}
/* Deletes an item from the tree */
struct AVLNode * deldata(struct AVLNode *root,int data,int *h)
{
struct AVLNode *node;
if(!root)
{
printf("\n No such data. ");
return (root);
}
else
{
if(data < root->data)
{
root->left=deldata(root->left,data,h);
if(*h)
root=balr(root,h);
}
else
{
if(data > root->data)
{
root->right=deldata(root->right,data,h);
if(*h)
root=ball(root,h);
}
else
{
node=root;
if(node->right==NULL)
{
root=node->left;
*h=TRUE;
free(node);
}
else
{
node->right=del(node->right,node,h);
if(*h)
root=ball(root,h);
}
}
}
}
return(root);
}
struct AVLNode * del(struct AVLNode *succ,struct AVLNode *node,int *h)
{
struct AVLNode *temp=succ;
if(succ->left!=NULL)
{
succ->left=del(succ->left,node,h);
if(*h)
succ=balr(succ,h);
}
else
{
temp=succ;
node->data=succ->data;
succ=succ->right;
free(temp);
*h=TRUE;
}
return(succ);
}
/* Balance the tree , if right subtree is higher */
struct AVLNode * balr(struct AVLNode *root,int *h)
{
struct AVLNode *node1,*node2;
switch(root->balfact)
{
case 1:
root->balfact=0;
break;
case 0:
root->balfact=-1;
*h=FALSE;
break;
case -1:
node1=root->right;
if(node1->balfact <= 0)
{
printf("\n Left rotation along %d. ",root->data);
root->right=node1->left;
node1->left=root;
if(node1->balfact==0)
{
root->balfact=-1;
node1->balfact=1;
*h=FALSE;
}
else
{
root->balfact=node1->balfact=0;
}
root=node1;
}
else
{
printf("\n Double rotation , right along %d ",node1->data);
node2=node1->left;
node1->left=node2->right;
node2->right=node1;
printf(" then left along %d. \n",root->data);
root->right=node2->left;
node2->left=root;
if(node2->balfact==-1)
root->balfact=1;
else
root->balfact=0;
if(node2->balfact==1)
node1->balfact=-1;
else
node1->balfact=0;
root=node2;
node2->balfact=0;
}
}
return (root);
}
/* Balances the tree , if the left subtree is higher */
struct AVLNode * ball(struct AVLNode * root,int *h)
{
struct AVLNode *node1,*node2;
switch(root->balfact)
{
case -1:
root->balfact=0;
break;
case 0:
root->balfact=1;
*h=FALSE;
break;
case 1:
node1=root->left;
if(node1->balfact >= 0)
{
printf("]n Right rotation along %d. ",root->data);
root->left=node1->right;
node1->right=root;
if(node1->balfact==0)
{
root->balfact=1;
node1->balfact=-1;
*h=FALSE;
}
else
{
root->balfact=node1->balfact=0;
}
root=node1;
}
else
{
printf("\n Double rotation , left along %d ",node1->data);
node2=node1->right;
node1->right=node2->left;
node2->left=node1;
printf(" then right along %d .\n",root->data);
root->left=node2->right;
node2->right=root;
if(node2->balfact==1)
root->balfact=-1;
else
root->balfact=0;
if(node2->balfact==-1)
node1->balfact=1;
else
node1->balfact=0;
root=node2;
node2->balfact=0;
}
}
return (root);
}
/*n Displays the tree in-order */
void display(struct AVLNode *root)
{
if(root!=NULL)
{
display(root->left);
printf("%d\t",root->data);
display(root->right);
}
}
/* Deletes the tree */
void deltree(struct AVLNode * root)
{
if(root!=NULL)
{
deltree(root->left);
deltree(root->right);
}
free(root);
}
Friday, November 12, 2010
C Program to Count chars,spaces,tabs and newlines in a file
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int nol=0,not=0,nos=0,noc=0;
clrscr();
fp=fopen("pr1.c","r");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
noc++;
if(ch==' ')
nos++;
if(ch=='\n')
nol++;
if(ch=='\t')
not++;
}
fclose(fp);
printf("\n Number of characters = %d",noc);
printf("\n Number of blanks = %d",nos);
printf("\n Number of tabs = %d",not);
printf("\n Number of lines = %d",nol);
getch();
}
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int nol=0,not=0,nos=0,noc=0;
clrscr();
fp=fopen("pr1.c","r");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
noc++;
if(ch==' ')
nos++;
if(ch=='\n')
nol++;
if(ch=='\t')
not++;
}
fclose(fp);
printf("\n Number of characters = %d",noc);
printf("\n Number of blanks = %d",nos);
printf("\n Number of tabs = %d",not);
printf("\n Number of lines = %d",nol);
getch();
}
C Progarm to input numbers into a file and separate the odd and even numbers into two different files
/*
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int number,i;
clrscr();
printf(" \nContents of DATA file \n\n");
f1=fopen("DATA","w");
for(i=1;i<=30;i++)
{
scanf("%d",&number);
if(number==-1)
break;
putw(number,f1);
}
fclose(f1);
f1=fopen("DATA","r");
f3=fopen("ODD","w");
f2=fopen("EVEN","w");
/* Read from DATA file */
while((number=getw(f1))!=EOF)
{
if(number%2==0)
putw(number,f2);
else
putw(number,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD","r");
f3=fopen("EVEN","r");
printf("\n\n Contents of ODD FILE \n\n");
while((number=getw(f2))!=EOF)
printf("%4d",number);
printf("\n\n Contents of EVEN FILE \n\n");
while((number=getw(f3))!=EOF)
printf("%4d",number);
fclose(f2);
fclose(f3);
getch();
}
// Use -1 to stop inserting numbers.................................................
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int number,i;
clrscr();
printf(" \nContents of DATA file \n\n");
f1=fopen("DATA","w");
for(i=1;i<=30;i++)
{
scanf("%d",&number);
if(number==-1)
break;
putw(number,f1);
}
fclose(f1);
f1=fopen("DATA","r");
f3=fopen("ODD","w");
f2=fopen("EVEN","w");
/* Read from DATA file */
while((number=getw(f1))!=EOF)
{
if(number%2==0)
putw(number,f2);
else
putw(number,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD","r");
f3=fopen("EVEN","r");
printf("\n\n Contents of ODD FILE \n\n");
while((number=getw(f2))!=EOF)
printf("%4d",number);
printf("\n\n Contents of EVEN FILE \n\n");
while((number=getw(f3))!=EOF)
printf("%4d",number);
fclose(f2);
fclose(f3);
getch();
}
// Use -1 to stop inserting numbers.................................................
Thursday, November 11, 2010
C Program to demonstrate Heap Sort
#include<stdio.h>
#include<conio.h>
void creatheap(int [],int);
void sort(int[],int);
void main()
{
int xarr[10]={11,2,9,13,57,25,17,1,90,3};
int i;
printf(" \n Heap Sort ");
creatheap(xarr,10);
printf("\n Before Sorting \n");
for(i=0;i<=9;i++)
printf("%d\t",xarr[i]);
sort(xarr,10);
printf("\n Array Sorting :\n");
for(i=0;i<=9;i++)
printf("%d\t",xarr[i]);
getch();
}
void creatheap(int x[],int n)
{
int i,val,s,f;
for(i=1;i<n;i++)
{
val=x[i];
s=i;
f=(s-1)/2;
while(s>0 && x[f] < val)
{
x[s]=x[f];
s=f;
f=(s-1)/2;
}
x[s]=val;
}
}
void sort(int x[],int n)
{
int i,s,f,ivalue;
for(i=n-1;i>=1;i--)
{
ivalue=x[i];
x[i]=x[0];
f=0;
if(i==1)
s=-1;
else
s=1;
if(i>2 && x[2] > x[1])
s=2;
while(s >= 0 && ivalue<x[s])
{
x[f]=x[s];
f=s;
s=2*f +1;
if((s+1)<=(i-1) && x[s]<x[s+1])
s++;
if(s>i-1)
s=-1;
}
x[f]=ivalue;
}
}
#include<conio.h>
void creatheap(int [],int);
void sort(int[],int);
void main()
{
int xarr[10]={11,2,9,13,57,25,17,1,90,3};
int i;
printf(" \n Heap Sort ");
creatheap(xarr,10);
printf("\n Before Sorting \n");
for(i=0;i<=9;i++)
printf("%d\t",xarr[i]);
sort(xarr,10);
printf("\n Array Sorting :\n");
for(i=0;i<=9;i++)
printf("%d\t",xarr[i]);
getch();
}
void creatheap(int x[],int n)
{
int i,val,s,f;
for(i=1;i<n;i++)
{
val=x[i];
s=i;
f=(s-1)/2;
while(s>0 && x[f] < val)
{
x[s]=x[f];
s=f;
f=(s-1)/2;
}
x[s]=val;
}
}
void sort(int x[],int n)
{
int i,s,f,ivalue;
for(i=n-1;i>=1;i--)
{
ivalue=x[i];
x[i]=x[0];
f=0;
if(i==1)
s=-1;
else
s=1;
if(i>2 && x[2] > x[1])
s=2;
while(s >= 0 && ivalue<x[s])
{
x[f]=x[s];
f=s;
s=2*f +1;
if((s+1)<=(i-1) && x[s]<x[s+1])
s++;
if(s>i-1)
s=-1;
}
x[f]=ivalue;
}
}
C Program to demonstrate BinarY Tree Sort
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct BinaryTree
{
struct BinaryTree *LeftChild;
int data;
struct BinaryTree *RightChild;
};
void insert(struct BinaryTree **,int);
void inorder(struct BinaryTree *);
void main()
{
struct BinaryTree *BTree;
int Data[10]={11,2,9,13,57,25,17,1,90,3};
int i;
BTree=NULL;
clrscr();
printf(" Binary Tree Sort .\n");
printf("\nArray :\n");
for(i=0;i<10;i++)
printf("%d\t",Data[i]);
for(i=0;i<=9;i++)
insert(&BTree,Data[i]);
printf("\n In-order traversal of Binary Tree : \n");
inorder(BTree);
getch();
}
void insert(struct BinaryTree **sr,int num)
{
if(*sr == NULL)
{
(*sr)=malloc(sizeof(struct BinaryTree));
(*sr)->LeftChild=NULL;
(*sr)->data=num;
(*sr)->RightChild=NULL;
}
else
{
if(num < (*sr)->data)
insert(&((*sr)->LeftChild),num);
else
insert(&((*sr)->RightChild),num);
}
}
void inorder(struct BinaryTree *sr)
{
if(sr!=NULL)
{
inorder(sr -> LeftChild);
printf("%d\t",sr->data);
inorder(sr -> RightChild);
}
}
#include<conio.h>
#include<alloc.h>
struct BinaryTree
{
struct BinaryTree *LeftChild;
int data;
struct BinaryTree *RightChild;
};
void insert(struct BinaryTree **,int);
void inorder(struct BinaryTree *);
void main()
{
struct BinaryTree *BTree;
int Data[10]={11,2,9,13,57,25,17,1,90,3};
int i;
BTree=NULL;
clrscr();
printf(" Binary Tree Sort .\n");
printf("\nArray :\n");
for(i=0;i<10;i++)
printf("%d\t",Data[i]);
for(i=0;i<=9;i++)
insert(&BTree,Data[i]);
printf("\n In-order traversal of Binary Tree : \n");
inorder(BTree);
getch();
}
void insert(struct BinaryTree **sr,int num)
{
if(*sr == NULL)
{
(*sr)=malloc(sizeof(struct BinaryTree));
(*sr)->LeftChild=NULL;
(*sr)->data=num;
(*sr)->RightChild=NULL;
}
else
{
if(num < (*sr)->data)
insert(&((*sr)->LeftChild),num);
else
insert(&((*sr)->RightChild),num);
}
}
void inorder(struct BinaryTree *sr)
{
if(sr!=NULL)
{
inorder(sr -> LeftChild);
printf("%d\t",sr->data);
inorder(sr -> RightChild);
}
}
Saturday, November 6, 2010
C Program to recontruct a Binary Search Tree
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define MAX 101
struct node
{
struct node *left;
int data;
struct node *right;
};
void insert(struct node **,int);
void preorder(struct node *);
void postorder(struct node *);
void inorder(struct node *);
struct node * recons(int*, int *,int);
void deltree(struct node *);
int in[MAX],pre[MAX],x;
void main()
{
struct node *t,*p,*q;
int req,i,num;
t=NULL; /* Empty Tree */
clrscr();
printf(" Specify the number of items to be inserted : ");
while(1)
{
scanf(" %d",&req);
if(req >= MAX || req <= 0)
printf("\n Enter number between 1 to 100. \n");
else
break;
}
for(i=0;i<req;i++)
{
printf("\n Enter the data : ");
scanf("%d",&num);
insert(&t,num);
}
printf("\n In-order Traversal :\n");
x=0;
inorder(t);
printf("\n Pre-order Traversal :\n");
x=0;
preorder(t);
printf("\n Post-order Traversal :\n");
x=0;
postorder(t);
deltree(t);
t=NULL;
t=recons(in,pre,req);
printf("\n\n After reconstruction of the binary tree.\n");
x=0;
printf("\n In-order Traversal :\n");
inorder(t);
x=0;
printf("\n Pre-order Traversal :\n");
preorder(t);
x=0;
printf("\n Post-order Traversal :\n");
postorder(t);
deltree(t);
getch();
}
/* Inserts a new node in a binary search tree */
void insert(struct node **sr,int num)
{
if(*sr == NULL)
{
*sr=(struct node *)malloc(sizeof(struct node));
(*sr)->left=NULL;
(*sr)->data=num;
(*sr)->right=NULL;
return;
}
else /* Search the node to which new node will be attached */
{
/* If new data is less , traverse to left */
if(num < (*sr)->data)
insert(&((*sr)->left),num);
else
/* Else traverse to right */
insert(&((*sr)->right),num);
}
}
void preorder(struct node *t)
{
if(t != NULL)
{
printf("%d\t",pre[x++]=t->data);
preorder(t->left);
preorder(t->right);
}
}
void postorder(struct node *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("%d\t",t->data);
}
}
void inorder(struct node *t)
{
if(t != NULL)
{
inorder(t->left);
printf("%d\t",in[x++]=t->data);
inorder(t->right);
}
}
struct node * recons(int *inorder,int *preorder,int noofnodes)
{
struct node *temp,*left,*right;
int tempin[100],temppre[100],i,j;
if(noofnodes == 0)
return NULL;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=preorder[0];
temp->left=NULL;
temp->right=NULL;
if(noofnodes == 1)
return temp;
for(i=0;inorder[i]!=preorder[0];)
i++;
if(i>0)
{
for(j=0;j<=i;j++)
tempin[j]=inorder[j];
for(j=0;j<i;j++)
temppre[j]=preorder[j+1];
}
left=recons(tempin,temppre,i);
temp->left=left;
if(i<noofnodes -1)
{
for(j=i;j<noofnodes;j++)
{
tempin[j-i]=inorder[j+1];
temppre[j-i]=preorder[j+1];
}
}
right=recons(tempin,temppre,noofnodes-i-1);
temp->right=right;
return temp;
}
void deltree(struct node *t)
{
if(t!=NULL)
{
deltree(t->left);
deltree(t->right);
}
free(t);
}
#include<conio.h>
#include<alloc.h>
#define MAX 101
struct node
{
struct node *left;
int data;
struct node *right;
};
void insert(struct node **,int);
void preorder(struct node *);
void postorder(struct node *);
void inorder(struct node *);
struct node * recons(int*, int *,int);
void deltree(struct node *);
int in[MAX],pre[MAX],x;
void main()
{
struct node *t,*p,*q;
int req,i,num;
t=NULL; /* Empty Tree */
clrscr();
printf(" Specify the number of items to be inserted : ");
while(1)
{
scanf(" %d",&req);
if(req >= MAX || req <= 0)
printf("\n Enter number between 1 to 100. \n");
else
break;
}
for(i=0;i<req;i++)
{
printf("\n Enter the data : ");
scanf("%d",&num);
insert(&t,num);
}
printf("\n In-order Traversal :\n");
x=0;
inorder(t);
printf("\n Pre-order Traversal :\n");
x=0;
preorder(t);
printf("\n Post-order Traversal :\n");
x=0;
postorder(t);
deltree(t);
t=NULL;
t=recons(in,pre,req);
printf("\n\n After reconstruction of the binary tree.\n");
x=0;
printf("\n In-order Traversal :\n");
inorder(t);
x=0;
printf("\n Pre-order Traversal :\n");
preorder(t);
x=0;
printf("\n Post-order Traversal :\n");
postorder(t);
deltree(t);
getch();
}
/* Inserts a new node in a binary search tree */
void insert(struct node **sr,int num)
{
if(*sr == NULL)
{
*sr=(struct node *)malloc(sizeof(struct node));
(*sr)->left=NULL;
(*sr)->data=num;
(*sr)->right=NULL;
return;
}
else /* Search the node to which new node will be attached */
{
/* If new data is less , traverse to left */
if(num < (*sr)->data)
insert(&((*sr)->left),num);
else
/* Else traverse to right */
insert(&((*sr)->right),num);
}
}
void preorder(struct node *t)
{
if(t != NULL)
{
printf("%d\t",pre[x++]=t->data);
preorder(t->left);
preorder(t->right);
}
}
void postorder(struct node *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("%d\t",t->data);
}
}
void inorder(struct node *t)
{
if(t != NULL)
{
inorder(t->left);
printf("%d\t",in[x++]=t->data);
inorder(t->right);
}
}
struct node * recons(int *inorder,int *preorder,int noofnodes)
{
struct node *temp,*left,*right;
int tempin[100],temppre[100],i,j;
if(noofnodes == 0)
return NULL;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=preorder[0];
temp->left=NULL;
temp->right=NULL;
if(noofnodes == 1)
return temp;
for(i=0;inorder[i]!=preorder[0];)
i++;
if(i>0)
{
for(j=0;j<=i;j++)
tempin[j]=inorder[j];
for(j=0;j<i;j++)
temppre[j]=preorder[j+1];
}
left=recons(tempin,temppre,i);
temp->left=left;
if(i<noofnodes -1)
{
for(j=i;j<noofnodes;j++)
{
tempin[j-i]=inorder[j+1];
temppre[j-i]=preorder[j+1];
}
}
right=recons(tempin,temppre,noofnodes-i-1);
temp->right=right;
return temp;
}
void deltree(struct node *t)
{
if(t!=NULL)
{
deltree(t->left);
deltree(t->right);
}
free(t);
}
Thursday, October 28, 2010
C Progarm to demonstrate MERGE SORT
/* Merge Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]= { 10, 1, 9, 11, 46};
int y[5]= { 20, 15, 0, 72, 2};
int z[10];
int i,j,k,temp;
clrscr();
printf(" Merge Sort .\n");
printf("\n First Array :\n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
printf("\n\n Second Array :\n");
for(i=0;i<=4;i++)
printf(" %d\t",y[i]);
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(x[i] > x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
if(y[i] > y[j])
{
temp=y[i];
y[i]=y[j];
y[j]=temp;
}
}
}
for(i=j=k=0;i<=9;)
{
if(x[j] <= y[k])
z[i++] = x[j++];
else
z[i++] = y[k++];
if(j==5 || k==5)
break;
}
for(;j<=4;)
z[i++] = x[j++];
for(;k<=4;)
z[i++] = y[k++];
printf("\n\n Array after Sorting :\n");
for(i=0;i<=9;i++)
printf(" %d\t",z[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]= { 10, 1, 9, 11, 46};
int y[5]= { 20, 15, 0, 72, 2};
int z[10];
int i,j,k,temp;
clrscr();
printf(" Merge Sort .\n");
printf("\n First Array :\n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
printf("\n\n Second Array :\n");
for(i=0;i<=4;i++)
printf(" %d\t",y[i]);
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(x[i] > x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
if(y[i] > y[j])
{
temp=y[i];
y[i]=y[j];
y[j]=temp;
}
}
}
for(i=j=k=0;i<=9;)
{
if(x[j] <= y[k])
z[i++] = x[j++];
else
z[i++] = y[k++];
if(j==5 || k==5)
break;
}
for(;j<=4;)
z[i++] = x[j++];
for(;k<=4;)
z[i++] = y[k++];
printf("\n\n Array after Sorting :\n");
for(i=0;i<=9;i++)
printf(" %d\t",z[i]);
getch();
}
C Program to demonstrate QUICK SORT
/* Quick Sort */
#include<stdio.h>
#include<conio.h>
int split(int*,int,int);
void main()
{
int arr[10]={ 10, 1, 9, 11,46, 20, 15, 0, 72, 2};
int i;
void quicksort(int *,int,int);
clrscr();
printf(" Quick Sort. \n");
printf("\n Array before sorting : \n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
quicksort(arr,0,9);
printf("\n Array after sorting :\n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
getch();
}
void quicksort(int z[],int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(z,lower,upper);
quicksort(z,lower,i-1);
quicksort(z,i+1,upper);
}
}
int split(int z[],int lower,int upper)
{
int i,a,b,t;
a=lower+1;
b=upper;
i=z[lower];
while(b>=a)
{
while(z[a]<i)
a++;
while(z[b]>i)
b--;
if(b>a)
{
t=z[a];
z[a]=z[b];
z[b]=t;
}
}
t=z[lower];
z[lower]=z[b];
z[b]=t;
return b;
}
#include<stdio.h>
#include<conio.h>
int split(int*,int,int);
void main()
{
int arr[10]={ 10, 1, 9, 11,46, 20, 15, 0, 72, 2};
int i;
void quicksort(int *,int,int);
clrscr();
printf(" Quick Sort. \n");
printf("\n Array before sorting : \n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
quicksort(arr,0,9);
printf("\n Array after sorting :\n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
getch();
}
void quicksort(int z[],int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(z,lower,upper);
quicksort(z,lower,i-1);
quicksort(z,i+1,upper);
}
}
int split(int z[],int lower,int upper)
{
int i,a,b,t;
a=lower+1;
b=upper;
i=z[lower];
while(b>=a)
{
while(z[a]<i)
a++;
while(z[b]>i)
b--;
if(b>a)
{
t=z[a];
z[a]=z[b];
z[b]=t;
}
}
t=z[lower];
z[lower]=z[b];
z[b]=t;
return b;
}
C Program to demontrate INSERTION SORT
/* Insertion Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]= { 23, 15, 29, 11, 1};
int i,j,k,temp;
clrscr();
printf(" Insertion Sort. \n");
printf(" \n Array prior to sorting :\n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
for(i=1;i<=4;i++)
{
for(j=0;j<i;j++)
{
if(x[j] > x[i])
{
temp=x[j];
x[j]=x[i];
for(k=i;k>j;k--)
x[k]=x[k-1];
x[k+1]=temp;
}
}
}
printf("\n\n Array after sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]= { 23, 15, 29, 11, 1};
int i,j,k,temp;
clrscr();
printf(" Insertion Sort. \n");
printf(" \n Array prior to sorting :\n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
for(i=1;i<=4;i++)
{
for(j=0;j<i;j++)
{
if(x[j] > x[i])
{
temp=x[j];
x[j]=x[i];
for(k=i;k>j;k--)
x[k]=x[k-1];
x[k+1]=temp;
}
}
}
printf("\n\n Array after sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
getch();
}
C Program to demontrate Selection Sort
/* Selection Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={23, 15, 29, 11, 1};
int i,j,temp;
clrscr();
printf(" Selection Sort ");
printf("\n Array before Sorting : \n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\n\n Array after Sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",arr[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={23, 15, 29, 11, 1};
int i,j,temp;
clrscr();
printf(" Selection Sort ");
printf("\n Array before Sorting : \n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\n\n Array after Sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",arr[i]);
getch();
}
C Program to demonstrate BUBBLE SORT
/* BUBBLE SORT */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={ 23, 15, 29, 11, 1 };
int i,j,temp;
clrscr();
printf(" Bubble Sort. \n");
printf(" \nArray before sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",arr[i]);
for(i=0;i<=3;i++)
{
for(j=0;j<=3-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\n Array after sorting : \n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={ 23, 15, 29, 11, 1 };
int i,j,temp;
clrscr();
printf(" Bubble Sort. \n");
printf(" \nArray before sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",arr[i]);
for(i=0;i<=3;i++)
{
for(j=0;j<=3-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\n Array after sorting : \n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
getch();
}
Tuesday, October 26, 2010
C Program to demostrate Binary Tree traversal using recursion
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct NODE
{
int data;
struct NODE *leftchild;
struct NODE *rightchild;
};
struct NODE *Binary_Tree(char *,int,int);
void display(struct NODE *,int);
void Pre_order(struct NODE *);
void In_order(struct NODE *);
void Post_order(struct NODE *);
/* Function to create a binary tree */
struct NODE * Binary_Tree(char *List,int Lower,int Upper)
{
struct NODE *Node;
int Mid=(Lower + Upper)/2;
Node=(struct NODE*)malloc(sizeof(struct NODE));
Node->data=List[Mid];
if(Lower>=Upper)
{
Node->leftchild=NULL;
Node->rightchild=NULL;
return(Node);
}
if(Lower <=Mid-1)
Node->leftchild=Binary_Tree(List,Lower,Mid-1);
else
Node->leftchild=NULL;
if(Mid+1<=Upper)
Node->rightchild=Binary_Tree(List,Mid+1,Upper);
else
Node->rightchild=NULL;
return(Node);
}
/* Output function */
void display(struct NODE *T,int Level)
{
int i;
if(T)
{
display(T->rightchild,Level+1);
printf("\n");
for(i=0;i<Level;i++)
printf(" ");
printf(" %d",T->data);
display(T->leftchild,Level+1);
}
}
/* Pre-order traversal */
void Pre_order(struct NODE *Node)
{
if(Node)
{
printf(" %d",Node->data);
Pre_order(Node->leftchild);
Pre_order(Node->rightchild);
}
}
/* In-order traversal */
void In_order(struct NODE *Node)
{
if(Node)
{
In_order(Node->leftchild);
printf(" %d",Node->data);
In_order(Node->rightchild);
}
}
/* Post-order traversal */
void Post_order(struct NODE *Node)
{
if(Node)
{
Post_order(Node->leftchild);
Post_order(Node->rightchild);
printf(" %d",Node->data);
}
}
/* Function Main */
void main()
{
char List[100];
int Number=0;
int info;
char choice;
struct NODE *T=(struct NODE*)malloc(sizeof(struct NODE));
T=NULL;
printf("\n Input Choice 'b' to break : ");
choice=getchar();
fflush(stdin);
while(choice != 'b')
{
printf("\n Input information of the node : ");
scanf("%d",&info);
List[Number++]=info;
fflush(stdin);
printf("\n Input choice 'b' to break : ");
choice=getchar();
fflush(stdin);
}
Number--;
printf("\n Number of elements in the list is %d ",Number);
T=Binary_Tree(List,0,Number);
display(T,1);
printf("\n Pre-order traversal \n");
Pre_order(T);
printf("\n In-order traversal \n");
In_order(T);
printf("\n Post-order traversal \n");
Post_order(T);
}
#include<conio.h>
#include<alloc.h>
struct NODE
{
int data;
struct NODE *leftchild;
struct NODE *rightchild;
};
struct NODE *Binary_Tree(char *,int,int);
void display(struct NODE *,int);
void Pre_order(struct NODE *);
void In_order(struct NODE *);
void Post_order(struct NODE *);
/* Function to create a binary tree */
struct NODE * Binary_Tree(char *List,int Lower,int Upper)
{
struct NODE *Node;
int Mid=(Lower + Upper)/2;
Node=(struct NODE*)malloc(sizeof(struct NODE));
Node->data=List[Mid];
if(Lower>=Upper)
{
Node->leftchild=NULL;
Node->rightchild=NULL;
return(Node);
}
if(Lower <=Mid-1)
Node->leftchild=Binary_Tree(List,Lower,Mid-1);
else
Node->leftchild=NULL;
if(Mid+1<=Upper)
Node->rightchild=Binary_Tree(List,Mid+1,Upper);
else
Node->rightchild=NULL;
return(Node);
}
/* Output function */
void display(struct NODE *T,int Level)
{
int i;
if(T)
{
display(T->rightchild,Level+1);
printf("\n");
for(i=0;i<Level;i++)
printf(" ");
printf(" %d",T->data);
display(T->leftchild,Level+1);
}
}
/* Pre-order traversal */
void Pre_order(struct NODE *Node)
{
if(Node)
{
printf(" %d",Node->data);
Pre_order(Node->leftchild);
Pre_order(Node->rightchild);
}
}
/* In-order traversal */
void In_order(struct NODE *Node)
{
if(Node)
{
In_order(Node->leftchild);
printf(" %d",Node->data);
In_order(Node->rightchild);
}
}
/* Post-order traversal */
void Post_order(struct NODE *Node)
{
if(Node)
{
Post_order(Node->leftchild);
Post_order(Node->rightchild);
printf(" %d",Node->data);
}
}
/* Function Main */
void main()
{
char List[100];
int Number=0;
int info;
char choice;
struct NODE *T=(struct NODE*)malloc(sizeof(struct NODE));
T=NULL;
printf("\n Input Choice 'b' to break : ");
choice=getchar();
fflush(stdin);
while(choice != 'b')
{
printf("\n Input information of the node : ");
scanf("%d",&info);
List[Number++]=info;
fflush(stdin);
printf("\n Input choice 'b' to break : ");
choice=getchar();
fflush(stdin);
}
Number--;
printf("\n Number of elements in the list is %d ",Number);
T=Binary_Tree(List,0,Number);
display(T,1);
printf("\n Pre-order traversal \n");
Pre_order(T);
printf("\n In-order traversal \n");
In_order(T);
printf("\n Post-order traversal \n");
Post_order(T);
}
C Program to insert and delete a node from the binary search tree
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define TRUE 1
#define FALSE 0
struct btreenode
{
struct btreenode *leftchild;
int data;
struct btreenode *rightchild;
};
void insert(struct btreenode **,int);
void del(struct btreenode **,int);
void search(struct btreenode **,int,struct btreenode **,struct btreenode **,int *);
void inorder(struct btreenode *);
void main()
{
struct btreenode *bt;
int req;
int i=0,num,a[]={10,7,11,5,8,12,16,15,6};
bt=NULL; /* Empty Tree */
clrscr();
while(i<=8)
{
insert(&bt,a[i]);
i++;
}
clrscr();
printf(" Binary tree before deletion :\n");
inorder(bt);
del(&bt,11);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,10);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,6);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,16);
printf("\n Binary tree after deletion :\n");
inorder(bt);
getch();
}
/* inserts a new node in a binary search tree */
void insert(struct btreenode **sr,int num)
{
if(*sr==NULL)
{
(*sr)=malloc(sizeof(struct btreenode));
(*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
}
else /* Search the node to whilch new node will be attatched */
{
/* If new data is less, traverse to left*/
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else
/* Else traverse to right */
insert(&((*sr)->rightchild),num);
}
}
/* Deletes a node from the binary search tree */
void del(struct btreenode **root,int num)
{
int found;
struct btreenode *parent,*x,*xsucc;
/* If tree is empty */
if(*root == NULL)
{
printf("\n Tree is Empty ");
return;
}
parent=x=NULL;
/* Call to search function to find the node to be deleted */
search(root,num,&parent,&x,&found);
/* If the node to be deleted is not found */
if(found == FALSE)
{
printf("\n Data to be deleted , not found ");
return;
}
/* If the node to be deleted has two children */
if(x->leftchild !=NULL && x->rightchild!=NULL)
{
parent=x;
xsucc=x->rightchild;
while(xsucc->leftchild != NULL)
{
parent=xsucc;
xsucc=xsucc->leftchild;
}
x->data=xsucc->data;
x=xsucc;
}
/* If the node to be deleted has no child */
if(x->leftchild==NULL && x->rightchild==NULL)
{
if(parent->rightchild==x)
parent->rightchild=NULL;
else
parent->rightchild=NULL;
free(x);
return;
}
/* If the node to be deleted has only right child */
if(x->leftchild==NULL && x->rightchild !=NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->rightchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
/* If the node to be deleted has only left child */
if(x->leftchild != NULL && x->rightchild==NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->leftchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
}
/* Returns the address of the node to be deleted ,address of its parent and whether the node is found or not */
void search(struct btreenode **root,int num,struct btreenode **par,struct btreenode **x,int *found)
{
struct btreenode *q;
q=*root;
*found=FALSE;
*par=NULL;
while(q!=NULL)
{
/* If the node to be deleted is found */
if(q->data == num)
{
*found=TRUE;
*x=q;
return;
}
if(q->data==num)
{
*found=TRUE;
*x=q;
return;
}
*par=q;
if(q->data > num)
q=q->leftchild;
else
q=q->rightchild;
}
}
/* Traverse a binary search tree in an LDR(Left-Data-Right) fashion */
void inorder(struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
/* Print the data of the node whose leftchild is NULL or the path has already been traversed */
printf("%d\t",sr->data);
inorder(sr->rightchild);
}
}
#include<conio.h>
#include<alloc.h>
#define TRUE 1
#define FALSE 0
struct btreenode
{
struct btreenode *leftchild;
int data;
struct btreenode *rightchild;
};
void insert(struct btreenode **,int);
void del(struct btreenode **,int);
void search(struct btreenode **,int,struct btreenode **,struct btreenode **,int *);
void inorder(struct btreenode *);
void main()
{
struct btreenode *bt;
int req;
int i=0,num,a[]={10,7,11,5,8,12,16,15,6};
bt=NULL; /* Empty Tree */
clrscr();
while(i<=8)
{
insert(&bt,a[i]);
i++;
}
clrscr();
printf(" Binary tree before deletion :\n");
inorder(bt);
del(&bt,11);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,10);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,6);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,16);
printf("\n Binary tree after deletion :\n");
inorder(bt);
getch();
}
/* inserts a new node in a binary search tree */
void insert(struct btreenode **sr,int num)
{
if(*sr==NULL)
{
(*sr)=malloc(sizeof(struct btreenode));
(*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
}
else /* Search the node to whilch new node will be attatched */
{
/* If new data is less, traverse to left*/
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else
/* Else traverse to right */
insert(&((*sr)->rightchild),num);
}
}
/* Deletes a node from the binary search tree */
void del(struct btreenode **root,int num)
{
int found;
struct btreenode *parent,*x,*xsucc;
/* If tree is empty */
if(*root == NULL)
{
printf("\n Tree is Empty ");
return;
}
parent=x=NULL;
/* Call to search function to find the node to be deleted */
search(root,num,&parent,&x,&found);
/* If the node to be deleted is not found */
if(found == FALSE)
{
printf("\n Data to be deleted , not found ");
return;
}
/* If the node to be deleted has two children */
if(x->leftchild !=NULL && x->rightchild!=NULL)
{
parent=x;
xsucc=x->rightchild;
while(xsucc->leftchild != NULL)
{
parent=xsucc;
xsucc=xsucc->leftchild;
}
x->data=xsucc->data;
x=xsucc;
}
/* If the node to be deleted has no child */
if(x->leftchild==NULL && x->rightchild==NULL)
{
if(parent->rightchild==x)
parent->rightchild=NULL;
else
parent->rightchild=NULL;
free(x);
return;
}
/* If the node to be deleted has only right child */
if(x->leftchild==NULL && x->rightchild !=NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->rightchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
/* If the node to be deleted has only left child */
if(x->leftchild != NULL && x->rightchild==NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->leftchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
}
/* Returns the address of the node to be deleted ,address of its parent and whether the node is found or not */
void search(struct btreenode **root,int num,struct btreenode **par,struct btreenode **x,int *found)
{
struct btreenode *q;
q=*root;
*found=FALSE;
*par=NULL;
while(q!=NULL)
{
/* If the node to be deleted is found */
if(q->data == num)
{
*found=TRUE;
*x=q;
return;
}
if(q->data==num)
{
*found=TRUE;
*x=q;
return;
}
*par=q;
if(q->data > num)
q=q->leftchild;
else
q=q->rightchild;
}
}
/* Traverse a binary search tree in an LDR(Left-Data-Right) fashion */
void inorder(struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
/* Print the data of the node whose leftchild is NULL or the path has already been traversed */
printf("%d\t",sr->data);
inorder(sr->rightchild);
}
}
C Program to demonstrate Binary Tree through Array
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
#define MAX 10
// #define LEFTCHILD(K) 2K-1
// #define RIGHTCHILD(K) 2K
struct btree
{
int data;
int k;
}tree[100];
void initialize(struct btree tree[],int maxlength)
{
int i;
for(i=0;i<=maxlength;i++)
{
tree[i].data=-1;
tree[i].k=0;
}
}
void create(struct btree tree[],int num)
{
int i;
if(tree[0].data==-1)
{
tree[0].data=num;
tree[0].k=1;
}
else
{
i=tree[0].k;
while(tree[i-1].data!=-1 && i<=MAX)
{
if(tree[i-1].data>num)
{
i=2*tree[i-1].k;
}
else
{
i=2*tree[i-1].k+1;
}
}
tree[i-1].k=i;
tree[i-1].data=num;
}
}
void display(void)
{
int row=0,col=0,root=0;
int i=0;
for(i=0;i<=MAX;i++)
{
printf("\n %d",tree[i].data);
}
//getch();
}
void main()
{
int length=10;
char ch='y';
int choice=0;
int key=0;
clrscr();
do
{
clrscr();
printf("\n\t <<< ARRAY REPRESENTATION OF BINARY TREE >>>\n");
printf("\n 1. Initialize. ");
printf("\n 2. Insert. ");
printf("\n 3. Delete. ");
printf("\n 4. Display. ");
printf("\n 5. EXIT. \n");
printf("\n Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
// INITIALIZE
initialize(tree,length);
printf("\n Binary Tree Initialized. \n");
break;
case 2:
// INSERT
printf("\n Enter The Key : ");
scanf("%d",&key);
create(tree,key);
break;
case 3:
// DELETE
break;
case 4:
// DISPLAY
display();
break;
case 5:
// EXIT
exit(1);
default:
printf("\n Invalid Choice . Try Again...");
}
fflush(stdin);
printf("\n Do you Wish to continue [y/n] :");
ch=getch();
}
while(ch=='y');
getch();
}
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
#define MAX 10
// #define LEFTCHILD(K) 2K-1
// #define RIGHTCHILD(K) 2K
struct btree
{
int data;
int k;
}tree[100];
void initialize(struct btree tree[],int maxlength)
{
int i;
for(i=0;i<=maxlength;i++)
{
tree[i].data=-1;
tree[i].k=0;
}
}
void create(struct btree tree[],int num)
{
int i;
if(tree[0].data==-1)
{
tree[0].data=num;
tree[0].k=1;
}
else
{
i=tree[0].k;
while(tree[i-1].data!=-1 && i<=MAX)
{
if(tree[i-1].data>num)
{
i=2*tree[i-1].k;
}
else
{
i=2*tree[i-1].k+1;
}
}
tree[i-1].k=i;
tree[i-1].data=num;
}
}
void display(void)
{
int row=0,col=0,root=0;
int i=0;
for(i=0;i<=MAX;i++)
{
printf("\n %d",tree[i].data);
}
//getch();
}
void main()
{
int length=10;
char ch='y';
int choice=0;
int key=0;
clrscr();
do
{
clrscr();
printf("\n\t <<< ARRAY REPRESENTATION OF BINARY TREE >>>\n");
printf("\n 1. Initialize. ");
printf("\n 2. Insert. ");
printf("\n 3. Delete. ");
printf("\n 4. Display. ");
printf("\n 5. EXIT. \n");
printf("\n Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
// INITIALIZE
initialize(tree,length);
printf("\n Binary Tree Initialized. \n");
break;
case 2:
// INSERT
printf("\n Enter The Key : ");
scanf("%d",&key);
create(tree,key);
break;
case 3:
// DELETE
break;
case 4:
// DISPLAY
display();
break;
case 5:
// EXIT
exit(1);
default:
printf("\n Invalid Choice . Try Again...");
}
fflush(stdin);
printf("\n Do you Wish to continue [y/n] :");
ch=getch();
}
while(ch=='y');
getch();
}
C Program to multiply two polynomials using array
#include<stdio.h>
#include<conio.h>
#define MAX 10
struct term
{
int coeff;
int exp;
};
struct poly
{
struct term t[10];
int totalterms;
};
void initpoly(struct poly*);
void polycreate(struct poly*,int c,int e);
struct poly mulpoly(struct poly,struct poly);
struct poly addpoly(struct poly,struct poly);
void display(struct poly);
void main()
{
struct poly p1,p2,p3;
clrscr();
initpoly(&p1);
initpoly(&p2);
initpoly(&p3);
polycreate(&p1,1,7);
polycreate(&p1,2,6);
polycreate(&p1,3,5);
polycreate(&p1,4,4);
polycreate(&p1,5,2);
polycreate(&p2,1,4);
polycreate(&p2,1,3);
polycreate(&p2,1,2);
polycreate(&p2,1,1);
polycreate(&p2,2,0);
p3=mulpoly(p1,p2);
printf("\n First polynomial :\n ");
display(p1);
printf("\n Second polynomial :\n ");
display(p2);
printf("\n\n Resultant Polynomial :\n");
display(p3);
getch();
}
/* Initializes elements of struct poly */
void initpoly(struct poly *p)
{
int i;
p->totalterms=0;
for(i=0;i<MAX;i++)
{
p->t[i].coeff=0;
p->t[i].exp=0;
}
}
/* Add the term of polynomial to the array t */
void polycreate(struct poly *p,int c,int e)
{
p->t[p->totalterms].coeff=c;
p->t[p->totalterms].exp=e;
(p->totalterms)++;
}
/* DISPLAY the polynomial equation */
void display(struct poly p)
{
int flag=0,i;
for(i=0;i<p.totalterms;i++)
{
if(p.t[i].exp != 0)
printf("%d x^%d + ",p.t[i].coeff,p.t[i].exp);
else
{
printf("%d",p.t[i].coeff);
flag=1;
}
}
if(!flag)
printf("\b\b");
}
/* ADD two polynomials p1 and p2 */
struct poly addpoly(struct poly p1,struct poly p2)
{
int i,j,c;
struct poly p3;
initpoly(&p3);
if(p1.totalterms>p2.totalterms)
c=p1.totalterms;
else
c=p2.totalterms;
for(i=0,j=0;i<=c;p3.totalterms++)
{
if(p1.t[i].coeff==0 && p2.t[j].coeff==0)
break;
if(p1.t[i].exp>=p2.t[j].exp)
{
if(p1.t[i].exp==p2.t[j].exp)
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff + p2.t[j].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
j++;
}
else
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
}
}
else
{
p3.t[p3.totalterms].coeff=p2.t[j].coeff;
p3.t[p3.totalterms].exp=p2.t[j].exp;
j++;
}
}
return p3;
}
/* Multiplies two polynomials p1 and p2 */
struct poly mulpoly(struct poly p1,struct poly p2)
{
int coeff,exp;
struct poly temp,p3;
initpoly(&temp);
initpoly(&p3);
if(p1.totalterms != 0 && p2.totalterms != 0)
{
int i;
for(i=0;i<p1.totalterms;i++)
{
int j;
struct poly p;
initpoly(&p);
for(j=0;j<p2.totalterms;j++)
{
coeff=p1.t[i].coeff * p2.t[j].coeff;
exp= p1.t[i].exp +p2.t[j].exp;
polycreate(&p,coeff,exp);
}
if(i !=0)
{
p3=addpoly(temp,p);
temp=p3;
}
else
temp=p;
}
}
return p3;
}
#include<conio.h>
#define MAX 10
struct term
{
int coeff;
int exp;
};
struct poly
{
struct term t[10];
int totalterms;
};
void initpoly(struct poly*);
void polycreate(struct poly*,int c,int e);
struct poly mulpoly(struct poly,struct poly);
struct poly addpoly(struct poly,struct poly);
void display(struct poly);
void main()
{
struct poly p1,p2,p3;
clrscr();
initpoly(&p1);
initpoly(&p2);
initpoly(&p3);
polycreate(&p1,1,7);
polycreate(&p1,2,6);
polycreate(&p1,3,5);
polycreate(&p1,4,4);
polycreate(&p1,5,2);
polycreate(&p2,1,4);
polycreate(&p2,1,3);
polycreate(&p2,1,2);
polycreate(&p2,1,1);
polycreate(&p2,2,0);
p3=mulpoly(p1,p2);
printf("\n First polynomial :\n ");
display(p1);
printf("\n Second polynomial :\n ");
display(p2);
printf("\n\n Resultant Polynomial :\n");
display(p3);
getch();
}
/* Initializes elements of struct poly */
void initpoly(struct poly *p)
{
int i;
p->totalterms=0;
for(i=0;i<MAX;i++)
{
p->t[i].coeff=0;
p->t[i].exp=0;
}
}
/* Add the term of polynomial to the array t */
void polycreate(struct poly *p,int c,int e)
{
p->t[p->totalterms].coeff=c;
p->t[p->totalterms].exp=e;
(p->totalterms)++;
}
/* DISPLAY the polynomial equation */
void display(struct poly p)
{
int flag=0,i;
for(i=0;i<p.totalterms;i++)
{
if(p.t[i].exp != 0)
printf("%d x^%d + ",p.t[i].coeff,p.t[i].exp);
else
{
printf("%d",p.t[i].coeff);
flag=1;
}
}
if(!flag)
printf("\b\b");
}
/* ADD two polynomials p1 and p2 */
struct poly addpoly(struct poly p1,struct poly p2)
{
int i,j,c;
struct poly p3;
initpoly(&p3);
if(p1.totalterms>p2.totalterms)
c=p1.totalterms;
else
c=p2.totalterms;
for(i=0,j=0;i<=c;p3.totalterms++)
{
if(p1.t[i].coeff==0 && p2.t[j].coeff==0)
break;
if(p1.t[i].exp>=p2.t[j].exp)
{
if(p1.t[i].exp==p2.t[j].exp)
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff + p2.t[j].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
j++;
}
else
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
}
}
else
{
p3.t[p3.totalterms].coeff=p2.t[j].coeff;
p3.t[p3.totalterms].exp=p2.t[j].exp;
j++;
}
}
return p3;
}
/* Multiplies two polynomials p1 and p2 */
struct poly mulpoly(struct poly p1,struct poly p2)
{
int coeff,exp;
struct poly temp,p3;
initpoly(&temp);
initpoly(&p3);
if(p1.totalterms != 0 && p2.totalterms != 0)
{
int i;
for(i=0;i<p1.totalterms;i++)
{
int j;
struct poly p;
initpoly(&p);
for(j=0;j<p2.totalterms;j++)
{
coeff=p1.t[i].coeff * p2.t[j].coeff;
exp= p1.t[i].exp +p2.t[j].exp;
polycreate(&p,coeff,exp);
}
if(i !=0)
{
p3=addpoly(temp,p);
temp=p3;
}
else
temp=p;
}
}
return p3;
}
C Program to add two polynomials using array
#include<stdio.h>
#include<conio.h>
#define MAX 10
struct term
{
int coeff;
int exp;
};
struct poly
{
struct term t[10];
int totalterms;
};
void initpoly(struct poly*);
void polycreate(struct poly*,int c,int e);
struct poly addpoly(struct poly,struct poly);
void display(struct poly);
void main()
{
struct poly p1,p2,p3;
clrscr();
initpoly(&p1);
initpoly(&p2);
initpoly(&p3);
polycreate(&p1,1,7);
polycreate(&p1,2,6);
polycreate(&p1,3,5);
polycreate(&p1,4,4);
polycreate(&p1,5,2);
polycreate(&p2,1,4);
polycreate(&p2,1,3);
polycreate(&p2,1,2);
polycreate(&p2,1,1);
polycreate(&p2,2,0);
p3=addpoly(p1,p2);
printf("\n First polynomial :\n ");
display(p1);
printf("\n Second polynomial :\n ");
display(p2);
printf("\n\n Resultant Polynomial :\n");
display(p3);
getch();
}
/* Initializes elements of struct poly */
void initpoly(struct poly *p)
{
int i;
p->totalterms=0;
for(i=0;i<MAX;i++)
{
p->t[i].coeff=0;
p->t[i].exp=0;
}
}
/* Add the term of polynomial to the array t */
void polycreate(struct poly *p,int c,int e)
{
p->t[p->totalterms].coeff=c;
p->t[p->totalterms].exp=e;
(p->totalterms)++;
}
/* DISPLAY the polynomial equation */
void display(struct poly p)
{
int flag=0,i;
for(i=0;i<p.totalterms;i++)
{
if(p.t[i].exp != 0)
printf("%d x^%d + ",p.t[i].coeff,p.t[i].exp);
else
{
printf("%d",p.t[i].coeff);
flag=1;
}
}
if(!flag)
printf("\b\b");
}
/* ADD two polynomials p1 and p2 */
struct poly addpoly(struct poly p1,struct poly p2)
{
int i,j,c;
struct poly p3;
initpoly(&p3);
if(p1.totalterms>p2.totalterms)
c=p1.totalterms;
else
c=p2.totalterms;
for(i=0,j=0;i<=c;p3.totalterms++)
{
if(p1.t[i].coeff==0 && p2.t[j].coeff==0)
break;
if(p1.t[i].exp>=p2.t[j].exp)
{
if(p1.t[i].exp==p2.t[j].exp)
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff + p2.t[j].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
j++;
}
else
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
}
}
else
{
p3.t[p3.totalterms].coeff=p2.t[j].coeff;
p3.t[p3.totalterms].exp=p2.t[j].exp;
j++;
}
}
return p3;
}
#include<conio.h>
#define MAX 10
struct term
{
int coeff;
int exp;
};
struct poly
{
struct term t[10];
int totalterms;
};
void initpoly(struct poly*);
void polycreate(struct poly*,int c,int e);
struct poly addpoly(struct poly,struct poly);
void display(struct poly);
void main()
{
struct poly p1,p2,p3;
clrscr();
initpoly(&p1);
initpoly(&p2);
initpoly(&p3);
polycreate(&p1,1,7);
polycreate(&p1,2,6);
polycreate(&p1,3,5);
polycreate(&p1,4,4);
polycreate(&p1,5,2);
polycreate(&p2,1,4);
polycreate(&p2,1,3);
polycreate(&p2,1,2);
polycreate(&p2,1,1);
polycreate(&p2,2,0);
p3=addpoly(p1,p2);
printf("\n First polynomial :\n ");
display(p1);
printf("\n Second polynomial :\n ");
display(p2);
printf("\n\n Resultant Polynomial :\n");
display(p3);
getch();
}
/* Initializes elements of struct poly */
void initpoly(struct poly *p)
{
int i;
p->totalterms=0;
for(i=0;i<MAX;i++)
{
p->t[i].coeff=0;
p->t[i].exp=0;
}
}
/* Add the term of polynomial to the array t */
void polycreate(struct poly *p,int c,int e)
{
p->t[p->totalterms].coeff=c;
p->t[p->totalterms].exp=e;
(p->totalterms)++;
}
/* DISPLAY the polynomial equation */
void display(struct poly p)
{
int flag=0,i;
for(i=0;i<p.totalterms;i++)
{
if(p.t[i].exp != 0)
printf("%d x^%d + ",p.t[i].coeff,p.t[i].exp);
else
{
printf("%d",p.t[i].coeff);
flag=1;
}
}
if(!flag)
printf("\b\b");
}
/* ADD two polynomials p1 and p2 */
struct poly addpoly(struct poly p1,struct poly p2)
{
int i,j,c;
struct poly p3;
initpoly(&p3);
if(p1.totalterms>p2.totalterms)
c=p1.totalterms;
else
c=p2.totalterms;
for(i=0,j=0;i<=c;p3.totalterms++)
{
if(p1.t[i].coeff==0 && p2.t[j].coeff==0)
break;
if(p1.t[i].exp>=p2.t[j].exp)
{
if(p1.t[i].exp==p2.t[j].exp)
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff + p2.t[j].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
j++;
}
else
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
}
}
else
{
p3.t[p3.totalterms].coeff=p2.t[j].coeff;
p3.t[p3.totalterms].exp=p2.t[j].exp;
j++;
}
}
return p3;
}
Friday, October 8, 2010
C Program to create,display and add polynomials
/* Program to create,display and add polynomials */
#include
#include
#include
struct polynode
{
float coeff;
int exp;
struct polynode *next;
};
void create_poly(struct polynode **,float,int);
void display(struct polynode *);
void add_poly(struct polynode *,struct polynode *,struct polynode **);
void main()
{
struct polynode *first,*second,*total;
int i=0;
first=second=total=NULL; /* Empty linked lists */
create_poly(&first,1.4,5);
create_poly(&first,1.5,4);
create_poly(&first,1.7,2);
create_poly(&first,1.8,1);
create_poly(&first,1.9,0);
clrscr();
display(first);
create_poly(&second,1.5,6);
create_poly(&second,2.5,5);
create_poly(&second,-3.5,4);
create_poly(&second,4.5,3);
create_poly(&second,6.5,1);
printf("\n\n");
display(second);
/* Draws a dashed horizontal line */
printf("\n");
while(i++<79) printf("-"); printf("\n\n"); add_poly(first,second,&total); display(total); getch(); } /* ADDs a term to polynomial */ void create_poly(struct polynode **q,float x,int y) { struct polynode *temp; temp=*q; /* Creates a new node if the list is empty */ if(*q==NULL) { (*q)=malloc(sizeof(struct polynode)); temp=*q; } else { /* Traverse the entire Linked List */ while(temp->next != NULL)
temp=temp->next;
/* Create new node at intermediate stages */
temp->next=malloc(sizeof(struct polynode));
temp=temp->next;
}
/* Assign coefficient and exponent */
temp->coeff=x;
temp->exp=y;
temp->next=NULL;
}
/* Displays the contents of linked list representing a polynomial */
void display(struct polynode *q)
{
/* Traverse till the end of the linked list */
while(q!= NULL)
{
printf("%2.1f x^ %d : ",q->coeff,q->exp);
q=q->next;
}
printf("\b\b\b"); /* Erases the last colon */
}
/* Add two polynomials */
void add_poly(struct polynode *x,struct polynode *y,struct polynode **s)
{
struct polynode *z;
/* If both linked lists are empty */
if(x==NULL && y==NULL)
return;
/*Traverse till one of the list ends */
while(x!=NULL && y!=NULL)
{
/*Create a new node if the list is empty */
if(*s==NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
/* Create new nodes at intermediate stages */
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Store a term of larger degree polynomial */
if(x->exp < y->exp)
{
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next; /* GO to next node */
}
else
{
if(x->exp > y->exp)
{
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next;
}
else
{
/* Add the coefficients when exponents ae equal */
if(x->exp == y->exp)
{
/* Assigning the added coefficients */
z->coeff=x->coeff+y->coeff;
z->exp=x->exp;
/* Go to next node */
x=x->next;
y=y->next;
}
}
}
}
/* Assigning remainining terms of the first polynomial to the rssult */
while(x!=NULL)
{
if(*s == NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Assign coefficient and exponent */
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next; /* Go to next node */
/* Assign remainning terms of the second polynomial to the result */
while(y!=NULL)
{
if(*s==NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Assign coefficient and exponent */
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next;
}
z->next=NULL; /* Assign NULLL at the end of the resultoing linked list */
} }
#include
#include
#include
struct polynode
{
float coeff;
int exp;
struct polynode *next;
};
void create_poly(struct polynode **,float,int);
void display(struct polynode *);
void add_poly(struct polynode *,struct polynode *,struct polynode **);
void main()
{
struct polynode *first,*second,*total;
int i=0;
first=second=total=NULL; /* Empty linked lists */
create_poly(&first,1.4,5);
create_poly(&first,1.5,4);
create_poly(&first,1.7,2);
create_poly(&first,1.8,1);
create_poly(&first,1.9,0);
clrscr();
display(first);
create_poly(&second,1.5,6);
create_poly(&second,2.5,5);
create_poly(&second,-3.5,4);
create_poly(&second,4.5,3);
create_poly(&second,6.5,1);
printf("\n\n");
display(second);
/* Draws a dashed horizontal line */
printf("\n");
while(i++<79) printf("-"); printf("\n\n"); add_poly(first,second,&total); display(total); getch(); } /* ADDs a term to polynomial */ void create_poly(struct polynode **q,float x,int y) { struct polynode *temp; temp=*q; /* Creates a new node if the list is empty */ if(*q==NULL) { (*q)=malloc(sizeof(struct polynode)); temp=*q; } else { /* Traverse the entire Linked List */ while(temp->next != NULL)
temp=temp->next;
/* Create new node at intermediate stages */
temp->next=malloc(sizeof(struct polynode));
temp=temp->next;
}
/* Assign coefficient and exponent */
temp->coeff=x;
temp->exp=y;
temp->next=NULL;
}
/* Displays the contents of linked list representing a polynomial */
void display(struct polynode *q)
{
/* Traverse till the end of the linked list */
while(q!= NULL)
{
printf("%2.1f x^ %d : ",q->coeff,q->exp);
q=q->next;
}
printf("\b\b\b"); /* Erases the last colon */
}
/* Add two polynomials */
void add_poly(struct polynode *x,struct polynode *y,struct polynode **s)
{
struct polynode *z;
/* If both linked lists are empty */
if(x==NULL && y==NULL)
return;
/*Traverse till one of the list ends */
while(x!=NULL && y!=NULL)
{
/*Create a new node if the list is empty */
if(*s==NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
/* Create new nodes at intermediate stages */
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Store a term of larger degree polynomial */
if(x->exp < y->exp)
{
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next; /* GO to next node */
}
else
{
if(x->exp > y->exp)
{
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next;
}
else
{
/* Add the coefficients when exponents ae equal */
if(x->exp == y->exp)
{
/* Assigning the added coefficients */
z->coeff=x->coeff+y->coeff;
z->exp=x->exp;
/* Go to next node */
x=x->next;
y=y->next;
}
}
}
}
/* Assigning remainining terms of the first polynomial to the rssult */
while(x!=NULL)
{
if(*s == NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Assign coefficient and exponent */
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next; /* Go to next node */
/* Assign remainning terms of the second polynomial to the result */
while(y!=NULL)
{
if(*s==NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Assign coefficient and exponent */
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next;
}
z->next=NULL; /* Assign NULLL at the end of the resultoing linked list */
} }
Thursday, September 16, 2010
C Program for insertion and deletion operations in a "Doubly Linked List"
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
void d_append(struct node **s,int num)
{
struct node *r,*q=*s;
if(*s==NULL)
{
/* Create a New Node */
(*s)=(struct node*)malloc(sizeof(struct node));
(*s)->left=NULL;
(*s)->data=num;
(*s)->right=NULL;
}
else
{
/* Traverse the Linked List till the last node is reached */
while(q->right != NULL)
q=q->right;
/* Add a new node at the end */
r=(struct node*)malloc(sizeof(struct node));
r->right=NULL;
r->left=q; //*s
q->right=r;
r->data=num;
}
}
void d_display(struct node *q)
{
printf("\n");
/* Traverse the entire linked list */
while(q != NULL)
{
printf(" %d\t",q->data);
q=q->right;
}
}
void d_addatbeg(struct node **s,int num)
{
struct node *q;
/* Create a new node */
q=(struct node*)malloc(sizeof(struct node));
/* Assign data and pointer to the new node */
q->left=NULL;
q->data=num;
q->right=(*s);
/* Make new node the head node */
(*s)->left=q;
(*s)=q;
}
void d_addafter(struct node *q,int loc,int num)
{
struct node *temp;
int i;
/* Skip tp desired position */
for(i=0;i<loc;i++)
{
q=q->right;
/* If end of list is encountered */
if(q==NULL)
{
printf("\n There are less than %d elements ",loc);
return;
}
}
/* Insert new node */
q=q->left;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->left=q;
temp->right=q->right;
temp->right->left=temp;
q->right=temp;
}
void d_delete(struct node **s,int num)
{
struct node *q=*s;
/* Traverse the entire linked list */
while(q != NULL)
{
/* If node to be deleted is found */
if(q->data==num)
{
/* If node to be deleted is the first node */
if(q==*s)
{
(*s)=(*s)->right;
(*s)->left=NULL;
}
else
{
/* If node to be deleted is the last node */
if(q->right==NULL)
q->left->right=NULL;
else
/* If node to be deleted ia any intermediate node */
{
q->left->right=q->right;
q->right->left=q->left;
}
free(q);
}
return;
}
q=q->right; /* Go to next node */
}
printf("\n%d not found ",num);
}
void main()
{
struct node *head;
clrscr();
head=NULL;
d_append(&head,1);
d_append(&head,2);
d_append(&head,3);
d_append(&head,4);
d_append(&head,5);
printf("\n\n The Linked List is :\n");
d_display(head);
printf("\n\n After insertion at the beginning ");
d_addatbeg(&head,100);
d_display(head);
printf("\n\n Insertion after a specified node ");
d_addafter(head,2,200);
d_display(head);
printf("\n\n After Deletion of an element");
d_delete(&head,200);
d_display(head);
getch();
}
#include<conio.h>
#include<malloc.h>
struct node
{
struct node *left;
int data;
struct node *right;
};
void d_append(struct node **s,int num)
{
struct node *r,*q=*s;
if(*s==NULL)
{
/* Create a New Node */
(*s)=(struct node*)malloc(sizeof(struct node));
(*s)->left=NULL;
(*s)->data=num;
(*s)->right=NULL;
}
else
{
/* Traverse the Linked List till the last node is reached */
while(q->right != NULL)
q=q->right;
/* Add a new node at the end */
r=(struct node*)malloc(sizeof(struct node));
r->right=NULL;
r->left=q; //*s
q->right=r;
r->data=num;
}
}
void d_display(struct node *q)
{
printf("\n");
/* Traverse the entire linked list */
while(q != NULL)
{
printf(" %d\t",q->data);
q=q->right;
}
}
void d_addatbeg(struct node **s,int num)
{
struct node *q;
/* Create a new node */
q=(struct node*)malloc(sizeof(struct node));
/* Assign data and pointer to the new node */
q->left=NULL;
q->data=num;
q->right=(*s);
/* Make new node the head node */
(*s)->left=q;
(*s)=q;
}
void d_addafter(struct node *q,int loc,int num)
{
struct node *temp;
int i;
/* Skip tp desired position */
for(i=0;i<loc;i++)
{
q=q->right;
/* If end of list is encountered */
if(q==NULL)
{
printf("\n There are less than %d elements ",loc);
return;
}
}
/* Insert new node */
q=q->left;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->left=q;
temp->right=q->right;
temp->right->left=temp;
q->right=temp;
}
void d_delete(struct node **s,int num)
{
struct node *q=*s;
/* Traverse the entire linked list */
while(q != NULL)
{
/* If node to be deleted is found */
if(q->data==num)
{
/* If node to be deleted is the first node */
if(q==*s)
{
(*s)=(*s)->right;
(*s)->left=NULL;
}
else
{
/* If node to be deleted is the last node */
if(q->right==NULL)
q->left->right=NULL;
else
/* If node to be deleted ia any intermediate node */
{
q->left->right=q->right;
q->right->left=q->left;
}
free(q);
}
return;
}
q=q->right; /* Go to next node */
}
printf("\n%d not found ",num);
}
void main()
{
struct node *head;
clrscr();
head=NULL;
d_append(&head,1);
d_append(&head,2);
d_append(&head,3);
d_append(&head,4);
d_append(&head,5);
printf("\n\n The Linked List is :\n");
d_display(head);
printf("\n\n After insertion at the beginning ");
d_addatbeg(&head,100);
d_display(head);
printf("\n\n Insertion after a specified node ");
d_addafter(head,2,200);
d_display(head);
printf("\n\n After Deletion of an element");
d_delete(&head,200);
d_display(head);
getch();
}
Sunday, September 12, 2010
C Program for various patterns of Pyramid
1)
#include<stdio.h>
#include<conio.h>
void main()
{
int c,k,i,j,n,v=1;
clrscr();
printf("enter the limit.....\n");
scanf("%d",&n);
c=n-1;
for(i=0;i<n;i++)
{
for(j=c;j>0;j--)
{
printf(" ");
}
c--;
for(k=0;k<=i;k++)
{
printf("%d ",v);
}
v++;
printf("\n");
}
getch();
}
2)
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,c,k;
printf(" Enter the limit ");
scanf("%d",&n);
c=n;
for(j=0;j<n;j++)
{
for(i=c;i>0;i--)
{
printf("*");
}
printf("\n");
c--;
for(k=0;k<=j;k++)
{
printf(" ");
}
}
getch();
}
3)
#include<stdio.h>
#include<conio.h>
void main()
{
int row,col,n;
printf(" Enter the height ");
scanf("%d",&n);
for(row=0;row<n;row++)
{
for(col=0;col<row+1;col++)
{
printf("*");
}
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int c,k,i,j,n,v=1;
clrscr();
printf("enter the limit.....\n");
scanf("%d",&n);
c=n-1;
for(i=0;i<n;i++)
{
for(j=c;j>0;j--)
{
printf(" ");
}
c--;
for(k=0;k<=i;k++)
{
printf("%d ",v);
}
v++;
printf("\n");
}
getch();
}
2)
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,c,k;
printf(" Enter the limit ");
scanf("%d",&n);
c=n;
for(j=0;j<n;j++)
{
for(i=c;i>0;i--)
{
printf("*");
}
printf("\n");
c--;
for(k=0;k<=j;k++)
{
printf(" ");
}
}
getch();
}
3)
#include<stdio.h>
#include<conio.h>
void main()
{
int row,col,n;
printf(" Enter the height ");
scanf("%d",&n);
for(row=0;row<n;row++)
{
for(col=0;col<row+1;col++)
{
printf("*");
}
printf("\n");
}
getch();
}
4)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n;
printf(" Enter the limit ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
printf(" ");
for(j=i+1;j<2*i+2;j++)
printf("*");
for(m=2*i;m>i;m--)
printf("*");
printf("\n");
}
}
5)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n;
printf(" Enter the limit ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
printf(" ");
for(j=i+1;j<2*i+2;j++)
printf("%d",j%10);
for(m=2*i;m>i;m--)
printf("%d",m%10);
printf("\n");
}
}
Subscribe to:
Posts (Atom)