You are on page 1of 47

# ICE,6th Semester , 13106030

## DEPARTMENT OF INSTRUMENTATION AND CONTROL ENGINEERING

Dr. B R AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY
JALANDHAR 144011, PUNJAB (INDIA)
January-June, 2016

Submitted To:

Submitted By:

Jagreeti Kaur

Mayank Gupta

Asst. Professor

13106030
1

Dept. Of CSE

## 1. Objective : Sort an Array in Ascending Order

Program:
#include <stdio.h>
int main()
{

int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{

{

## if (number[i] > number[j])

{

a = number[i];
number[i] = number[j];
number[j] = a;

}
}

3

## for (i = 0; i < n; ++i)

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

Output:

## 2. Objective : Implement Sparsh Matrix Using Array

Program:
#include<stdio.h>
#include<conio.h>
int main()
{

int A[10][10],B[10][3],m,n,s=0,i,j;
printf("\nEnter the order m x n of the sparse matrix\n");
scanf("%d%d",&m,&n);
printf("\nEnter the elements in the sparse matrix(mostly zeroes)\n");
for(i=0;i<m;i++)

for(j=0;j<n;j++)
{

## printf("\n%d row and %d column: ",i,j);

scanf("%d",&A[i][j]);

}
}
printf("The given matrix is:\n");
for(i=0;i<m;i++)
5

## ICE,6th Semester , 13106030

for(j=0;j<n;j++)
{

printf("%d ",A[i][j]);

}
printf("\n");
}
for(i=0;i<m;i++)
{

for(j=0;j<n;j++)
{

if(A[i][j]!=0)
{

B[s][0]=A[i][j];
B[s][1]=i;
B[s][2]=j;
s++;

}
}
}
printf("\nThe sparse matrix is given by");
printf("\n");
for(i=0;i<s;i++)
{

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

printf("%d ",B[i][j]);

}
printf("\n");
6

}
getch();
}

Output:

## 3. Objective : To Search An Element In Array using Linear Search

Program:
#include<stdio.h>
int main()
{

## int array[100], search, c, n;

printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{

if (array[c] == search)

8

## DATA STRUCTURE AND ALGORITHMS

{
printf("%d is present at location %d.\n", search, c+1);
break;

}
if (c == n)
printf("%d is not present in array.\n", search);
return 0;
}
Output:
Case1.

Case2.

## 4. Objective: To Search An Element In Array using Binary Search

Program:
#include <stdio.h>
int main()
{

## int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
10

## for (c = 0; c < n; c++)

scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
return(0);
}
11

Output:

12

## 5. Objective : Matrix Multiplication 3*3

Program:
#include <stdio.h>
int main()
{
13

## ICE,6th Semester , 13106030

int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
14

## for (k = 0; k < p; k++)

{
sum = sum + first[c][k]*second[k][d]; }
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}

Output:

15

16

## 6. Objective : To perform Bubble Sort In Array

Program:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap
= array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}

Output:

17

## 7. Objective : To Perform Push and pop Operation on Stack

Program:
#include <stdio.h>
18

## ICE,6th Semester , 13106030

#include<conio.h>
#define MAX 5
int top, status;
void push (int stack[], int item)
{ if (top == (MAX-1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
int pop (int stack[])
{
int ret;
if (top == -1)
{ ret = 0;
status = 0;
}
else
{ status = 1;
ret = stack [top];
19

## ICE,6th Semester , 13106030

--top;
}
return ret;
}
void display (int stack[])
{ int i;
printf ("\nThe Stack is: ");
if (top == -1)
printf ("empty");
else
{ for (i=top; i>=0; --i)
printf ("\n--------\n|%3d |\n--------",stack[i]);
}
printf ("\n");
}
void main()
{
int stack [MAX], item;
int ch;
top = -1;

do
20

## ICE,6th Semester , 13106030

{ do
printf ("\n1.PUSH (Insert) in the Stack");
printf ("\n2.POP (Delete) from the Stack");
printf ("\n3.Exit (End the Execution)");
scanf ("%d", &ch);
if (ch<1 || ch>3)
printf ("\nInvalid Choice, Please try again");
} while (ch<1 || ch>3);
switch (ch)
{case 1:
printf ("\nEnter the Element to be pushed : ");
scanf ("%d", &item);
printf (" %d", item);
push (stack, item);
if (status)
{ printf ("\nAfter Pushing ");
display (stack);
if (top == (MAX-1))
printf ("\nThe Stack is Full");
}
21

## ICE,6th Semester , 13106030

else
printf ("\nStack overflow on Push");
break;
case 2:
item = pop (stack);
if (status)
{

## printf ("\nThe Popped item is %d. After Popping: ");

display (stack);

}
else
printf ("\nStack underflow on Pop");
break;
default:
printf ("\nEND OF EXECUTION");
}
}while (ch != 3);
getch();
}

22

Output:

23

## 8. Objective : Implement two Stacks In Single Array

Program:
#include <stdio.h>
#define SIZE 10
int ar[SIZE]; int top1 = -1; int top2 = SIZE;
void push_stack1 (int data)
{

## if (top1 < top2 - 1)

{ ar[++top1] = data; }
else
{ printf ("Stack Full! Cannot Push\n");

}
void push_stack2 (int data)
{

## if (top1 < top2 - 1)

{

ar[--top2] = data; }

## printf ("Stack Full! Cannot Push\n"); }

else

}
void pop_stack1 ()
{

if (top1 >= 0)
{ int popped_value = ar[top1--];
24

else

## { printf ("Stack Empty! Cannot Pop\n"); }

}
void pop_stack2 ()
{ if (top2 < SIZE)
{

## int popped_value = ar[top2++];

printf ("%d is being popped from Stack 2\n", popped_value); }

else
{

## printf ("Stack Empty! Cannot Pop\n"); }

}
void print_stack1 ()
{

int i;
for (i = top1; i >= 0; --i)
{ printf ("%d ", ar[i]); }
printf ("\n");

}
void print_stack2 ()
{

int i;
for (i = top2; i < SIZE; ++i)
{ printf ("%d ", ar[i]); }
printf ("\n");

}
25

int main()
{

## int ar[SIZE]; int i; int num_of_ele;

printf ("We can push a total of 10 values\n");
for (i = 1; i <= 6; ++i)
{

push_stack1 (i);
printf ("Value Pushed in Stack 1 is %d\n", i); }

## for (i = 1; i <= 4; ++i)

{

push_stack2 (i);
printf ("Value Pushed in Stack 2 is %d\n", i); }

print_stack1 ();

print_stack2 ();

## printf ("Pushing Value in Stack 1 is %d\n", 11);

push_stack1 (11);
num_of_ele = top1 + 1;
while (num_of_ele)
{

pop_stack1 ();

--num_of_ele; }

pop_stack1 ();
}

26

Output:

27

## 9. Objective : To change Expression Infix to Postfix

Program:

#include<stdio.h>

#include<conio.h>

## #define SIZE 100

int top = -1;

char stack[SIZE];

char pop();

## int is_operator(char symbol);

int precedence(char symbol);
void main()
{

int i,j;

## char infix_exp[SIZE], postfix_exp[SIZE];

char item,x;
printf("\nEnter Infix expression in parentheses: \n");
gets(infix_exp);
i=0; j=0; item=infix_exp[i++];
28

## DATA STRUCTURE AND ALGORITHMS

while(item != '\0')
{

if(item == '(')
{

push(item);

## else if((item >= 'A' && item <= 'Z') ||

(item >= 'a' && item <= 'z'))
{

postfix_exp[j++] = item;

else if(is_operator(item) == 1)
{

x=pop();

## while(is_operator(x) == 1 && precedence(x)

>= precedence(item))
{

postfix_exp[j++] = x;

x = pop();
push(x);

}
push(item);

}
else if(item == ')')
{

x = pop();

while(x != '(')
{

postfix_exp[j++] = x;

x = pop();

}
else
{

29

## DATA STRUCTURE AND ALGORITHMS

getch();

exit(0);

item = infix_exp[i++];
}
postfix_exp[j++] = '\0';
printf("\nArithmetic expression in Postfix notation: ");
puts(postfix_exp);
}
void push(char item)
{

{

## printf("\nStack Overflow. Push not possible.\n");

else
{

top = top+1;

stack[top] = item; }

}
char pop()
{

{

else
{

top = top-1;

return(item);
}
30

{

## if(symbol == '^' || symbol == '*' || symbol == '/' ||

symbol == '+' || symbol == '-')

return( 1);

return 0;

else
{
}
int precedence(char symbol)
{

if(symbol == '^')

return(3);

{

return(2);

{

return(1);

return(0);

else

31

Output:

32

## 10. Objective : To Create a Linked List

Program:

#include<stdio.h>
void create();

#include<stdlib.h>
void display();

struct node
{

int info;
struct node*next;

};

int main()
{

create();
33

## DATA STRUCTURE AND ALGORITHMS

display();

void create()
{

int ch,n;

struct node*p,*q;

scanf("%d",&ch);
while(ch>0)
{

scanf("%d",&n);

## p=(struct node*)malloc(sizeof(struct node));

p->info=n;

p->next=NULL;

if(first==NULL)
first=p;
else
{

q=first;
while(q->next!=NULL)
q=q->next;
q->next=p;

ch--; }

void display()
{

struct node*p;

p=first;

while(p!=NULL)
{

printf("%d\n",p->info); p=p->next;
}

}
34

Output:

35

## 11. Objective : To Delete a Node From a Linked List

Program:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
struct node
{

int data;
struct node* next;

};

36

## struct node* new_node = (struct node*) malloc(sizeof(struct node));

new_node->data = new_data;

= new_node;

}
{

## struct node *temp = head;

while(temp != NULL)
{

## printf("%d ", temp->data);

temp = temp->next;

}
void deleteNode(struct node *node_ptr)
{

node_ptr->data

= temp->data;

node_ptr->next

= temp->next;

free(temp);

int main()
{

## struct node* head = NULL;

printf("Before deleting \n");
37

getchar();
return 0;
}

Output:

38

39

## DATA STRUCTURE AND ALGORITHMS

Program:
#include<stdio.h> #include<conio.h>
struct node
{

int data;

## struct node *header, *ptr, *temp;

void insert_front();

void insert_end();

void insert_any();

void display();

void main()
{ int choice; int cont = 1;
header = (struct node *) malloc(sizeof(struct node));

while(cont == 1)
{

## printf("\n1. Insert at front\n");

printf("\n2. Insert at end\n");
printf("\n3. Insert at any position\n");
scanf("%d", &choice);

switch(choice)
{

## case 1: insert_front(); break;

case 2: insert_end(); break;
40

## case 3: insert_any(); break;

case 4: display();

break;

}
printf("\n\nDo you want to continue? (1 / 0): ");
scanf("%d", &cont);
}
}
void insert_front()
{

int data_value;
printf("\nEnter data of the node: ");
scanf("%d", &data_value);
temp = (struct node *) malloc(sizeof(struct node));
temp->data = data_value;

}
void insert_end()
{

int data_value;
printf("\nEnter data of the node: ");
scanf("%d", &data_value);
temp = (struct node *) malloc(sizeof(struct node));
41

## DATA STRUCTURE AND ALGORITHMS

temp->data = data_value;

}

}
void insert_any()
{

## int data_value, key;

printf("\nEnter data of the node: "); scanf("%d", &data_value);
printf("\nEnter data of the node after which new node is to be inserted: ");
scanf("%d", &key);
temp = (struct node *) malloc(sizeof(struct node));
while(ptr->link != NULL && ptr->data != key)

if(ptr->data == key)
{ temp->data = data_value;

else
}
void display()
{

42

Output:

43

## 13. Objective : Perform Operation Push and Pop in Queue

Program:
#include <stdio.h>
#include <conio.h>

#include <process.h>
#define QUEUE_SIZE 5

void main()
{

## void insert_rear(int, int *, int *);

void delete_front(int *, int *, int *);
void display(int *, int, int);
int choice, item, f, r, q[10];
f = 0; /* Front end of queue*/
r = -1; /* Rear end of queue*/
for (;;)
{

## printf("\t Ordinary Queue Operation\n\n");

printf("\t 1 . Push / Insert\n");
printf("\t 2 . Pop / Delete\n");
printf("\t 3 . View / Display\n");
printf("\t 4 . Exit\n\n\n");
printf("\t Enter the choice : "); scanf("%d", &choice);
44

switch (choice)
{

## case 1: printf("Enter the item to be inserted : "); scanf("%d", &item);

insert_rear(item, q, &r); continue;
case 2: delete_front(q, &f, &r);
case 3: display(q, f, r);

break;

break;

case 4: exit(0);
default:

## printf("\t\t\tInvalid Input Try Again");

} getch();
}
}
void insert_rear(int item, int q[], int *r)
{

{

## printf("\t\t\tQueue overflow\n"); return; }

q[++(*r)] = item;

}
void delete_front(int q[], int *f, int *r)
{

if (qempty(*f, *r))
{

## printf("\t\t\tQueue underflow\n"); return; }

printf(" Pop Successfull, element deleted = %d ",q[(*f)++]);

if(*f> *r)
{

*f=0,*r=-1; }

}
45

## void display(int q[], int f, int r)

{ int i;
if (qempty(f,r))
{

printf("Queue is empty\n");

return; }

## printf("\t\t\t Queue Container\n\n");

for(i=f;i<=r; i++)
printf("\t\t\t| %5d |\n",q[i]);
}
int qempty(int f, int r)
{

return (f>r)?1:0; }
int qfull(int r)

return (r==QUEUE_SIZE-1)?1:0;

46

Output :

47