Assignment 1:- Searching Algorithms
a) Create a random array of n integers. Accept a value x from user and use linear search algorithm to check whether the number is present in the array or not and output the position if the number is present.
b) Accept n sorted values in array from user. Accept a value x from user and use binary search algorithm to check whether the number is present in sorted array or not and output the position if the number is present.
Set B
a) Read the data from file 'student.txt' containing names of student and their class (FY, SY, TY). Accept a student name from user and use linear search algorithm to check whether the name is present in the file and output the Class of student otherwise output “Student not in the list”.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define NAME_LEN 50
#define CLASS_LEN 5
// Structure to store student records
typedef struct {
char name[NAME_LEN];
char s_class[CLASS_LEN];
} Student;
int main() {
FILE *fp;
Student s[MAX_STUDENTS];
char search_name[NAME_LEN];
int n = 0;
int found = 0;
fp = fopen("student.txt", "r");
if (fp == NULL) {
printf("Error: Could not open file student.txt\n");
return 1;
}
while (fscanf(fp, "%s %s", s[n].name, s[n].s_class) != EOF) {
n++;
if (n >= MAX_STUDENTS) break;
}
fclose(fp);
printf("Enter the student name to search: ");
scanf("%s", search_name);
for (int i = 0; i < n; i++) {
// Perform a case-sensitive string match
if (strcmp(s[i].name, search_name) == 0) {
printf("Class: %s\n", s[i].s_class);
found = 1;
break; // Stop looking once the item is found
}
}
if (!found) {
printf("Student not in the list\n");
}
return 0;
}
b) Read the data from file 'student.txt' containing names of student and their class(FY, SY,TY). Accept a student name from user and use binary search algorithm to check whether the name is present in the file and output the class of student otherwise output “Student not in the list”.
Create Student.txt file
Amit FY
Kiran SY
Rahul TY
Zoya SY
Complete C Code Implementationc#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[50];
char s_class[10]; // Named s_class to avoid conflict with C++ keywords
} Student;
int read_file(Student s[]);
void sort_records(Student s[], int n);
void binary_search(Student s[], int n, char key[]);
int main() {
Student s[100];
int n;
char search_name[50];
n = read_file(s);
if (n == 0) {
printf("No records found or file error.\n");
return 1;
}
sort_records(s, n);
printf("Enter the name of the student to search: ");
scanf("%s", search_name);
binary_search(s, n, search_name);
return 0;
}
int read_file(Student s[]) {
FILE *fp;
int i = 0;
fp = fopen("student.txt", "r");
if (fp == NULL) {
printf("Error: Cannot open file 'student.txt'. Make sure it exists.\n");
return 0;
}
while (fscanf(fp, "%s %s", s[i].name, s[i].s_class) != EOF) {
i++;
}
fclose(fp);
return i; // Returns total number of students read
}
void sort_records(Student s[], int n) {
int i, j;
Student temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
// Compare names using strcmp
if (strcmp(s[j].name, s[j + 1].name) > 0) {
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
void binary_search(Student s[], int n, char key[]) {
int low = 0;
int high = n - 1;
int mid;
int found = 0;
while (low <= high) {
mid = (low + high) / 2;
int res = strcmp(s[mid].name, key);
if (res == 0) {
printf("Class of student: %s\n", s[mid].s_class);
found = 1;
break;
}
else if (res < 0) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
if (!found) {
printf("Student not in the list\n");
}
}
No comments:
Post a Comment