Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

1] Write a c program to Transpose of a Sparse matrix.

#include<stdio.h>

#include<conio.h>

void main()

int mat[10][10],mat_trans[10][10],row,col,nonZeroValue,i=0,j=0,p=1;

clrscr();

printf("\nEnter number of rows and columns : ");

scanf("%d %d",&row,&col);

printf("\nEnter number of non zero elements : ");

scanf("%d",&nonZeroValue);

mat[0][0]=row;

mat[1][0]=col;

mat[2][0]=nonZeroValue;

printf("\nEnter non-zero value in triple format : \n");

for(i=1;i<=nonZeroValue;i++)

printf("\nFor value %d - ",i);

printf("\n\tEnter Row No., Column No. and Value : ");

scanf("%d %d %d",&mat[0][i],&mat[1][i],&mat[2][i]);

printf("\n**** SPARSE MATRIX IS *****");

printf("\nNo. of rows No. of columns No. of nonzero values");

printf("\n[%d]\t\t[%d]\t\t[%d] ",mat[0][0],mat[1][0],mat[2][0]);

for(i=1;i<=nonZeroValue;i++)

printf("\n%d\t\t %d\t\t %d ",mat[0][i],mat[1][i],mat[2][i]);

//Finding transpose

//if original matrix is m * n then, transpose will be n * m


//So copying number of rows and columns in the same manner

mat_trans[0][0] = mat[1][0];

mat_trans[1][0] = mat[0][0];

mat_trans[2][0] = mat[2][0];

//Copying values columnwise i.e. first check for column 0 in all

//triples and copy those triples first. Then for column 1 and so on..

p = 1;

for(i=0;i<mat_trans[0][0];i++)

for(j=1;j<=nonZeroValue;j++)

if(mat[1][j] == i)

mat_trans[0][p] = i;

mat_trans[1][p] = mat[0][j];

mat_trans[2][p] = mat[2][j];

p++;

//Simple logic without rearranging columns

/*for(i=1;i<=nonZeroValue;i++)

mat_trans[i][0] = mat[i][1];

mat_trans[i][1] = mat[i][0];

mat_trans[i][2] = mat[i][2];

} */

printf("\n**** TRANSPOSE OF SPARSE MATRIX IS *****");

printf("\nNo. of rows No. of columns No. of nonzero values");

printf("\n[%d]\t\t[%d]\t\t[%d] ",mat_trans[0][0],mat_trans[1][0],mat_trans[2][0]);
for(i=1;i<=nonZeroValue;i++)

printf("\n%d\t\t %d\t\t\ %d ",mat_trans[0][i],mat_trans[1][i],mat_trans[2][i]);

Output:

Enter number of rows and columns : 5 4

Enter number of non zero elements : 6

Enter non-zero value in triple format :

For value 1 -

Enter Row No., Column No. and Value : 2 0 3

For value 2 -

Enter Row No., Column No. and Value : 4 0 3

For value 3 -

Enter Row No., Column No. and Value : 2 1 4

For value 4 -

Enter Row No., Column No. and Value : 3 1 5

For value 5 -

Enter Row No., Column No. and Value : 1 3 7

For value 6 -

Enter Row No., Column No. and Value : 2 3 6

**** SPARSE MATRIX IS *****

No. of rows No. of columns No. of nonzero values

[5] [4] [6]

2 0 3

4 0 3

2 1 4

3 1 5

1 3 7

2 3 6
**** TRANSPOSE OF SPARSE MATRIX IS *****

No. of rows No. of columns No. of nonzero values

[4] [5] [6]

0 2 3

0 4 3

1 2 4

1 3 5

3 1 7

3 2 6
2] Write a c program to implement stack using static array.

#include<stdio.h>

int stack[100],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);

int main()

clrscr();

top=-1;

printf("\n Enter the size of STACK[MAX=100]:");

scanf("%d",&n);

printf("\n\t STACK OPERATIONS USING ARRAY");

printf("\n\t--------------------------------");

printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

do

printf("\n Enter the Choice:");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();

break;
}

case 3:

display();

break;

case 4:

printf("\n\t EXIT POINT ");

break;

default:

printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");

while(choice!=4);

return 0;

void push()

if(top>=n-1)

printf("\n\tSTACK is over flow");

else

printf(" Enter a value to be pushed:");


scanf("%d",&x);

top++;

stack[top]=x;

void pop()

if(top<=-1)

printf("\n\t Stack is under flow");

else

printf("\n\t The popped elements is %d",stack[top]);

top--;

void display()

if(top>=0)

printf("\n The elements in STACK \n");

for(i=top; i>=0; i--)

printf("\n%d",stack[i]);

printf("\n Press Next Choice");

else

printf("\n The STACK is empty");

}
}

Output:

Enter the size of STACK[MAX=100]:50

STACK OPERATIONS USING ARRAY

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

1.PUSH

2.POP

3.DISPLAY

4.EXIT

Enter the Choice:1

Enter a value to be pushed:6

Enter the Choice:3

The elements in STACK

Press Next Choice

Enter the Choice:2

The popped elements is 6

Enter the Choice:3

The STACK is empty


3] Design, develop and execute a program in C to evaluate a valid postfix expression using stack .
Assume that a postfix expression is read as single line consisting of non-negative single digit
operands and binary arithmetic operations. The arithmetic operators are +(add0,-
(substract),*(multiply) and /(divide).

#include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

#define SIZE 40

int pop();

void push(int);

char postfix[SIZE];

int stack[SIZE], top = -1;

void main()

int i, a, b, result, pEval;

char ch;

clrscr();

for(i=0; i<SIZE; i++)

stack[i] = -1;

printf("\nEnter a postfix expression: ");

scanf("%s",postfix);

for(i=0; postfix[i] != '\0'; i++)

ch = postfix[i];

if(isdigit(ch))

{
push(ch-'0');

printf("\nPushed element is: %d\n",ch-'0');

else if(ch == '+' || ch == '-' || ch == '*' || ch == '/')

b = pop();

printf("\nPoped element is %d\n",b);

a = pop();

printf("\nPoped element is %d\n",a);

switch(ch)

case '+': result = a+b;

break;

case '-': result = a-b;

break;

case '*': result = a*b;

break;

case '/': result = a/b;

break;

push(result);

pEval = pop();

printf("\nThe postfix evaluation is: %d\n",pEval);

getch();

}
void push(int n)

if (top < SIZE -1)

stack[++top] = n;

else

printf("Stack is full!\n");

exit(-1);

int pop()

int n;

if (top > -1)

n = stack[top];

stack[top--] = -1;

return n;

else

printf("Stack is empty!\n");

exit(-1);

}
Output:

Enter a postfix expression: 456*+

Pushed element is: 4

Pushed element is: 5

Pushed element is: 6

Poped element is 6

Poped element is 5

Poped element is 30

Poped element is 4

The postfix evaluation is: 34.


4] Design, develop and execute a program in C to simulate the working of queue of integers using
pointers. Provide the following operations: a. Insert b. Delete c. Display

#include<stdio.h>

#define n 5

void main()

int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;

clrscr();

printf("Queue using Array");

printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");

while(ch)

printf("\nEnter the Choice:");

scanf("%d",&ch);

switch(ch)

case 1:

if(rear==x)

printf("\n Queue is Full");

else

printf("\n Enter no %d:",j++);

scanf("%d",&queue[rear++]);

break;

case 2:

if(front==rear)

printf("\n Queue is empty");

}
else

printf("\n Deleted Element is %d",queue[front++]);

x++;

break;

case 3:

printf("\nQueue Elements are:\n");

if(front==rear)

printf("\n Queue is Empty");

else

for(i=front; i<rear; i++)

printf("%d",queue[i]);

printf("\n");

break;

case 4:

exit(0);

default:

printf("Wrong Choice: please see the options");

getch();

}
Output:

Queue using Array

1.Insertion

2.Deletion

3.Display

4.Exit

Enter the Choice:1

Enter no 1:15

Enter the Choice:1

Enter no 2:16

Enter the Choice:3

Queue Elements are:

15

16

Enter the Choice:2

Deleted Element is 15

Enter the Choice:3

Queue Elements are:

16
5] Design, develop and execute a C program to demonstrate how jobs are executed in CPU using
queue. Assume all jobs which are submitted to CPU for execution are stored in a queue and each
job will be executed in first come first serve manner. In this process also calculate waiting time of
a job before being executed by the CPU. An example is mentioned below.

#include<stdio.h>

#define n 5

void main()

int queue[n][3],ch=1,front=0,rear=0,i,j=1,x=n, r=0;

int f, wt[n];

clrscr();

printf("Queue using Array");

printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");

while(ch)

printf("\nEnter the Choice:");

scanf("%d",&ch);

switch(ch)

case 1:

if(rear==x)

printf("\n Queue is Full");

else

printf("\n Enter process id, Burst time and Arrival time \n");

r=rear++;

scanf("%d %d %d",&queue[r][0], &queue[r][1], &queue[r][2]);

break;

case 2:

if(front==rear)

{
printf("\n Queue is empty");

else

f=front++;

printf("\n Executed process ID is %d\n",queue[f][0]);

x++;

if(f==0)

wt[0]=0;

else

wt[f]=wt[f-1]+queue[f-1][1]-queue[f][2];

printf(" WT= %d", wt[f]);

break;

case 3:

printf("\n Queue Elements are:\n");

if(front==rear)

printf("\n Queue is Empty");

else

printf(" P_Id\tBT\tAT\n");

for(i=front; i<rear; i++)

for(j=0;j<3;j++)

printf(" %d\t",queue[i][j]);

printf("\n");

break;

case 4:

exit(0);
default:

printf(" Wrong Choice: please see the options");

getch();

Output:

You might also like