ASIA PACIFIC INSTITUTE OF INFORMATION
TECHNOLOGY
APIIT Diploma Part II
INCOURSE ASSIGNMENT
DATABASE AND DATA STRUCTURES
Prepared By
A.N.Ahamed Nishadh (CB004081)
S.D.Ilangakoon (CB004041)
A.C.Tiffaniya Fernando (CB004165)
Taiyaba T. Ahmed(CB004119)
Module Code & Title
AAPP001-3-2-DBDS
Cohort
DF10A1ICT
Date of Submission
05th May 2011
Instructor
Mr. Udesh Amarasinghe
Submitted in partial fulfillment for the degree of
Bachelor of Science (Hons) in Computing
i
ACKNOWLEDGEMENTS
Firstly we would like to thank our lecturer Mr.Udesh Amarasinghe for all the help
and guidance given to us while doing this assignment. Especially for teaching us
this module in such a way that we were able to learn this highly technical module
very easily.
Also there are many individuals who have helped us in numerous ways directly
and indirectly so that we were able to complete this assignment.
APIIT Lanka for providing us with resources and the Tech Team at APIIT Lanka
for their assistance at required times.
And last but not least our friends, parents and the well-wishers without whose
moral support and encouragement, we would not have been able to do a good job.
Finally, if there are any shortcomings in this project, then we request to excuse us
for all those and accept this documentation.
Ahamed Nishadh
Deshan Ilangakoon
Tiffaniya Fernando
Taiyaba Ahmed
ii
TABLE OF CONTENTS
1.0 - INTRODUCTION ............................................................................................... 1
1.1 – SECTION A: DATA STRUCTURE .......................................................... 1
1.2 – SECTION B: DYNAMIC MEMORY ALLOCATION AND LINK LIST1
2.0 – SOURCE CODE ................................................................................................. 2
2.1 – SECTION A ................................................................................................ 2
2.2 – SECTION B ................................................................................................ 8
2.2.1 – PART A ................................................................................................ 8
2.2.2 – PART 2 .............................................................................................. 10
3.0 – ASSUMPTIONS ............................................................................................... 13
4.0 – AGREED WORK PERCENTAGE ................................................................... 14
1
1.0 - INTRODUCTION
We were asked to implement two applications using C programming concepts.
Part A appreciates the use and application of data structure while part B uses
dynamic memory and link list.
1.1 – SECTION A: DATA STRUCTURE
This application enables the user to run a parking system which has a single lane
to hold up to a maximum of 10 vehicles. The arrival and the departure times are
being noted and a receipt is being issued after arrival and before departing. The
receipt includes the total charges, deducted rate, duration and number of times
vehicle has been moved. The application was developed using linear queues.
1.2 – SECTION B: DYNAMIC MEMORY ALLOCATION AND
LINK LIST
This section includes two parts.
The first uses the round robin scheduling algorithm to display the order in which
an accepted list of tasks will be executed.
The second part operates on first in first out basis where it will display the waiting
times of a list of jobs.
2
2.0 – SOURCE CODE
2.1 – SECTION A
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define max 10
typedef struct tkt {
char vplnum[10];
char vtype;
int timein;
int timeout;
char status;
int hrs;
float totamnt;
int moved;
int position;
};
typedef struct queue{
tkt vehicle[max];
int front;
int rear;
};
void newvehicle(queue *);
void modify(queue *);
void insert(queue *, char,int,char);
void payment(queue *, int);
void receipt(queue *,int);
void remove(queue *, int);
void viewvehicles(queue *);
main()
{
queue q;
q.front=0;
q.rear=-1;
int opt;
do
{
3
clrscr();
printf(" PREMADASA PARKING GARAGE");
printf("\n ========================\n\n\n");
printf("SELECT ACTION TO PERFORM");
printf("\n1. New Vehicle\n2. Modify Entry\n3. View Vehicles\n4.
Exit\n\n YOUR CHOICE : ");
fflush(stdin);
scanf("%d",&opt);
switch(opt)
{
case 1:
{
newvehicle(&q);
break;
}
case 2:
{
modify(&q);
break;
}
case 3:
{
viewvehicles(&q);
break;
}
case 4:
{
_exit(1);
break;
}
default:
{
printf("\n INVALID CHOICE!!!\n PRESS
ANY KEY TO CONTINUE");
}
}
getch();
}while (opt<=4||opt>0);
}
void insert(queue *qu, char vnum[10], int tin, char vet)
{
if(qu->rear<max)
{
4
qu->rear++;
strcpy(qu->vehicle[qu->rear].vplnum,vnum);
qu->vehicle[qu->rear].timein = tin;
qu->vehicle[qu->rear].vtype = vet;
qu->vehicle[qu->rear].status = 'A';
qu->vehicle[qu->rear].position=qu->rear+1;
qu->vehicle[qu->rear].moved =0;
}
else
{
printf("\nPARKING IS FULL");
}
}
void newvehicle(queue *q2)
{
if(q2->rear<max)
{
char lnum[10],status,vt;
int in,pos2;
clrscr();
printf(" PREMADASA PARKING GARAGE");
printf("\n ========================\n\n\n");
printf(" ENTER NEW VEHICLE DETAILS ");
printf("\n\n 1. Enter Vehicle Type[c/v] : ");
fflush(stdin);
scanf("%c",&vt);
printf("\n\n 1. Licence Plate Number : ");
fflush(stdin);
scanf("%s",&lnum);
printf("\n 2. Enter Time In : ");
fflush(stdin);
scanf("%d",&in);
insert(q2,lnum,in,vt);
receipt(q2,q2->rear);
}
else
{
printf("\nPARKING IS FULL");
}
}
void modify(queue *q3)
{
5
char lpnum[10];
int compres=0,pos=-1,i;
if(q3->rear>=q3->front)
{
clrscr();
printf(" PREMADASA PARKING GARAGE");
printf("\n ========================\n\n\n");
printf(" MODIFY VEHICLE DETAILS \n\n");
printf("Enter License Plate Number to Search : ");
scanf("%s",lpnum);
do
{
pos++;
compres=strcmp(q3->vehicle[pos].vplnum,lpnum);
}while(compres!=0);
if (compres==0)
{
clrscr();
printf(" PREMADASA PARKING GARAGE");
printf("\n ========================\n\n\n");
printf(" MODIFY VEHICLE DETAILS \n\n");
printf("1. License Plate Number : %s \n",q3-
>vehicle[pos].vplnum);
printf("2. Time In : %d\n",q3->vehicle[pos].timein);
printf("\n3. Enter Time Out : ");
fflush(stdin);
scanf("%d",&q3->vehicle[pos].timeout);
q3->vehicle[pos].hrs = (q3->vehicle[pos].timeout - q3-
>vehicle[pos].timein);
if(q3->vehicle[pos].position > 1)
{
for(i=0;i<(q3->vehicle[pos].position-1);i++)
{
q3->vehicle[i].moved++;
}
}
q3->vehicle[pos].status = 'D';
payment(q3,pos);
receipt(q3,pos);
remove(q3,pos);
}
else
{
printf("\n\n SPECIFIC VEHICLE NOT AVAILABLE");
}
6
}
else
{
printf("\n\n NO VEHICLES IN THE PARK");
}
}
void remove(queue *q4, int posi)
{
if(q4->rear>q4->front)
{
while(posi<=q4->rear)
{
q4->vehicle[posi]=q4->vehicle[posi+1];
posi++;
}
q4->rear--;
if(q4->rear==1)
{
q4->rear=-1;
}
}
else
{
printf("CAR PARK IS EMPTY");
}
}
void receipt(queue *q5, int pos1)
{
clrscr();
printf(" PREMADASA PARKING GARAGE");
printf("\n ========================\n\n\n");
if(q5->vehicle[pos1].status == 'A')
{
printf(" PREMADASA PARKING GARAGE");
printf("\n ========================\n\n\n");
printf("ENTRANCE TICKET\n\n");
printf("1. License Plate Number : %s \n",q5-
>vehicle[pos1].vplnum);
printf("2. Time In : %d\n",q5->vehicle[pos1].timein);
printf("3. Slot No : %d\n",q5->vehicle[pos1].position);
printf("\n PARKING FEES");
if(q5->vehicle[pos1].vtype='c')
{
printf("\nFirst 2 Hours : Rs. 50.00 ");
printf("\nEach Additional Hour : Rs. 20.00");
7
}
else
{
printf("\nFirst 2 Hours : Rs. 60.00 ");
printf("\nEach Additional Hour : Rs. 25.00");
}
}
if(q5->vehicle[pos1].status == 'D')
{
printf(" PREMADASA PARKING GARAGE");
printf("\n ========================\n\n\n");
printf("RECEIPT\n\n");
printf("1. License Plate Number : %s \n",q5-
>vehicle[pos1].vplnum);
printf("2. Time In : %d\n",q5->vehicle[pos1].timein);
printf("3. Time Out : %d\n",q5->vehicle[pos1].timeout);
printf("4. Hours Parked : %d\n",q5->vehicle[pos1].hrs);
printf("5. Times Moved : %d\n",q5-
>vehicle[pos1].moved);
printf("6. TOTAL PAYABLE : %.2f\n",q5-
>vehicle[pos1].totamnt);
}
}
void payment(queue *q6,int pos3)
{
if(q6->vehicle[pos3].vtype='c')
{
if(q6->vehicle[pos3].hrs<2)
{
q6->vehicle[pos3].totamnt=50;
}
else
{
q6->vehicle[pos3].totamnt = ((q6->vehicle[pos3].hrs-
2)*20)+50;
}
}
if(q6->vehicle[pos3].vtype='v')
{
if(q6->vehicle[pos3].hrs<2)
{
q6->vehicle[pos3].totamnt=60;
}
else
{
q6->vehicle[pos3].totamnt = ((q6->vehicle[pos3].hrs-
2)*25)+60;
}
8
}
if(q6->vehicle[pos3].moved>10)
{
q6->vehicle[pos3].totamnt = (q6->vehicle[pos3].totamnt + (q6-
>vehicle[pos3].totamnt * 0.01));
}
}
void viewvehicles(queue *q7)
{
int i=0;
if(q7->rear>=q7->front)
{
printf("\tSLOT \tVEHICLE ID\tTIME IN\n");
for(i=0;i<=q7->rear;i++)
{
printf("\t%d\t%s\t\t%d\n",q7->vehicle[i].position,q7-
>vehicle[i].vplnum,q7->vehicle[i].timein);
}
}
else
{
printf("\n\n NO VEHICLES IN THE PARK");
}
}
2.2 – SECTION B
2.2.1 – PART A
#include<stdio.h>
#include<malloc.h>
#include <conio.h>
typedef struct link
{
int data;
char name;
link *next;
};
link *hd;
int count=0;
//insert funtion
void insert(int item, char n)
{
9
link *node = (struct link*) malloc (sizeof(struct link));
if (hd==NULL)
{
node->data=item;
node->name=n;
node->next=NULL;
hd=node;
}
else
{
link *temp;
for(temp = hd;temp->next != NULL;temp=temp->next);
temp->next=node;
node->data=item;
node->name=n;
node->next = NULL;
}
}
//execute function
void execute()
{
link *temp;
link *pre;
while (count>0)
{
for(pre=NULL,temp=hd; temp!=NULL;pre=temp,temp=temp-
>next)
{
if (temp->data>0)
{
printf("%c,\t", temp->name);
temp->data = temp->data - 4;
}
else
{
if(pre==NULL)
{
hd=temp->next;
free(temp);
count--;
}
else
{
if(temp->next==NULL)
{
pre=temp;
free(temp);
count--;
10
}
else
{
pre->next=temp->next;
free(temp);
count--;
}
}
}
}
}
}
main()
{
int timeQ,i;
char name;
i=1;
hd=NULL;
while (i==1)
{
clrscr();
printf("ROUND ROBIN ALGORITHUM");
printf("\n\nEnter Task Name: \t");
fflush(stdin);
scanf("%c",&name);
printf("\nEnter Task Burst Time: \t");;
fflush(stdin);
scanf("%d",&timeQ);;
insert(timeQ,name);
printf("To continue press: 1 or To Stop Press: 0\n");
fflush(stdin);
scanf("%d",&i);
count++;
}
printf("Order of Process Execution: ");
execute();
}
2.2.2 – PART 2
#include<stdio.h>
#include<malloc.h>
11
#include <conio.h>
typedef struct link
{
int time;
char name;
link *next;
};
link *hd;
void insert(int item, char n)
{
link *node = (struct link*) malloc (sizeof(struct link));
if (hd==NULL)
{
node->time=item;
node->name=n;
node->next=NULL;
hd=node;
}
else
{
link *temp;
for(temp = hd;temp->next != NULL;temp=temp->next);
temp->next=node;
node->time=item;
node->name=n;
node->next = NULL;
}
}
void execute()
{
link *temp;
int wait=0;
for(temp=hd; temp!=NULL;temp=temp->next)
{
printf("%c\t\t", temp->name);
printf("%d\n",wait);
wait=wait+temp->time;
}
}
main()
{
int timeQ,i;
char name;
12
i=1;
hd=NULL;
while (i==1)
{
clrscr();
printf("FIRST IN FIRST OUT ALGORITHUM");
printf("\n\nEnter Task Name: \t");
scanf("%c",&name);
flushall();
printf("\nEnter Task Burst Time: \t");;
scanf("%d",&timeQ);;
flushall();
insert(timeQ,name);
printf("To continue press: 1 or To Stop Press: 0\n");
scanf("%d",&i);
flushall();
}
printf("\nProcess Waiting time\n\n");
printf("Process Name \tWaiting Time\n");
execute();
}
13
3.0 – ASSUMPTIONS
1. The arrival time, only the hour when the vehicle arrived is recorded. It is
the same case with the departure time.
2. The time is inserted in the 24hr format clock.
14
4.0 – AGREED WORK PERCENTAGE
STUDENT NAME SECTION A SECTION B
Ahamed Nishadh 60%
Taiyaba Ahmed 40%
Deshan Illangakoon 60%
Tiffaniya Fernando 40%
I, hereby agree that the above members have undertaken the above tasks and their
work is reflective of what I have written above.
Group Leader : A.N.Ahamed Nishadh (CB004081)
Signature :