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

How would I create this C program with the given functions? The sample output is

ID: 3904169 • Letter: H

Question

How would I create this C program with the given functions? The sample output is shown. Thank you e.g. a-lab2.c or b-lab2.c Description For this lab assignment you will write a C program that does basic inventory control for a car dealership. To represent each car, you will use a structure that holds a few pieces of information. The structure is as follows: struct car int vin num float mileage char "color You are encouraged to use the typedef keyword and use an alias for your struct. If you choose to do this, your function prototypes will look slightly different because you won't need to include the struct keyword. Don't forget to error check everything that you need to error check. Especially user input. You must assume that user input may be entered wrong more than once. If you are doing the bonus, then implement the bonus after you implement the main lab. Implement the following functions

Explanation / Answer

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

typedef struct{
    int vin_number;
    float mileage;
    char *color;
} car;

car *read_data(int num){

    car *list = (car *)malloc(sizeof(car)*num);
    int i;
   
    for (i = 0; i<num; i++){
        list[i].color = (char *)malloc(sizeof(char)*30);
        printf("Please enter a VIN number:");
        scanf("%d",&list[i].vin_number);
        printf("Please enter mileage of the vehicle:");
        scanf("%d",&list[i].mileage);
        printf("Please enter a color:");
        scanf("%s",&list[i].color);
    }
    return list;
}

void print_garage(car *list, int num){

   printf("Cars currently in garage: ");
   int i;
    for (i = 0; i<num; i++){
        printf("VIN number:%d ",list[i].vin_number);
        printf("Mileage:%d ",list[i].mileage);
        printf("Color:%d ",list[i].color);
       
    }
}

int is_VIN(car *a, int vin){
   if (a->vin_number == vin)
      return 1;
   else
      return 0;
}


void sort_garage(car *list, int num){

   int n,i,j;
   printf("1-Sort by vin number, 2- sort by mileage ");
   printf("Enter choice:");
   scanf("%d",&n);
   for (i = 0; i<num; i++){
       for (j = i+1; j<n; j++){
          if ((n==1 && list[i].vin_number > list[j].vin_number) || (n==2 && list[i].mileage > list[j].mileage)){
               car temp;
               temp.color = (char *)malloc(sizeof(char)*30);
               temp.vin_number = list[i].vin_number;
               temp.mileage = list[i].mileage;
               strcpy(temp.color,list[i].color);
               list[i].vin_number = list[j].vin_number;
               list[i].mileage = list[j].mileage;
               strcpy(list[i].color,list[j].color);
               list[j].vin_number = temp.vin_number;
               list[j].mileage = temp.mileage;
               strcpy(list[j].color,temp.color);
          }
       }
   }
  
}

void save_garage(car *list, int num, char *nm){

   FILE *fp;

   fp = fopen(nm,"w");
  
   fprintf(fp,"Cars currently in garage: ");
   int i;
    for (i = 0; i<num; i++){
        fprintf(fp,"VIN number:%d ",list[i].vin_number);
        fprintf(fp,"Mileage:%d ",list[i].mileage);
        fprintf(fp,"Color:%d ",list[i].color);
       
    }
    fclose(fp);
}

void is_color(car *list,int num){
     char color[30];
     int i;
     printf("Please enter a color to search for:");
     scanf("%s",color);
     int found = 0;
     for (i = 0; i<num; i++){
         if (strcmp(list[i].color,color) == 0){
            found = 1;
            printf("A %s color car has been found.The vin number is %d ",color, list[i].vin_number);
         }
     }
     if (found == 0)
        printf("Not found ");

}


int main(){

     int n;
     printf("Enter number of cars:");
     scanf("%d",&n);

     car *list = read_data(n);

     print_garage(list,n);
     printf("Would you like to serach for a VIN number? ");
     int ch;
     while(1){
        printf("Please select 1-yes 0-no ");
        scanf("%d",&ch);
        if (ch != 1 && ch != 0)
           printf("Invalid choice ");
        else
           break;
     }
     if (ch == 1){
        is_color(list,n);
     }
     save_garage(list,n,"output.txt");
}

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