Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using only the C language, how would I write the code for this program? The bonu

ID: 3909897 • Letter: U

Question

Using only the C language, how would I write the code for this program? The bonus is not needed. s submit s submit Cs2050 2050LAB4 e-lab4.c Filename must be: sectionletter-lab3.c (Include your respective lab section) e.g. a-lab4.c or b-lab4.c Description For this lab assignment you will write a C program that implements a basic version of a professor rating system. An array of instructors will be read in from an input file. The array will be sorted on average rating and then the user will be allowed to search for an average rating Include the following structure definition at the top of your program: typedef struct instructor float average gpa; float average rating; char *name; instructor Implement the following functions: instructor read_data(char *filename, int size); The read data function takes a filename and input size as inputs and retums a pointer to an array of instructors. This is very similar to read data functions from previous labs and prelabs. You will need to malloc memory within this function. Remember to malloc space for each character pointer within the structures in the array. You can assume that no name will be longer than 15 characters.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

typedef struct instructor
{
    float average_gpa;
    float average_rating;
    char* name;
}instructor;

instructor* read_data(char* filename, int size);
instructor* merge_sort(instructor* teachers, int l_index, int r_index);
int binary_search(instructor* teachers, int left, int right, float x);
void printTeachers(instructor* teachers, int size);
int main(int argc, char** argv)
{
    if(argc != 3)
    {
       printf("Invalid number of arguments. ");
       return 0;
    }
    int size = atoi(argv[1]);
    instructor* teachers = read_data(argv[2], size);
    printTeachers(teachers, size);
    teachers = merge_sort(teachers, 0, size-1);
    //printf("%f %f%s ", teachers[9].average_gpa, teachers[9].average_rating, teachers[9].name);
    printTeachers(teachers, size);
    printf("What average rating would you like to search for? ");
    printf("Please enter a rating between 0 and 10: ");
    float rating;
    scanf("%f", &rating);
    while(rating < 0 || rating > 10)
    {
       printf("ERROR: INPUT MUST BE BETWEEN 0 AND 10! ");
       printf("Please enter a rating between 0 and 10: ");
       scanf("%f", &rating);
    }
    int pos = binary_search(teachers, 0, size-1, rating);
    if(pos != -1)
        printf("%s has an average rating of %f that matches your search request! ", teachers->name, teachers->average_rating);
    else
        printf("No instructor was found with your requested average rating. ");  
}

int binary_search(instructor* teachers, int left, int right, float x)
{
    if(left <= right)
    {
       int mid = (left + right) / 2;
       if(x < teachers[mid].average_rating)
           return binary_search(teachers, left, mid-1, x);
       else if(x > teachers[mid].average_rating)
           return binary_search(teachers, mid+1, right, x);
       else
           return mid;      
    }
    return -1;
}

void merge(instructor* teachers, int l_index, int s_index, int r_index)
{
    int n1 = s_index - l_index + 1;
   int n2 = r_index - s_index;
   instructor L[n1], R[n2];
   for(int i = 0; i < n1; i++)
        L[i] = teachers[l_index + i];
   for(int j = 0; j < n2; j++)
        R[j] = teachers[s_index + j + 1];
   /*L[n1] = 999;
   R[n2] = 999;*/
   int i = 0, j = 0;
   for(int k = l_index; k <= r_index; k++)
        if(L[i].average_rating <= R[j].average_rating)
       teachers[k] = L[i++];
       else
       teachers[k] = R[j++];
}

instructor* merge_sort(instructor* teachers, int l_index, int r_index)
{
    if(l_index < r_index)
    {
       int m = (l_index + r_index) / 2;
       merge_sort(teachers, l_index, m);
       merge_sort(teachers, m+1, r_index);
       merge(teachers, l_index, m, r_index);
    }
    return teachers;
}

instructor* read_data(char* filename, int size)
{
    instructor* temp = (instructor*)malloc(sizeof(instructor) * size);
    FILE *fp = fopen(filename, "r");
    for(int i = 0; i < size; i++)
    {
       fscanf(fp, "%f", &temp[i].average_gpa);
       fscanf(fp, "%f", &temp[i].average_rating);
       temp[i].name = malloc(sizeof(char)*15);
       fscanf(fp, "%s", temp[i].name);
    }
    return temp;
}
void printTeachers(instructor* teachers, int size)
{
    printf("AVERAGE_GPA AVERAGE_RATING NAME ");
    for(int i = 0; i < size; i++)
        printf("%.2f %.2f %s ", teachers[i].average_gpa, teachers[i].average_rating, teachers[i].name);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote