Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data structures #403

Open
Diliphindustani opened this issue May 3, 2023 · 1 comment
Open

Data structures #403

Diliphindustani opened this issue May 3, 2023 · 1 comment

Comments

@Diliphindustani
Copy link

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;

}

@varunpai314
Copy link

Could you add a screenshot of what the error is? I am getting it alright.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants