DBMS - Lab Assignment - Week1-Week5 (Ap20110010597)
DBMS - Lab Assignment - Week1-Week5 (Ap20110010597)
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 main()
int n,i;
scanf("%d",&n);
FILE *fp;
fp = fopen("records.txt","w");
for(i=0;i<n;i++){
printf("Student: %d\n",i+1);
fflush(stdin);
fgets(a[i].name,sizeof(a[i].name),stdin);
fflush(stdin);
fgets(a[i].branch,sizeof(a[i].branch),stdin);
fflush(stdin);
fgets(a[i].rollNo,sizeof(a[i].rollNo),stdin);
fflush(stdin);
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("\nAfter Sorting\n\n");
qsort(b,n,sizeof(struct student),comparator);
for(i=0;i<n;++i){
}
fclose(fp);
OUTPUT:
DESCRIPTION OF WEEK-1:-
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()
int n,rn;
scanf("%d",&n);
FILE *FP;
FP = fopen("student.txt","w");
if(FP == NULL)
for(int i=0;i<n;i++)
fflush(stdin);
scanf("%s",&s[i].name);
scanf("%d",&s[i].rollNo);
scanf("%d",&s[i].age);
fclose(FP);
scanf("%d",&rn);
FP = fopen("student.txt","r");
for(int i=0;i<n;i++)
if(s[i].rollNo == rn)
time_spent += (double)(end-begin)/CLOCKS_PER_SEC;
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>
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++){
scanf("%d",&student[i].roll);
fflush(stdin);
scanf("%s",&student[i].name);
scanf("%s",&student[i].branch);
scanf("%d",&student[i].age);
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;
for( i=0;i<n;i++){
fclose(file);
char ch[max];
FILE *file1,*index;
file1=fopen("record.txt","r");
index=fopen("index.txt","w");
int arr1[n];
if(index==NULL)
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
scanf("%d",&ns);
for( j=0;j<ns;j++)
int num;
scanf("%d",&num);
low = 0;
high = n - 1;
mid = (low+high)/2;
low = mid + 1;
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;
printf("Not found\n");
fclose(index);
end=clock();
time=(end - start)/CLOCKS_PER_SEC;
return 0;
OUTPUT:
3.2 CODE:
#include<stdio.h>
#include<string.h>
#include<time.h>
int n=0;
while(low<=high)
int mid=(low+high)/2;
if(strcmp(rollf,arr[mid])==0)
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){
else{
while(fgets(line,MAX_LINE_LENGTH,fp3)){
n++;
char new_arr[n][20];
if(fp1==NULL){
}
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();
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;
Output:
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;
};
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)
else
while(fgets(Fline1,500,fp1))
{
n++;
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();
scanf("%d",&roll);
for(i=0;i<n;i++){
if(roll==RollNo[i]){
flag=1;
index=i;
if(flag==1){
printf("%d %s %s %d",s[index].RollNo,s[index].Name,s[index].Branch,s[index].Age);
else
endtime=clock();
double time=(double)(endtime-starttime)/CLOCKS_PER_SEC;
if (new_node != NULL)
if (root == NULL)
root = new_node;
fprintf(fp2,"%d\n", data);
return;
}
struct node *temp = root;
prev = temp;
temp = temp->right;
else
temp = temp->left;
prev->right = new_node;
else
prev->left = new_node;
fprintf(fp2,"%d\n", data);
if (new_node == NULL)
return NULL;
}
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
OUTPUT:
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 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)
else
{
while(fgets(Fline1,500,fp1))
n++;
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;
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);
return key % n;
OUTPUT:
597
900
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.