Saturday 13 December 2014

wap to implement transpose of matrix

wap to implement transpose of matrix

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j,m,n;
clrscr();
printf("enter the elements of matrix A\n");
{
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
b[j][i]=a[i][j];
}
}
}
printf("\n transepose of matrix A is \n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
  printf("\t %d",b[i][j]);
}
printf("\n");
}
getch();
}

wap for polynomial

wap for polynomial

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

typedef struct node
{
int coe;
int power;
struct node *next;
}node;
void main()
{
node *head,*p;
int x,val=0;
int i,n;
clrscr();
printf("enter the value of n=");
scanf("%d",&n);
head=(node*)malloc(sizeof(node));
scanf("%d%d",&head->coe,&head->power);
head->next=NULL;
p=head;
for(i=0;i<n;i++)
{
p->next=(node*)malloc(sizeof(node));
p=p->next;
scanf("%d%d",&p->coe,&p->power);
p->next=NULL;
}
p=head;
while(p->next!=NULL)
{
printf("%dx^%d+",p->coe,p->power);
p=p->next;
}
printf("%dx^n%d",p->coe,p->power);
printf("enter the value of x");
scanf("%d",&x);
p=head;
while(p!=NULL)
{
val=val+(p->coe*pow(x,p->power));
p=p->next;
}
printf("%d",val);
getch();
}

wap to implement stack

wap to implement stack

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int s[20];
int top;
}st;
int stfull()
{
if(st.top>=19)
return 1;
else
return 0;
}
int stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
void push(int x)
{
st.top++;
st.s[st.top]=x;
}
int pop()
{
int x;
x=st.s[st.top];
st.top--;
return (x);
}
void display()
{
int i;
if(stempty())
printf("Empty use.");
else
{
for(i=st.top;i>=0;i--)
printf("%d",st.s[i]);
}
}
main()
{
int x,choice;
char ch;
st.top=-1;
do
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf("\nEnter your choice=");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter value=");
scanf("%d",&x);
if(stfull())
printf("\nStack is full.");
else
push(x);
break;
case 2:
if(stempty())
printf("\nStack is empty.");
else
x=pop();
printf("\nPopped out element is=%d",x);
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nNo action will be performed.");
}
printf("\nDo you want to continue");
scanf("%c",&ch);
}
while(ch=='y'||ch=='Y');
getch();
}

wap for rainbow

wap for rainbow

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gdriver = DETECT,gmode;
int x,y,i;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
for(i=30;i<200;i++)
{
delay(100);
setcolor(i/10);
arc(x,y,0,180,i-10);
}
getch();
}
wap to implement  queue

#include<stdio.h>
#include<conio.h>
#define MAX 30
struct queue
{
int a[MAX];
int front,rear;
}q;
void main()
{
int i,op,x;
q.rear=-1;
q.front=-1;
clrscr();
do
{
printf("\nmain menu:");
printf("\n1)enqueuen\n2)dequeue\n3)display\n4)exit");
printf("\nenter your choioce");
scanf("%d",&op);
switch(op)
{
case 1: if(q.rear==MAX-1)
printf("\nqueue is full");
else
{
printf("\nenter element");
scanf("%d",&x);
if(q.rear==-1)
{
q.rear=q.rear+1;
q.front=q.front+1;
q.a[q.rear]=x;
}
else
{
q.rear=q.rear+1;
q.a[q.rear]=x;
}
}
break;
case 2: if(q.rear==-1)
printf("\n wueue is empty");
else
{
if(q.rear==q.front)
{
x=q.a[q.front];
q.front=-1;
q.rear=-1;
printf("\ndeleted element is:%d",x);
}
else
{
x=q.a[q.front];
q.front=q.front+1;
printf("\ndeleted element is %d",x);
}
}
break;
case 3: if(q.rear==-1)
printf("\nqueue is empty");
else
{
printf("\nqueue is");
for(i=q.front;i<=q.rear;i++)
printf("<%d>",q.a[i]);
}
break;
}
}while(op!=4);
getch();
}

wap to invert t linked list

wap to invert t linked list


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct node
{
struct node *next;
int data;
}node;
void main()
{
int n,i;
node *head,*p,*q,*r;
clrscr();
printf("\nenter no of elements");
scanf("%d",&n);
head=(node*)malloc(sizeof(node));
scanf("%d",&head->data);
head->next=NULL;

p=head;
for(i=1;i<n;i++)
{
p->next=(node*)malloc(sizeof(node));
p=p->next;
scanf("%d",&p->data);
p->next=NULL;
}
p=head;
while(p!=NULL)
{
printf("\ndata=<%d>address=%u,next data address=%u",p->data,&p->data,&p->next->data);
p=p->next;
}
p=NULL;
q=head;
r=q->next;
while(q!=NULL)
{
q->next=p;
p=q;
q=r;
if(r!=NULL)
r=r->next;
}
printf("\ninversion is:");
while(p!=NULL)
{
printf("\ndata=<%d>address=%u,next data address=%u",p->data,&p->data,&p->next->data);
p=p->next;
}
getch();
}

wap to implement circular link list

wap to implement circular link list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct node
{
int data;
struct node *next;
}node;
void main()
{
node *rear,*p;
int n,i;
printf("\nenter no of elements");
scanf("%d",&n);
rear=(node*)malloc(sizeof(node));
scanf("%d",&rear->data);
rear->next=rear;
for(i=1;i<n;i++)
{
p=(node*)malloc(sizeof(node));
scanf("%d",&p->data);
p->next=rear->next;
rear->next=p;
rear=p;
}
p=rear->next;
do
{
printf("\ndata=%daddress=%unext data address=%u",p->data,&p->data,&p->next->data);
p=p->next;
}while(p!=rear->next);
getch();
}

summation of two variable using pointer

summation of two variable using pointer

#include<stdio.h>
#include<conio.h>
int sum()
{
int sum=0;,*x,*y;
printf("\n enter the value to be added=");
scanf("%d%d",x,y);
sum=(*x)+(*y);
printf("\n result=%d",sum);
return() 0;
}
void main()
{
clrscr();
sum();
getch();
}

wap to implement doubly linked list

wap to implement doubly linked list

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next,*prev;
}node;
void main()
{
int i,n,ch,loc,x;
node *head,*p,*q;
char c;
clrscr();
printf("N=");
scanf("%d",&n);
head=(node *)malloc(sizeof(node));
printf("\n\nEnter data of the nodes=");
scanf("%d",&head->data);
head->next=NULL;
head->prev=NULL;
p=head;
for(i=1;i<n;i++)
{
p->next=(node *)malloc(sizeof(node));
q=p;
p=p->next;
scanf("%d",&p->data);
p->prev=q;
p->next=NULL;
}
p=head;
printf("The linked list is=\nHead ");
while(p!=NULL)
{
printf(" -><-%d",p->data);
p=p->next;
}
do
{
printf("\n\n1.Insertion\t2.Deletion\n");
printf("Enter your choice=");
scanf("%d",&ch);
if(ch==1)
{
printf("\n\nEnter the location on which element to be inserted=");
scanf("%d",&loc);
printf("\nEnter the data to be inserted=");
scanf("%d",&x);
p=(node *)malloc(sizeof(node));
p->data=x;
p->next=NULL;
p->prev=NULL;
if(loc==1)
{
p->next=head;
head->prev=p;
head=p;
n++;
}
else if(loc==n)
{
q=head;
while(q->next!=NULL)
q=q->next;
q->next=p;
p->prev=q;
n++;
}
else if(loc>1&&loc<n)
{
q=head;
for(i=1;i<loc-1;i++)
q=q->next;
p->next=q->next;
p->prev=q;
q->next=p;
p->next->prev=p;
}
else
{
printf("\nInsertion not possible.");
}
p=head;
printf("The linked list is=\nHead ");
while(p!=NULL)
{
printf(" -><-%d",p->data);
p=p->next;
}
}
else if(ch==2)
{
printf("\n\nEnter the location to be deleted=");
scanf("%d",&loc);
p=head;
if(loc==1)
{
p->next->prev=NULL;
head=head->next;
free(p);
n--;
}
else if(loc==n)
{
while(p->next!=NULL)
p=p->next;
p->prev->next=NULL;
free(p);
n--;
}
else if(loc>1&&loc<n)
{
for(i=1;i<loc;i++)
p=p->next;
p->prev->next=p->next;
p->next->prev=p->prev;
free(p);
}
else
{
printf("\nInvalid location.");
}
p=head;
printf("The linked list is=\nHead ");
while(p!=NULL)
{
printf(" -><-%d",p->data);
p=p->next;
}
}
else
{
printf("\nSorry.");
}
printf("\n\nDo you  want to continue=");
c=getche();
}
while(c=='Y'||c=='y');
getch();
}

wap to implement sparse matrix using c

wap to  implement sparse matrix using c

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[3][10],k=1,i,j,m,n;
clrscr();
printf("enter the value of a matrix");
scanf("%d%d",&n,&m);
printf("\n\n enter the matrix elements=");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n\n the matrix is =\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
}
b[0][0]=m;
b[0][1]=n;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]!=0)
{
b[k][0]=i;
b[k][1]=j;
b[k][2]=a[i][j];
k++;
}
}
}
b[0][2]=k-1;
printf("\nThe sparse matrix is=\n");
for(k=0;k<b[0][2];k++)
{
printf("\n");
for(i=0;i<3;i++)
{
printf("%d\t",b[k][i]);
}
}
getch();
}