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

C program A small car dealer would like to maintain information for the cars in

ID: 3607138 • Letter: C

Question

C program

A small car dealer would like to maintain information for the cars in stock. Each car was stored with the make and model, color, manufacture year, city mpg, highway mpg, and quantity. The program dealer.c contains the car struct declaration, function prototypes, and the main function. Complete the function definitions so it uses a dynamically allocated linked list to store the cars. The following functions should be completed: 1. append_to_list: ask the user to enter a car’s make, model, color, manufacture year, city mpg, highway mpg, and quantity (in the exact order), then add the car to the end of the linked list. a. It should check whether the car has already existed by make, model, color, and manufacture year. If so, the function should print a message and exit. b. If the car does not exist, allocate memory for the player, store the data, and append the car to the end of the linked list. c. If the list is empty, the function should return the pointer to the newly created car. d. Otherwise, add the car to the end of the linked list and return the pointer to the linked list. 2. find_car: search by make and model, print the car’s color, manufacture year, city mpg, highway mpg, and quantity. Print all cars that match the make and model. If the make and model is not found, print a message. 3. printList: print the make, model, and manufacture year, and color of all the cars. 4. clearList: when the user exists the program, all the memory allocated for the linked list should be deallocated.

Dealer.c

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define LEN 30

struct car{

char make[LEN+1];

char model[LEN+1];

char color[LEN+1];

int year;

int city_mpg;

int highway_mpg;

int quantity;

struct car *next;

};

struct car *append_to_list(struct car *list);

void find_car(struct car *list);

void printList(struct car *list);

void clearList(struct car *list);

int read_line(char str[], int n);

/**********************************************************

* main: Prompts the user to enter an operation code, *

* then calls a function to perform the requested *

* action. Repeats until the user enters the *

* command 'q'. Prints an error message if the user *

* enters an illegal code. *

**********************************************************/

int main(void)

{

char code;

struct car *car_list = NULL;  

printf("Operation Code: a for appending to the list, f for finding a car"

", p for printing the list; q for quit. ");

for (;;) {

printf("Enter operation code: ");

scanf(" %c", &code);

while (getchar() != ' ') /* skips to end of line */

;

switch (code) {

case 'a': car_list = append_to_list(car_list);

break;

case 'f': find_car(car_list);

break;

case 'p': printList(car_list);

break;

case 'q': clearList(car_list);

return 0;

default: printf("Illegal code ");

}

printf(" ");

}

}

struct car *append_to_list(struct car *list)

{

printf(" Please enter the car's make, model, color, manufacture year, city, mpg, highway mpg, and quantity in that order: ");

  

//add your code here and remove the return NULL; statement

return NULL;

}

void find_car(struct car * list)

{

//add your code here

}

void printList(struct car *list){

//add your code here

}

void clearList(struct car *list)

{

//add your code here

}

int read_line(char str[], int n)

{

int ch, i = 0;

while (isspace(ch = getchar()))

;

str[i++] = ch;

while ((ch = getchar()) != ' ') {

if (i < n)

str[i++] = ch;

  

}

str[i] = '';

return i;

}

Explanation / Answer

Given below is the completed code. I tested it with some dummy values and attached the output. Please do rate the answer if it helpe.d Thank you.

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

#define LEN 30
struct car{
char make[LEN+1];
char model[LEN+1];
char color[LEN+1];
int year;
int city_mpg;
int highway_mpg;
int quantity;
struct car *next;
};


struct car *append_to_list(struct car *list);
void find_car(struct car *list);
void printList(struct car *list);
void clearList(struct car *list);
int read_line(char str[], int n);

/**********************************************************
* main: Prompts the user to enter an operation code, *
* then calls a function to perform the requested *
* action. Repeats until the user enters the *
* command 'q'. Prints an error message if the user *
* enters an illegal code. *
**********************************************************/
int main(void)
{
char code;
  
struct car *car_list = NULL;
printf("Operation Code: a for appending to the list, f for finding a car"
", p for printing the list; q for quit. ");
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': car_list = append_to_list(car_list);
break;
case 'f': find_car(car_list);
break;
case 'p': printList(car_list);
break;
case 'q': clearList(car_list);
return 0;
default: printf("Illegal code ");
}
printf(" ");
}
}

struct car *append_to_list(struct car *list)
{
struct car * c = (struct car *) malloc(sizeof(struct car));
struct car *last = NULL, *curr = list;
int found = 0;
printf(" Please enter the car's make, model, color, manufacture year, city, mpg, highway mpg, and quantity in that order: ");
  
//add your code here and remove the return NULL; statement
read_line(c->make, LEN);
read_line(c->model, LEN);
read_line(c->color, LEN);
scanf("%d %d %d %d", &c->year, &c->city_mpg, &c->highway_mpg, &c->quantity);
c->next = NULL;
if(list == NULL)
return c;
else
{
//check if duplicate
while(curr != NULL)
{
if(strcmp(curr->make, c->make) == 0 &&
strcmp(curr->model, c->model) == 0 &&
strcmp(curr->color, c->color) == 0 &&
curr->year == c->year)
{
found = 1;
break;
}
if(curr->next == NULL)
last = curr;
  
curr = curr->next;
}
  
  
if(!found) //not a duplicate
{
last->next = c;
}
else
printf("Car with similar values exist in the list ");
return list;
}
  
}

void find_car(struct car * list)
{
char make[LEN + 1], model[LEN + 1];
struct car *c = list;
  
printf("Enter make: ");
read_line(make, LEN);
printf("Enter model: ");
read_line(model, LEN);
printf("%15s %15s %15s %15s %15s " , "color", "year", "city mpg", "highway mpg" , "quantity");
while(c != NULL)
{
if(strcmp(c->make, make) == 0 && strcmp(c->model, model) == 0)
{
printf("%15s %15d %15d %15d %15d " , c->color, c->year, c->city_mpg, c->highway_mpg, c->quantity);
}
c = c->next;
}
  
printf("---------------------------------------------------------------- ");
  
}
void printList(struct car *list){
struct car *c = list;
printf("%15s %15s %15s %15s " , "make", "model", "year", "color" );
while(c != NULL)
{
printf("%15s %15s %15d %15s " , c->make, c->model, c->year , c->color );
c = c->next;
}
  
printf("---------------------------------------------------------------- ");
  
  
}
void clearList(struct car *list)
{
struct car *c = list, *temp;
while(c != NULL)
{
temp = c->next;
free(c);
c = temp;
}
}

int read_line(char str[], int n)
{
int ch, i = 0;
  
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
  
}
str[i] = '';
return i;
}

output

Operation Code: a for appending to the list, f for finding a car, p for printing the list; q for quit.
Enter operation code: a
Please enter the car's make, model, color, manufacture year, city, mpg, highway mpg, and quantity in that order:
make1
model1
red
2009
10
20
5

Enter operation code: p
make model year color
make1 model1 2009 red
----------------------------------------------------------------


Enter operation code: a
Please enter the car's make, model, color, manufacture year, city, mpg, highway mpg, and quantity in that order:
make2222
model222
blue
2010
55
43
10

Enter operation code: p
make model year color
make1 model1 2009 red
make2222 model222 2010 blue
----------------------------------------------------------------


Enter operation code: f
Enter make: make1
Enter model: model1
color year city mpg highway mpg quantity
----------------------------------------------------------------


Enter operation code: q
amoeba-2:Test2 raji$ gcc dealer.c
amoeba-2:Test2 raji$ ./a.out
Operation Code: a for appending to the list, f for finding a car, p for printing the list; q for quit.
Enter operation code: a
Please enter the car's make, model, color, manufacture year, city, mpg, highway mpg, and quantity in that order:
make1
model1
red
2009
11
22
8

Enter operation code: a
Please enter the car's make, model, color, manufacture year, city, mpg, highway mpg, and quantity in that order:
make1
model1
blue
2009
44
55
6

Enter operation code: p
make model year color
make1 model1 2009 red
make1 model1 2009 blue
----------------------------------------------------------------


Enter operation code: a
Please enter the car's make, model, color, manufacture year, city, mpg, highway mpg, and quantity in that order:
make1
model1
red
2009
88
99
3
Car with similar values exist in the list

Enter operation code: p
make model year color
make1 model1 2009 red
make1 model1 2009 blue
----------------------------------------------------------------


Enter operation code: f
Enter make: make1
Enter model: model1
color year city mpg highway mpg quantity
red 2009 11 22 8
blue 2009 44 55 6
----------------------------------------------------------------


Enter operation code: q