Escolar Documentos
Profissional Documentos
Cultura Documentos
0
2
Traversing
Insertion through position
Deletion through position
Searching ( Linear and Binary)
Bubble Sort
Merging
Reversing
case 2:
printf("\nenter the array element");
for(i=0;i<=5;i++)
scanf("\n%d",&ram[i]);
printf("\n\nthe array elements are :-> ");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
insert(ram,3,9);
printf("\n\nthe array elements after insertion are:->");
for(i=0;i<=6;i++)
printf(" %d",ram[i]);
break;
case 3:
printf("\nenter the array element");
for(i=0;i<=6;i++)
scanf("\n%d",&ram[i]);
printf("\n\nthe array elements are :-> ");
for(i=0;i<=6;i++)
printf(" %d",ram[i]);
delet(ram,3);
printf("\n\nthe array element after deletion are:->");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
break;
case 4:
printf("\nenter the array element");
for(i=0;i<=5;i++)
scanf("\n%d",&ram[i]);
printf("\n\nthe array elements are :-> ");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
revers(ram);
printf("\n\nthe array element after reversing are:->");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
break;
case 5:
printf("\nenter the array element");
for(i=0;i<=5;i++)
scanf("\n%d",&ram[i]);
printf("\n\nthe array elements are :-> ");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
printf("\n\nenter the element to be search by ls");
scanf("%d",&n);
for(i=0;i<=5;i++)
{
if(ram[i]==n)
break;
}
if(i==6)
printf("\n\nthe element %d is not in array",n);
else
printf("\n\nthe element %d is at %d position",n,i+1);
break;
case 6:
printf("\n\nenter the element of a new array in sorted order");
for(i=0;i<=7;i++)
scanf("%d",&hr[i]);
for(i=0;i<=7;i++)
printf(" %d",hr[i]);
printf("\n\nenter the element to be search by bs");
scanf("%d",&n2);
for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(hr[mid]==n2)
{
printf("\n\nthe no. is present at %d position of array",(mid+1));
flag=0;
break;
}
if(hr[mid]>n2)
upper=mid-1;
else
lower=mid+1;
}
if(flag)
printf("\n\nthe element is not in array");
break;
case 7:
printf("\n\nthe element before sorting are:->");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
for(i=0;i<=4;i++)
{
for(j=0;j<=4-i;j++)
{
if(ram[j]>ram[j+1])
{
temp=ram[j];
ram[j]=ram[j+1];
ram[j+1]=temp;
}
}
}
printf("\n\nthe array after sorting is:->");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
break;
case 8:
exit(0);
}
getch();
}
}
void insert(int*ram,int pos,int num)
{
int i;
for(i=6;i>=pos;i--)
{
ram[i]=ram[i-1];
}
ram[i]=num;
}
void delet(int*ram,int pos)
{
int i;
for(i=pos;i<=6;i++)
{
ram[i-1]=ram[i];
}
}
void revers(int*ram)
{
int temp;
for( int i=0;i<=5/2;i++)
{
temp=ram[i];
ram[i]=ram[5-i];
ram[5-i]=temp;
}
the
the
the
the
Enter
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
the
value
value
value
value
value
value
in
in
in
in
in
in
the
the
the
the
the
the
element
element
element
element
element
element
a[1][0]45
a[1][1]5
a[1][2]7
a[2][0]8
a[2][1]9
a[2][2]7
/*output is
Enter the values of element in the sparse tridiagonal matrix
1
2
3
4
5
6
7
8
9
1
Sprarse Matrix Is
1 2 0 0
3 4 5 0
0 6 7 8
0 0 9 1
The address of element a[0][0] is 65502
Enter the index of element you want to get address [i][j]: 0 1
The address of element a[0][1] is 65504
*/
4. Generate the lower and upper sparse matrix using for loop, and
store it in two one-ray. Derive a formula for its address retrieval.
#include<conio.h>
#include<stdio.h>
void main()
{
int a[4][4],b[10],i,j,ch,k=0;
clrscr();
printf("Do u want to enter upper triangular (press 1) or \nlower
triangular(press 2) :");
scanf("%d",&ch);
printf("\n\nEnter the elements of the array : \n\n");
/* for upper matrix*/
if(ch==1)
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(i>=j)
{
printf("Enter the value of element a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
else
a[i][j]=0;
}
/*for column major storage in 1-D array*/
for(j=0,k=0;j<4;j++)
for(i=0;i<4;i++)
if(i>=j)
{
b[k]=a[i][j];
k++;
}
}
/* for lower matrix*/
if(ch==2)
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(j>=i)
{
printf("Enter the value of element a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
else
a[i][j]=0;
}
/*for column major storage in 1-D array*/
for(j=0,k=0;j<4;j++)
for(i=0;i<4;i++)
if(j>=i)
{
b[k]=a[i][j];
k++;
}
}
printf("\n\nYour 2-D array is : \n\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf(" %d ",a[i][j]);
printf("\n\n");
}
printf("\n\nYour 1-D array is : \n\n");
for(k=0;k<10;k++)
printf(" %d ",b[k]);
printf("Enter the index of the non zero element of a[i][j]: ");
scanf("%d %d",&i,&j);
/*for address manupulation */
if(ch==1)
{
printf("FOR UPPERCASE MATRIX ONL\n");
printf("\nBase address is : %u\n",b);
printf("The address of the element is : %u " ,b+(((j*(9-i))/2)+i-j+1));
}
getch();
}
/*
output is
Do u want to enter upper triangular (press 1) or
lower triangular(press 2) :1
Enter the elements of the array :
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
the
the
the
the
the
value
value
value
value
value
value
value
value
value
value
of
of
of
of
of
of
of
of
of
of
element
element
element
element
element
element
element
element
element
element
a[0][0]
a[1][0]
a[1][1]
a[2][0]
a[2][1]
a[2][2]
a[3][0]
a[3][1]
a[3][2]
a[3][3]
:
:
:
:
:
:
:
:
:
:
1
1
1
1
1
1
1
1
1
1
s->top++;
s->ram[s->top]=num;
}
int pop(struct stack*s)
{
int data;
if(s->top==-1)
{
printf("\n\nstack is empty");
return NULL;
}
data=s->ram[s->top];
s->top--;
return data;
}
/* output
stack is full
item is poped:8
item is poped:5
item is poped:75
*/
return NULL;
}
data=ram[*pfront];
ram[*pfront]=0;
if(*pfront==*prear)
*pfront=*prear=-1;
else
(*pfront)++;
return data;
}
/* output
item is deleted
item is deleted
item is deleted
*/
if(p1.not>p2.not)
c=p1.not;
else
c=p2.not;
for(i=0,j=0;j<=c;p3.not++)
{
if(p1.t[i].coff==0&&p2.t[j].coff==0)
break;
if(p1.t[i].exp>=p2.t[j].exp)
{
if(p1.t[i].exp==p2.t[j].exp)
{
p3.t[p3.not].coff=p1.t[i].coff+p2.t[j].coff;
p3.t[p3.not].exp=p1.t[i].exp;
i++;
j++;
}
else
{
p3.t[p3.not].coff=p1.t[i].coff;
p3.t[p3.not].exp=p1.t[i].exp;
i++;
}
}
else
{
p3.t[p3.not].coff=p2.t[j].coff;
p3.t[p3.not].exp=p2.t[j].exp;
j++;
}
}
return p3;
}
/* output
first polynomial is:
1x^7+2x^6+3x^5+4x^ +
second polynomial is:
1x^4+1x^3+1x^5+2x^ +
res polynomial is:
1x^7+2x^6+3x^5+5x^4+1x^3+1x^5+2x^ +
*/
Item popped:29
*/
delqueue(&a);
getch();
}
void initqueue(struct queue *q)
{
q->front=q->rear=NULL;
}
void addq(struct queue *q, int item)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if (temp == NULL)
printf("\nQueue is full");
temp->data=item;
temp->link=NULL;
if(q->front == NULL)
{
q->rear=q->front=temp;
return;
}
q->rear->link=temp;
q->rear=q->rear->link;
}
int delq(struct queue *q)
{
struct node *temp;
int item;
if(q->front==NULL)
{
printf("\nQueue is empty");
return NULL;
}
item=q->front->data;
temp=q->front;
q->front=q->front->link;
free(temp);
return item;
}
void delqueue(struct queue *q)
{
struct node *temp;
if(q->front==NULL)
return;
while(q->front!=NULL)
{
temp=q->front;
q->front=q->front->link;
free(temp);
}
}
/*OUTPUT
Item extracted:11
Item extracted:-8
Item extracted:23*/
q->data=item;
if(*f==NULL)
*f=q;
else
(*r)->link=q;
*r=q;
(*r)->link=*f;
}
int delcirq(struct node **f, struct node **r)
{
struct node *q;
int item;
if(*f==NULL)
printf("queue is empty");
else
{
if(*f==*r)
{
item=(*f)->data;
free(*f);
*f=NULL;
*r=NULL;
}
else
{
q=*f;
item=q->data;
*f=(*f)->link;
(*r)->link=*f;
free(q);
}
return(item);
}
return NULL;
}
void cirq_display(struct node *f)
{
struct node *q=f,*p=NULL;
while(q!=p)
{
printf("%d",q->data);
q=q->link;
p=f;
}
}
/*OUTPUT
Before deletion:
10 17 18 5 30 15
After deletion:
5 30 15
*/
}
void setexpr(struct postfix *p, char *str)
{
p->s=str;
}
void push(struct postfix *p, int item)
{
if(p->top==MAX-1)
printf("\nStack is full");
else
{
p->top++;
p->stack[p->top]=item;
}
}
int pop(struct postfix *p)
{
int data;
if(p->top==-1)
{
printf("\nStack is empty");
return NULL;
}
data=p->stack[p->top];
p->top--;
return data;
}
void calculate(struct postfix *p)
{
int n1, n2,n3;
while(*(p->s))
{
if(*(p->s)==' '||*(p->s)=='\t')
{
p->s++;
continue;
}
if(isdigit(*(p->s)))
{
p->nn=*(p->s)-'0';
push(p,p->nn);
}
else
{
n1=pop(p);
n2=pop(p);
switch(*(p->s))
{
case'+':
n3=n2+n1;
break;
case'-':
n3=n2-n1;
break;
case'/':
n3=n2/n1;
break;
case'*':
n3=n2*n1;
break;
case'%':
n3=n2%n1;
break;
case'$':
n3=pow(n2,n1);
break;
default:
printf("Unknown operator");
exit(1);
}
push(p,n3);
}
p->s++;
}
}
void show(struct postfix p)
{
p.nn=pop(&p);
printf("Result is:%d",p.nn);
}
/*OUTPUT
Enter postfix expression to be evaluated:4 2 $ 3 * 3 - 8 4 / 1 1 + / +
Result is:46
*/
while(*(p->s))
{
if(*(p->s)==' '||*(p->s)=='\t')
{
p->s++;
continue;
}
if(isdigit(*(p->s))||isalpha(*(p->s)))
{
while(isdigit(*(p->s))||isalpha(*(p->s)))
{
*(p->t)=*(p->s);
p->s++;
p->t++;
}
}
if(*(p->s)=='(')
{
push(p, *(p->s));
p->s++;
}
if(*(p->s)=='*'||(p->s)=='/'||
*(p->s)=='%'||*(p->s)=='_'||*(p->s)=='$')
{
if(p->top!=-1)
{
opr=pop(p);
while(priority(opr)>=priority(*(p->s)))
{
*(p->t)=opr;
p->t++;
opr=pop(p);
}
push(p, opr);
push(p,*(p->s));
}
else
push(p, *(p->s));
p->s++;
}
if(*(p->s)==')')
{
opr=pop(p);
while((opr)!='(')
{
*(p->t)=opr;
p->t++;
opr=pop(p);
}
p->s++;
}
}
while(p->top!=-1)
{
char opr=pop(p);
*(p->t)=opr;
p->t++;
}
*(p->t)='\0';
}
int priority(char c)
{
if(c=='$')
return3;
if(c=='*'||c=='/'||c=='%')
return2;
else
{
if(c=='+'||c=='-')
return 1;
else
return 0;
}
}
void show(struct infix p)
{
printf("%s",p.target);
}
/*OUTPUT
Enter an expression in infix form: 4 $ 2 * 3 - 3 + 8 / 4 / ( 1 + 1
Stack is empty.
The postfix expression is: 42$3*3-84/11+/+*/
printf("\t%d", sr->data);
}
else
return;
}
/*OUTPUT
Specify the number of items to be inserted:5
Enter the data:1
Enter the data:2
Enter the data:3
Enter the data:4
Enter the data:5
In-order Traversal:
1
Pre-order Traversal: 1
Post-order Traversal: 5
*/
2
2
4
3
3
3
4
4
2
5
5
1