Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Using the below program which is reading a data file(student.txt file) from the disk…

  1. User

    Using the below program which is reading a data file(student.txt file) from the disk into array of structure which has 10 elements . Assume that data is sorted based on student number. Complete the below program for binary search operation.  Read a student from the monitor and find and list his name , grade and the position in the array of structure . #include <stdio.h> struct studinfo { int stnr; char name[10]; int grade; }; void main() { struct studinfo s[10]; int cnt; FILE *stud_file; cnt=0 ; stud_file = fopen("C:\cmpe231\2009Spring\student.txt" , "r"); while ( !feof(stud_file)) { fscanf(stud_file ,"%d %s %d" ,&s[cnt].stnr,&s[cnt].name,&s[cnt].grade); cnt++; } fclose(stud_file); } Assume that file has the following data in it(Prepare 10 record of data sorted by student number) 123 Ahmet 88 456 Cemal 99 633 Sevgi 75 776 Meral 66 792 Kemal 88 .. .. 8879 Meral 81

  2. Assistant

    Here is the completed program for binary search operation:

    #include <stdio.h>
    #include<string.h>
    
    struct studinfo
    {
        int stnr;
        char name[10];
        int grade;
    };
    
    int binarySearch(struct studinfo s[], int low, int high, int key)
    {
        if (high >= low) {
            int mid = low + (high - low) / 2;
            
            // If the key is present at the middle
            if (s[mid].stnr == key)
                return mid;
            
            // If the key is smaller than the middle, then it can only be present in the left subarray
            if (s[mid].stnr > key)
                return binarySearch(s, low, mid - 1, key);
            
            // Else, the key can only be present in the right subarray
            return binarySearch(s, mid + 1, high, key);
        }
        
        // If the key is not found in the array
        return -1;
    }
    
    int main() {
        struct studinfo s[10];
        int cnt;
        FILE *stud_file;
        
        cnt = 0;
        
        stud_file = fopen("C:\\cmpe231\\2009Spring\\student.txt", "r");
        
        while (!feof(stud_file) && cnt < 10) {
            fscanf(stud_file, "%d %s %d", &s[cnt].stnr, s[cnt].name, &s[cnt].grade);
            cnt++;
        }
        
        fclose(stud_file);
        
        int key;
        printf("Enter student number to search: ");
        scanf("%d", &key);
        
        int position = binarySearch(s, 0, cnt - 1, key);
        
        if (position == -1) {
            printf("Student not found.\n");
        } else {
            printf("Student Name: %s\n", s[position].name);
            printf("Student Grade: %d\n", s[position].grade);
            printf("Position in array: %d\n", position);
        }
        
        return 0;
    }
    

    Please note that the program assumes that the file contains exactly 10 records of data sorted by student number. If the file has less than 10 records or is not sorted, the program may not work correctly.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.