C Pracs
C Pracs
C Pracs
Practical File
INDEX
S.No Program REMARK
1.
Program to implement bubble sort, insertion sort, and selection sort.
Take the input from a file and compare their execution time
2. Program to implement stacks as array
3. Program to implement sorted linked list
4. Program to reverse a given linked list
5. Program to divide a linked list into two part
6. Program to convert Infix expression to postfix expression
7. To make damped pendulum using graphics
8. To demonstrate screensaver of moving ball using graphics.h
/*
** Program to time elementary sorting algorithm
*/
#include<stdio.h>
#include<time.h>
#define size 100
void randomArray(int a[],int n); // Generates an array of random numbers
void bubbleSort(int a[],int n); // Bubble Sorts given array
void insertionSort(int a[],int n); // Insertion Sorts given array
void selectionSort(int a[],int n); // Selection Sorts given array
int main(void) {
int i, a[size], temp, n, arr[100];
clock_t start;
FILE *fp = fopen("array.txt","r");
if(!fp) { // Auto create a file if doesn't exist
fp = fopen("array.txt","w");
randomArray(a,(n = rand()%50 + 1));
for(i = 0;i < n;i++)
fprintf(fp,"%d ",a[i]);
}
fclose(fp);
fp = fopen("array.txt","r");
i = 0;
printf("\n\nUnsorted Array : ");
while(!feof(fp)) {
fscanf(fp,"%d",&temp);
printf("%d ",(a[i++] = temp));
}
n = i;
fclose(fp);
start = clock(); bubbleSort(a,n); printf(" , time : %g s",((clock()-start)/CLK_TCK));
start = clock(); insertionSort(a,n); printf(" , time : %g s",((clock()-start)/CLK_TCK));
start = clock(); selectionSort(a,n); printf(" , time : %g s",((clock()-start)/CLK_TCK));
getch();
return 0;
}
void randomArray(int a[],int n) {
int i;
for(i = 0;i < n; i++)
a[i] = (rand()%100 - 50);
return;
}
void bubbleSort(int a[],int n) {
int b[n],i,j;
for(i = 0;i < n;i++) { // Copying array values
b[i] = a[i];
}
// Sorting algorithm
for(i = 0;i < n;i++) {
for(j = 0;j < n-1;j++)
if(b[j] > b[j+1])
b[j] = b[j+1] + (b[j+1] = b[j]) - b[j];
}
// Printing values
printf("\nBubble Sorted Array : ");
for(i = 0;i < n;i++) {
printf("%d ",b[i]);
}
return;
}
void insertionSort(int a[],int n) {
int b[n],i,j;
// Copying array values
for(i = 0;i < n;i++) {
b[i] = a[i];
}
// Sorting algorithm
for(i = 1;i < n;i++) {
j = i;
while(j > 0 && b[j-1] > b[j]) {
b[j-1] = b[j] + (b[j] = b[j-1]) - b[j-1];
j--;
}
}
// Printing values
printf("\nInsertion Sorted Array : ");
for(i = 0;i < n;i++) {
printf("%d ",b[i]);
}
return;
}
void selectionSort(int a[],int n) {
int b[n],i,j,pos;
// Copying array values
for(i = 0;i < n;i++) {
b[i] = a[i];
}
// Sorting algorithm
for(i = 0;i < n;i++) {
pos = i;
for(j = i;j < n;j++)
if(b[j] < b[pos])
pos = j; // Finding minimum
if(i != pos)
b[i] = b[pos] + (b[pos] = b[i]) - b[i];
}
// Printing values
printf("\nSelection Sorted Array : ");
for(i = 0;i < n;i++) {
printf("%d ",b[i]);
}
return;
}
/*
** Program to convert infix expression into postfix
*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
struct stack {
int top;
int items[MAX];
};
typedef struct stack sta;
int empty(sta *ps) {
if(ps->top==-1)
return 1;
else
return 0;
}
int pop(sta *ps) {
if(empty(ps)) {
printf("%s","Stack Underflow\n" );
exit(0);
}
return(ps->items[ps->top--]);
}
void push(sta *ps,int x) {
if(ps->top==MAX-1) {
printf("%s","stackoverflow\n");
return;
}
ps->items[++(ps->top)]=x;
return;
}
int stacktop(sta *ps) {
return(ps->items[ps->top]);
}
int isDigit(int c) {
if(c >= '0' && c <= '9')
return(1);
else
return(0);
}
int prcd(int a,int b) {
if((a=='*'||a=='/') && (b=='+'||b=='-'))
return(1);
else if((a=='*'||a=='/') && (b=='*'||b=='/'))
return(1);
else if((a=='+'||a=='-') && (b=='+'||b=='-'))
return(1);
else if(a=='(')
return(0);
else if(a!=')' && b=='(')
return(0);
else if(a!='(' && b==')')
return(1);
else
return (0);
}
void infixtopostfix(char expr[]) {
sta s;
s.top = -1;
int c,position;
int i=0,k;
int a[MAX];
for(position=0;(c = expr[position])!='\0';position++) {
if(isDigit(c)) {
a[i++]=c;
} else {
while(!empty(&s) && prcd(stacktop(&s),c)) {
a[i++]=pop(&s);
}
if(c!=')') {
push(&s,c);
} else {
pop(&s);
}
}
}
while(s.top!=-1)
a[i++]=pop(&s);
for(k=0;k<i;k++)
printf("%c",a[k]);
return;
}
int main(void) {
char ch[100];
int pos = 0;
while((ch[pos++]=getchar())!='\n');
ch[--pos]='\0';
printf("Infix Expression : %s\nPostfix Expression : ",ch);
infixtopostfix(ch);
getch();
return 0;
}
/*
** Program to show the motion of a damped pendulum using
graphics.h
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
int main(void) {
int gd = DETECT, gm;
double ballCoordinates[2], centreCoordinates[2], ballAngle, threadLength = 75;
double ballRadius = 25, timePeriod = 4000, amplitude = M_PI/3, t, dampingCoefficient =
0.00009;
centreCoordinates[0] = 300;
centreCoordinates[1] = 200;
initgraph(&gd, &gm, "C:\\TC");
while(!kbhit()) {
cleardevice();
// Setting the angular position of the ball
ballAngle = amplitude * exp(-1 * dampingCoefficient * t) * cos((2 * M_PI /
timePeriod) * t);
//Updating the coordinates of the ball
ballCoordinates[0] = centreCoordinates[0] + threadLength * sin(ballAngle);
ballCoordinates[1] = centreCoordinates[1] + threadLength * cos(ballAngle);
// Drawing the ball and the thread
circle(ballCoordinates[0], ballCoordinates[1], ballRadius);
line(centreCoordinates[0], centreCoordinates[1], ballCoordinates[0],
ballCoordinates[1]);
t += 1000/50; // Incrementing the time
delay(1000/50); // Refresh rate
}
closegraph();
return 0;
}
/*
** Program to make a screensaver with a ball bouncing back
from the ends of the screen
*/
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>
void *ball;
void image() {
setcolor(BLUE);
setfillstyle(SOLID_FILL,RED);
fillellipse(20,20,20,20);
ball=malloc(imagesize(0,0,40,40));
getimage(0,0,40,40,ball);
cleardevice();
return;
}
int main(void) {
int gm, gd = DETECT;
int l = getmaxx()/2,t = getmaxy()/2;
int x = 1,y = 1;
int xstep = 1,ystep = 1;
initgraph(&gd,&gm,"C:\\TC");
image();
setbkcolor(LIGHTGRAY);
while(!kbhit()) {
putimage(l,t,ball,XOR_PUT);
delay(1000/60);
cleardevice();
if(l >= getmaxx() - 40 || l <= 0) {
x *= -1;
xstep = x*(random(4) + 1);
ystep = y*(random(4) + 1);
l = l <= 0?0:getmaxx() - 40;
}
if(t >= getmaxy() - 40 || t <= 0) {
y *= -1;
ystep = y*(random(4) + 1);
xstep = x*(random(4) + 1);
t = t <= 0?0:getmaxy() - 40;
}
l += x + xstep;
t += y + ystep;
}
closegraph();
}
/*
** Program to implement stacks as array
*/
#include<stdio.h>
#define maxSize 100
struct stack {
int a[maxSize];
int tail;
int size;
}s1;
void push(int); // Inserts an element
void pop(void); // Deletes top element
void top(void); // Shows top element
void showAll(void); // Shows all elements from top to bottom
int main (void) {
char ch;
int n,stackSize;
printf("Enter size of array : ");
scanf("%d",&stackSize);
s1.size = stackSize;
s1.tail = -1;
do {
printf("\n\nA - Push B - Pop C - Top D - Show All E - End\nChoice : ");
printf("%c",ch = getch());
switch(ch) {
case 'A' : printf("\nEnter the value to push : "); scanf("%d",&n);
push(n);
break;
case 'B' : pop();
break;
case 'C' : top();
break;
case 'D' : showAll();
break;
case 'E' : break;
default : printf("Illegal option");
break;
}
} while(ch != 'E');
getch();
return;
}
void push(int n) {
if(s1.tail == -1) {
s1.a[0] = n;
s1.tail = 0;
}
else if(s1.tail+1 == s1.size)
printf("\nSTACK_OVERFLOW");
else
s1.a[++s1.tail] = n;
return;
}
void pop(void) {
if(s1.tail == -1)
printf("\nSTACK_UNDERFLOW");
else
s1.tail--;
return;
}
void top(void) {
if(s1.tail == -1)
printf("\nSTACK_UNDERFLOW");
else
printf("\n%d",s1.a[s1.tail]);
}
void showAll(void) {
if(s1.tail == -1)
printf("\nSTACK_UDNERFLOW");
else {
int i;
printf(\n);
for(i = s1.tail;i >= 0;i--)
printf("%d ",s1.a[i]);
}
return;
}
/*
** Program to implement sorted linked list
#include<stdio.h>
#include<stdlib.h>
struct node{
int value;
struct node* next;
}*head,*t;
struct node* createNode(int n); // Creates memory for node pointer and returns the address
void push(struct node*,int); // Inserts value at the end for given pointer address
void pop(struct node*); // Removes the last (tail) element and moves tail pointer a step back
void showAll(struct node*); // Traverses the linked list and shows all elements till it reaches ->next == NULL
int isNextNull(struct node*); // Helper function ,returns 1 if next is NULL, 0 if not
int main() {
char ch;
int n;
head = createNode(0); // Not part of stack
do {
printf("\n\nA - Push B - Pop C - Show All D - End\nChoice : ");
printf("%c",ch = getch());
switch(ch) {
case 'A' : printf("\nEnter the value to push : "); scanf("%d",&n);
push(head,n);
break;
case 'B' : pop(head);
break;
case 'C' : showAll(head);
break;
case 'D' : break;
default : printf("\nIllegal option");
break;
}
} while(ch != 'D');
free(head);
free(t);
getch();
return 0;
}
struct node* createNode(int n) {
struct node* p = (struct node*)malloc(sizeof(struct node*));
p->next = NULL;
p->value = n;
return p;
}
void push(struct node* p,int x) {
struct node* toBeInserted = createNode(x);
if(isNextNull(p)) { // Empty stack
p->next = toBeInserted;
}
else {
int flag = 0;
t = p;
while(t->next != NULL) {
if(x < t->next->value) {
toBeInserted->next = t->next;
t->next = toBeInserted;
flag++;
break;
}
t = t->next;
}
if(!flag) {
t->next = toBeInserted;
}
}
return;
}
void pop(struct node* p) {
if(isNextNull(p))
printf("\nUNDERFLOW");
else if(isNextNull(p->next)) {
free(p->next);
p->next = NULL;
}
else {
t = p;
while(t->next->next != NULL) {
t = t->next;
}
free(t->next);
t->next = NULL;
}
return;
}
void showAll(struct node* p) {
if(isNextNull(p))
printf("\nUNDERFLOW");
else {
t = p->next;
printf("\nLinked List : ");
while(t != NULL) {
printf("%d ",t->value);
t = t->next;
}
}
return;
}
int isNextNull(struct node* p) {
if(p->next == NULL)
return 1;
else
return 0;
}
/*
** Program to reverse a linked list (greedy)
#include<stdio.h>
#include<stdlib.h>
#define INSERT_AT_TAIL 1
#define INSERT_AT_HEAD 0
struct node{
int value;
struct node* next;
}*ll1,*ll2,*t;
struct node* createNode(int n); // Creates memory for node pointer and returns the address
void push(struct node*,int,int); // Inserts value at the end for given pointer address
void showAll(struct node*); // Traverses the linked list and shows all elements till it reaches ->next == NULL
int isNextNull(struct node*); // Helper function ,returns 1 if next is NULL, 0 if not
int main() {
char ch;
int n;
ll1 = createNode(0); // Not part of stack
ll2 = createNode(0);
do {
printf("\n\nA - Push B - Show Normal C - Show Reversed D - End\nChoice : ");
printf("%c",ch = getch());
switch(ch) {
case 'A' : printf("\nEnter the value to push : "); scanf("%d",&n);
push(ll1,n,INSERT_AT_TAIL); push(ll2,n,INSERT_AT_HEAD);
break;
case 'B' : showAll(ll1);
break;
case 'C' : showAll(ll2);
break;
case 'D' : break;
default : printf("\nIllegal option");
break;
}
} while(ch != 'D');
free(ll1);
free(ll2);
free(t);
getch();
return 0;
}
struct node* createNode(int n) {
struct node* p = (struct node*)malloc(sizeof(struct node*));
p->next = NULL;
p->value = n;
return p;
}
void push(struct node* p,int x,int type) {
struct node* toBeInserted = createNode(x);
if(isNextNull(p)) { // Empty stack
p->next = toBeInserted;
}
else {
if(type == INSERT_AT_TAIL) {
t = p;
while(t->next != NULL) {
t = t->next;
}
t->next = toBeInserted;
}
else if(type == INSERT_AT_HEAD) {
toBeInserted->next = p->next;
p->next = toBeInserted;
}
}
return;
}
void showAll(struct node* p) {
if(isNextNull(p))
printf("\nUNDERFLOW");
else {
t = p->next;
printf("\nLinked List : ");
while(t != NULL) {
printf("%d ",t->value);
t = t->next;
}
}
return;
}
int isNextNull(struct node* p) {
if(p->next == NULL)
return 1;
else
return 0;
}
/*
** Program to split a linked list into two on basis of length of
first half from head
#include<stdio.h>
#include<stdlib.h>
struct node{
int value;
struct node* next;
}*ll1,*ll2,*ll3,*t1,*t2,*t3;
struct node* createNode(int n); // Creates memory for node pointer and returns the address
void push(struct node*,int); // Inserts value at the end for given pointer address
void pop(struct node*); // Removes the last (tail) element and moves tail pointer a step back
void showAll(struct node*); // Traverses the linked list and shows all elements till it reaches -
>next == NULL
void splitBy(int,struct node*,struct node*,struct node*); // Splits 1st linked list in two based on length
int isNextNull(struct node*); // Helper function ,returns 1 if next is NULL, 0 if not
int main() {
char ch;
int n;
ll1 = createNode(0); // Not part of stack
ll2 = createNode(0);
ll3 = createNode(0);
do {
printf("\n\nA - Push B - Pop C - Split by Length D - Show All E- End\nChoice : ");
printf("%c",ch = getch());
switch(ch) {
case 'A' : printf("\nEnter the value to push : "); scanf("%d",&n);
push(ll1,n);
break;
case 'B' : pop(ll1);
break;
case 'C' : printf("\nEnter the length of first half (>0 and < %d): ",sizeOf(ll1)); scanf("%d",&n);
splitBy(n,ll1,ll2,ll3);
break;
case 'D' : printf("\nOriginal : "); showAll(ll1);
break;
case 'E' : break;
default : printf("\nIllegal option");
break;
}
} while(ch != 'E' && ch != 'C');
free(ll1);
free(ll2);
free(ll3);
free(t1);
free(t2);
free(t3);
getch();
return 0;
}
struct node* createNode(int n) {
struct node* p = (struct node*)malloc(sizeof(struct node*));
p->next = NULL;
p->value = n;
return p;
}
void splitBy(int x,struct node* p1,struct node* p2,struct node* p3) {
if(isNextNull(p1)) { // Empty stack
printf("\nUNDERFLOW");
return;
}
else {
int count = 0;
t1 = p1->next;
t2 = p2;
while(t1 != NULL) {
if(count < x) {
t2->next = createNode(t1->value);
t2 = t2->next;
}
else if(count > x) {
t3->next = createNode(t1->value);
t3 = t3->next;
}
else {
t3 = createNode(t1->value);
p3->next = t3;
}
t1 = t1->next;
count++;
}
printf("\nOriginal : "); showAll(ll1);
printf("\nList 1 : "); showAll(ll2);
printf("\nList 2 : "); showAll(ll3);
}
return;
}
void push(struct node* p,int x) {
struct node* toBeInserted = createNode(x);
if(isNextNull(p)) { // Empty stack
p->next = toBeInserted;
}
else {
t1 = p;
while(t1->next != NULL) {
t1 = t1->next;
}
t1->next = toBeInserted;
}
return;
}
void pop(struct node* p) {
if(isNextNull(p))
printf("\nUNDERFLOW");
else if(isNextNull(p->next)) {
free(p->next);
p->next = NULL;
}
else {
t1 = p;
while(t1->next->next != NULL) {
t1 = t1->next;
}
free(t1->next);
t1->next = NULL;
}
return;
}
void showAll(struct node* p) {
if(isNextNull(p))
printf("UNDERFLOW");
else {
t1 = p->next;
while(t1 != NULL) {
printf("%d ",t1->value);
t1 = t1->next;
}
}
return;
}
int isNextNull(struct node* p) {
if(p->next == NULL)
return 1;
else
return 0;
}
int sizeOf(struct node* p) {
if(isNextNull(p))
return 0;
else {
int count = 0;
t1 = p->next;
while(t1 != NULL) {
t1 = t1->next;
count++;
}
return count;
}
}