0% found this document useful (0 votes)
56 views23 pages

DBMS - Lab Assignment - Week1-Week5 (Ap20110010597)

The document describes a student's DBMS lab assignment over 3 weeks. Week 1 involves creating a data file to store student records with fields like roll number, name, etc. and sorting the records based on roll number. Week 2 involves storing student records in a file and performing linear search to find a student's details based on roll number input, calculating the time taken. Week 3 involves creating an index file with roll numbers and performing both linear and binary search on the index file to find student details by roll number from the data file, calculating search times.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views23 pages

DBMS - Lab Assignment - Week1-Week5 (Ap20110010597)

The document describes a student's DBMS lab assignment over 3 weeks. Week 1 involves creating a data file to store student records with fields like roll number, name, etc. and sorting the records based on roll number. Week 2 involves storing student records in a file and performing linear search to find a student's details based on roll number input, calculating the time taken. Week 3 involves creating an index file with roll numbers and performing both linear and binary search on the index file to find student details by roll number from the data file, calculating search times.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

DBMS LAB ASSIGNMENT

AP20110010574

D.S.ADITYA VASANTH

WEEK-1:-
Create a data file to store records of the students (fields: rollno, name, branch,age). (ii) Sort the
records of the file based on the rollno of the students. (iii) Perform external sorting procedure
(based on the roll number) on two data files which store records of the students and store the result
in to the third data file.

CODE:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct student{

char name[6];

char branch[4];

char rollNo[4];

char age[3];

};

int comparator(const void* p,const void* q){

return strcmp(((struct student*)p)->rollNo,((struct student*)q)->rollNo);

int main()

int n,i;

printf("Enter the no of students: ");

scanf("%d",&n);

struct student a[n],b[n];

FILE *fp;

fp = fopen("records.txt","w");
for(i=0;i<n;i++){

printf("Student: %d\n",i+1);

fflush(stdin);

printf("enter Name: ");

fgets(a[i].name,sizeof(a[i].name),stdin);

fflush(stdin);

printf("Enter Branch: ");

fgets(a[i].branch,sizeof(a[i].branch),stdin);

fflush(stdin);

Cse: printf("Enter RollNo: ");

fgets(a[i].rollNo,sizeof(a[i].rollNo),stdin);

fflush(stdin);

printf("Enter Age: ");

fgets(a[i].age,sizeof(a[i].age),stdin);

fwrite(a,sizeof(a),1,fp);

fclose(fp);

fp=fopen("records.txt","r");

fread(b,sizeof(b),1,fp);

printf("\nBefore Sorting\n\n");

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

printf("Name: %s\tBranch: %s\tRollNo: %s\tAge: %s\


n",b[i].name,b[i].branch,b[i].rollNo,b[i].age);

printf("\nAfter Sorting\n\n");

qsort(b,n,sizeof(struct student),comparator);

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

printf("Name: %s\tBranch: %s\tRollNo: %s\tAge: %s\


n",b[i].name,b[i].branch,b[i].rollNo,b[i].age);

}
fclose(fp);

OUTPUT:

DESCRIPTION OF WEEK-1:-

We have to create a text file by naming students_records.txt containing of n records. In this


program, we have to first need to create n structure variables. where n is no. of the recordings
containing in the file. This structure contains roll (character array of size 20), name (character array
of size 30), branch (character array of size 25), and age (integer type). For data sets in files, you need
to create n structure variables. now we need to create a record from student_records.txt and assign
it to a structure variable according to the fields. Now we have to sort the records in ascending order
based on roll number and write those records to a new file sorted_students_recordeds.txt.

week-2:-
Store student records (fields: rollno,name,branch,age) in a data file and perform linear search in the
data file by reading rollno as input and then display the student details and display the time required
to do this operation.

CODE:

#include<stdio.h>

#include<time.h>

#include<stdlib.h>

struct student

char name[20];

char branch[5];

int rollNo;

int age;

};

int main()

double time_spent = 0.0;

clock_t begin = clock();

int n,rn;

printf("Enter the no. of students: ");

scanf("%d",&n);

struct student s[n];

FILE *FP;

FP = fopen("student.txt","w");

if(FP == NULL)

printf("can't open file");

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

fflush(stdin);

printf("\t\t Enter name: ");

scanf("%s",&s[i].name);

printf("\t\t Enter branch: ");


scanf("%s",&s[i].branch);

printf("\t\t Enter rollNo: ");

scanf("%d",&s[i].rollNo);

printf("\t\t Enter age: ");

scanf("%d",&s[i].age);

fprintf(FP,"Name:%s\t\t Branch:%s\tRoll No:%d\tAge:%d\


n",s[i].name,s[i].branch,s[i].rollNo,s[i].age);

fclose(FP);

printf("Enter roll no: ");

scanf("%d",&rn);

FP = fopen("student.txt","r");

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

if(s[i].rollNo == rn)

printf("Name:%s\t\t Branch:%s\t\tRoll No:%d\tAge:%d\


n",s[i].name,s[i].branch,s[i].rollNo,s[i].age);

clock_t end = clock();

time_spent += (double)(end-begin)/CLOCKS_PER_SEC;

printf("\t\t The elapsed time is %f seconds \n\n",time_spent);

OUTPUT:
DESCRIPTION OF WEEK-2:-

we need to create a text file by naming them as students_records.txt containing of n records. In this
program, first need to create n structure variables. where n is no. of the recordings contained in the
file. This program consists of roll (character array of size 20), name (character array of size 30),
branch (character array of size 25), and age (integer type). For data sets in files,we need to create n
structure variables. Then read the records from sorted_student_records.txt and assign them to
structure variables according to the fields. To calculate the time it takes for the CPU to perform a
reel number lookup and display the corresponding student details, create two clock_t variables like
start_t and end_t to store the CPU time is needed. It indicates the total time taken to perform this
operation. Place start_t =clock() before taking input from the user to perform the search, and
end_t=clock() after performing the operation. It takes an integer variable flag=0. Read the role
number of the user to search for. Now perform a search operation by comparing the user input to
each part of the roll number in the structure variable.

WEEK-3:-
Store student records (fields: rollno,name,branch,age) in a data file and build an index file by
considering the rollno as the key.

1. Perform linear search in the index file by reading rollno as input and then display the student
details by reading from the data file and display the time required to do this operation.

2. Perform binary search in the index file (by sorting the index file based on the rollno) by reading
roll no as input and then display the student details by reading from the data file and display the
time required to do this operation.

1 CODE:-

#include<stdio.h>

#include<string.h>

#include<time.h>

#define max 100

struct record{

int roll;
char name[max];

char branch[max];

int age;

}student[max];

int main(){

clock_t start,end;

double time;

start=clock();

FILE *file;

file=fopen("record.txt","w");

int i,j;

int n;

printf("enter no of students:");

scanf("%d",&n);

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

printf("\n roll no: ");

scanf("%d",&student[i].roll);

fflush(stdin);

printf(" name: ");

scanf("%s",&student[i].name);

printf(" branch: ");

scanf("%s",&student[i].branch);

printf(" age: ");

scanf("%d",&student[i].age);

struct record temp;

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

for(i=0;i<n-1;i++){

if(student[j].roll<student[i].roll){

temp=student[j];

student[j]=student[i];
student[i]=temp;

fprintf(file,"ROLL_NUMBER NAME COURSE age\n");

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

fprintf(file, "%d %s %s %d\


n",student[i].roll,student[i].name,student[i].branch,student[i].age);

fclose(file);

char ch[max];

FILE *file1,*index;

file1=fopen("record.txt","r");

index=fopen("index.txt","w");

int arr1[n];

if(index==NULL)

printf("Can't open the file.\n");

else

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

arr1[i]=student[i].roll;

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

fprintf(index,"%d\n",arr1[i]);

fclose(file1);
fclose(index);

// FILE *index;

char check[100];

char num[100];

index=fopen("index","r");

int count,flag=0;

int ns,low,high,mid;

//Binary Search

printf("Enter the number of searches: ");

scanf("%d",&ns);

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

int num;

printf("Enter the roll number to be searched: ");

scanf("%d",&num);

low = 0;

high = n - 1;

mid = (low+high)/2;

while (low <= high) {

if(arr1[mid] < num)

low = mid + 1;

else if (arr1[mid] == num) {

printf("It is found\n");

printf("%d %s %s %d\
n",student[mid].roll,student[mid].name,student[mid].branch,student[mid].age);

break;

else

high = mid - 1;

mid = (low + high)/2;


}

if(low > high)

printf("Not found\n");

fclose(index);

end=clock();

time=(end - start)/CLOCKS_PER_SEC;

printf("time taken for execution %f seconds\n",time);

return 0;

OUTPUT:

DESCRIPTION OF WEEK 3.1:

we need to create a text file named students_records.txt containing n records. In this


program, we first need to create n structure variables. where n is no. of the recordings
contained in the file. This structure consists of roll (character array of size 20), name
(character array of size 30), branch (character array of size 25), and age (integer type).
For these data sets in files, you need to create an n structure variables. Then read the
records from sorted_student_records.txt and assign them to structure variables
according to the fields. To calculate the time it takes for the CPU to perform a reel
number lookup and display the corresponding student details, create two clock_t
variables like start_t and end_t to store the CPU time is needed. Indicates the total time
taken to perform this operation. Place start_t =clock() before taking input from the user
to perform the search, and end_t=clock() after performing the operation. You need to
create a new index file, write the record's roll number, and create AProll[n][20]. n is the
number of records in the student record file you created. Now perform a search
operation by comparing the user input to each part of the roll number in the structure
variable.

3.2 CODE:

#include<stdio.h>

#include<string.h>

#include<time.h>

#define MAX_LINE_LENGTH 500

int n=0;

int binary_search(char arr[][20],int low,int high,char rollf[])

while(low<=high)

int mid=(low+high)/2;

if(strcmp(rollf,arr[mid])==0)

printf("roll number found");

return mid;

else if(strcmp(rollf,arr[mid])>0)

return binary_search(arr,mid+1,high,rollf);

else

return binary_search(arr,low,mid-1,rollf);

}
int main()

clock_t start_t,end_t;

double total_t;

struct std1{

char roll[15];

char name[25],branch[20];

int age;

}temp1;

FILE *fp1,*fp2,*fp3,*fp4,*fp5;

char ch;

int i,j,flag=0;

char rollf[10];

char line[MAX_LINE_LENGTH];

fp1=fopen("StudentRecords_DBExercise_1.txt","r");

fp3=fopen("StudentRecords_DBExercise_1.txt","r");

if(fp3==NULL){

printf("fp3 not found");

else{

while(fgets(line,MAX_LINE_LENGTH,fp3)){

n++;

struct std1 sv[n];

char new_arr[n][20];

if(fp1==NULL){

printf("fp1 not found");

}
else{

for(i=1;i<=n;i++){

fscanf(fp1,"%s %s %s %d",sv[i].roll,sv[i].name,sv[i].branch,&sv[i].age);

fclose(fp1);

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

for(j=i+1;j<=n;j++)

if(strcmp(sv[i].roll,sv[j].roll)>0){

temp1=sv[i];

sv[i]=sv[j];

sv[j]=temp1;

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

strcpy(new_arr[i],sv[i].roll);

fp2=fopen("IndexFile2.txt","w");

for(i=1;i<=n;i++){

fprintf(fp2,"%s\n",sv[i].roll);

start_t=clock();

printf("Enter roll number:");

scanf("%s",rollf);

int index=binary_search(new_arr,1,n,rollf);

printf("\n%s %s %s %d",sv[index].roll,sv[index].name,sv[index].branch,sv[index].age);
end_t=clock();

total_t=(double)(end_t-start_t)/CLOCKS_PER_SEC;

printf("\nTotal cpu time:%f",total_t);

Output:

Enter roll number:597

roll number found

597 TEJA CSE 20

Total cpu time:3.386000

DESCRIPTION OF WEEK 3.2:

First we need to Sort the student records by ascending role number and then perform a binary search.
Then copy the roll number part of the structure variable to new_arr[n][20]. Open the new index file in
write mode and write the reel number. Executes a binary search with the reel number as input. Call the
binary search function with parameters new_arr,1,n,rollf (user input). In the binary search function, the
arguments are char arr[][20], int low, int high, char rollf[]. While low <= high, medium = (low +
high)/2. Returns middle if the given roll number is found in the middle of the index. If the given rollf is
less than the number of rolls at the midpoint of the index, perform a binary search between 0 and the
midpoint. Otherwise, do a binary search between middle + 1 and high. The value returned by the binary
search function is stored in the integer variable index. Shows the details corresponding to the roll
number using the index.

WEEK 4:
Store student records (fields: rollno,name,branch,age) in a data file and build an index file by using
binary search tree ( rollno is used as the key). i.Perform search in the index file by reading rollno as
input and then display the student details by reading from the data file and display the time required
to do this operation. ii.Add and delete the student records from the data file and then perform
corresponding modifications in the index file.

CODE:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>
#include<time.h>

FILE *fp2;

struct node

int data;

struct node *left;

struct node *right;

};

struct node *root = NULL;

struct node *create_node(int);

void insert(int data);

int main()

clock_t starttime,endtime;

struct std{

int RollNo;

int Age;

char Name[20];

char Branch[10];

};

int n=0,i,j,data;

char Fline1[500];

FILE *fp1,*fp3;

fp1=fopen("Dbms.txt","r");

if(fp1==NULL)

printf("file not found.");

else

while(fgets(Fline1,500,fp1))

{
n++;

struct std s[n],temp;

fp1=fopen("Dbms.txt","r");

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

fscanf(fp1,"%d %s %s %d",&s[i].RollNo,s[i].Name,s[i].Branch,&s[i].Age);

fp2=fopen("index.txt","w");

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

insert(s[i].RollNo);

fclose(fp2);

int RollNo[n];

fp3=fopen("index.txt","r");

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

fscanf(fp3,"%d",&RollNo[i]);

fclose(fp3);

int index,roll,flag=0;

starttime=clock();

printf("Enter Roll Number to check :");

scanf("%d",&roll);

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

if(roll==RollNo[i]){
flag=1;

index=i;

if(flag==1){

printf("The Searched Roll Number is found \n");

printf("%d %s %s %d",s[index].RollNo,s[index].Name,s[index].Branch,s[index].Age);

else

printf("The Searched Roll Number is Not Found");

endtime=clock();

double time=(double)(endtime-starttime)/CLOCKS_PER_SEC;

printf("\nTotal time in seconds :%d",time);

void insert(int data)

struct node *new_node = create_node(data);

if (new_node != NULL)

if (root == NULL)

root = new_node;

fprintf(fp2,"%d\n", data);

return;

}
struct node *temp = root;

struct node *prev = NULL;

while (temp != NULL)

prev = temp;

if (data > temp->data)

temp = temp->right;

else

temp = temp->left;

if (data > prev->data)

prev->right = new_node;

else

prev->left = new_node;

fprintf(fp2,"%d\n", data);

struct node *create_node(int data)

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

if (new_node == NULL)

printf("\nAll the nodes are completed. ");

return NULL;

}
new_node->data = data;

new_node->left = NULL;

new_node->right = NULL;

return new_node;

OUTPUT:

Enter Roll Number:597

Roll Number Found

597 TEJA CSE 20

Total cpu time:-171798692

DESCRIPTION OF WEEK 4:

Doing so inserts the reel number into the binary search tree and performs the search operation.
Define the other three functions as main functions to perform this task. Creates a new struct called
node with data members int data, struct *left, struct *right. Initialize the *root tree node to NULL.
Call the insert function, passing the roll number of each record. In the insert function, create a new
node by calling the create_node(data) function. When a new node is created and root=NULL. Assign
the created node to the root and write the data to the index file and return it. Create two structure
pointers: temp, prev and *temp=root, prev=*NULL. Here, we compare whether the left subtree of
the binary search tree contains values less than the value of the root node and whether the right
subtree contains values greater than the value of the root node, Find where to insert the new node.
After inserting the node, write the data to the index file. The create_node function creates a new
node by allocating memory for the structure pointer, inserting data into its data portion, assigning
the left and right pointers to NULL, and returning the new node address. The search function
performs a search by comparing the input value to the root node and traversing accordingly. If you
increment an index variable with this function, this function will return that index. If the specified
roll number is found, 0 is returned otherwise.

WEEK 5:
Store student records (fields: rollno,name,branch,age) in a data file and build an index file by using
hash table (rollno is used as the key here). iii.Perform search in the index file by reading rollno as
input and then display the student details by reading from the data file and display the time required
to do this operation. i.Add and delete the student records from the data file and then perform
corresponding modifications in the index file.

CODE:
#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<time.h>

struct dataItem

int key;

int data;

}ditem[6];

int hashCode(int key,int n);

int index=0;

int main()

clock_t start_t,end_t;

struct std{

int roll;

char name[20];

char branch[10];

int age;

};

int n=0,i,j,data;

char Fline1[500];

FILE *fp1,*fp3;

fp1=fopen("StudentRecords_DBExercise_1.txt","r");

if(fp1==NULL)

printf("fp1 not found.");

else

{
while(fgets(Fline1,500,fp1))

n++;

struct std sv[n],temp;

fp1=fopen("StudentRecords_DBExercise_1.txt","r");

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

ditem[i].key=0;

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

fscanf(fp1,"%d %s %s %d",&sv[i].roll,sv[i].name,sv[i].branch,&sv[i].age);

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

int k=hashCode(sv[i].roll,n),flag=0;

while(flag==0)

if(ditem[k].key==0)

ditem[k].key=sv[i].roll;

ditem[k].data=i;

flag=1;

else

k++;

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

printf("\n%d\n",ditem[i].key);

FILE *fp4;

fp4=fopen("HtIndex.txt","w");

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

fprintf(fp4,"%d\n",ditem[i].key);

int rollf;

printf("Enter roll number:");

scanf("%d",&rollf);

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

if(ditem[i].key==rollf)

printf("%d %s %s
%d",sv[ditem[i].data].roll,sv[ditem[i].data].name,sv[ditem[i].data].branch,sv[ditem[i].data].age);

int hashCode(int key,int n){

return key % n;

OUTPUT:

597

900

Enter roll number:597


597 TEJA CSE 20

DESCRIPTION OF WEEK 5:

First we need to create structure data item with data member int key, data, and structure variable
ditem[10]. Initialize the key element of the structure data element variable with 0. Now pass the roll
number in the parameter and store n in k to calculate the hash code. If the key value of the structure
variable ditem at index k is 0, insert the reel number, store the index of the std structure variable in
ditem[k].data, and update the flag with 1 to indicate that the insertion is complete. increase. If the
key value of the structure variable ditem at index k is not 0, increment k by 1. The hash code
function takes int key, int n as arguments and returns key%n.

You might also like