DSA Practical 1
DSA Practical 1
#include <iostream>
#include <string.h>
struct Student
{ int rollNo;
char name[50];
float SGPA;
};
if (!found) {
std::cout << "\nStudent not found in the list.\n";
}
}
void binarySearch(Student list[], int size) { int
lower, upper, mid, searchSGPA;
std::cout << "\nEnter the SGPA you want to search: "; std::cin
>> searchSGPA;
lower = 0;
upper = size - 1;
if (list[mid].SGPA == searchSGPA)
{ std::cout << "\nStudent found!\n";
std::cout << "Roll No: " << list[mid].rollNo << ", Name: " << list[mid].name << ", SGPA: " << list[mid].SGPA
<< std::endl;
return; // Exit the function if found
} else if (list[mid].SGPA < searchSGPA) {
lower = mid + 1;
} else {
upper = mid - 1;
}
}
int main() {
Student studentList[SIZE];
int choice;
switch (choice) {
case 1:
acceptStudentData(studentList);
break;
case 2:
displayStudentList(studentList);
break;
case 3:
bubbleSort(studentList, SIZE); std::cout
<< "\nStudent list sorted by SGPA.\n"; break;
case 4:
searchStudent(studentList, SIZE);
break;
case 5:
binarySearch(studentList, SIZE);
break; case 6:
std::cout << "\nExiting...\n";
return 0;
default:
std::cout << "\nInvalid choice!\n";
}
}
return 0;
}
Output:-