Data structures
Please find out the errors and tell me it's urgent.
#include <stdio.h> #include <string.h>
#define MAX_STUDENTS 50
// Structure for student data struct Student { int regNo; char name[50]; char branch[50]; float cgpa; };
// Linear search function to find a student by registration number void linearSearch(struct Student students[], int size, int regNo) { int found = 0;
for (int i = 0; i < size; i++) {
if (students[i].regNo == regNo) {
printf("Student found:\n");
printf("Reg No: %d\n", students[i].regNo);
printf("Name: %s\n", students[i].name);
printf("Branch: %s\n", students[i].branch);
printf("CGPA: %.2f\n", students[i].cgpa);
found = 1;
break;
}
}
if (!found) {
printf("Student not found!\n");
}
}
// Bubble sort function to arrange students data by registration number void bubbleSort(struct Student students[], int size) { for (int i = 0; i < size - 1; i++) { for (int j = 0; j < size - i - 1; j++) { if (students[j].regNo > students[j + 1].regNo) { // Swap students struct Student temp = students[j]; students[j] = students[j + 1]; students[j + 1] = temp; } } } }
// Binary search function to find a student by registration number (requires data to be sorted) void binarySearch(struct Student students[], int size, int regNo) { int left = 0; int right = size - 1; int found = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
if (students[mid].regNo == regNo) {
printf("Student found:\n");
printf("Reg No: %d\n", students[mid].regNo);
printf("Name: %s\n", students[mid].name);
printf("Branch: %s\n", students[mid].branch);
printf("CGPA: %.2f\n", students[mid].cgpa);
found = 1;
break;
}
else if (students[mid].regNo < regNo) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
if (!found) {
printf("Student not found!\n");
}
}
// Insertion sort function to arrange students data by CGPA in descending order void insertionSort(struct Student students[], int size) { for (int i = 1; i < size; i++) { struct Student key = students[i]; int j = i - 1;
while (j >= 0 && students[j].cgpa < key.cgpa) {
students[j + 1] = students[j];
j = j - 1;
}
students[j + 1] = key;
}
}
int main() { struct Student students[MAX_STUDENTS] = { { 1001, "John", "Computer Science", 8.5 }, // Add remaining student data // ... };
int regNoToSearch = 1003;
// Linear search
linearSearch(students, MAX_STUDENTS, regNoToSearch);
// Bubble sort by registration number
bubbleSort(students, MAX_STUDENTS);
// Binary search after sorting
binarySearch(students, MAX_STUDENTS, regNoToSearch);
// Insertion sort by CGPA in descending order
insertionSort(students, MAX_STUDENTS);
return 0;
}
Could you add a screenshot of what the error is? I am getting it alright.