gokulcris9

gokulcris9

Lv1

Gokul Cris

0 Followers
0 Following
0 Helped

ANSWERS

Published1

Subjects

Information Technology1

please fix error in this code I created. I am also looking for a step by step explenation of my code to present to my class.    ERROR!
gcc /tmp/0r6YqofOoW.c -lm
/usr/bin/ld: cannot open output file a.out: Permission denied
collect2: error: ld returned 1 exit status                                                                                                                                                                                                                                                                                                                                                                                                                                                  #include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_BRANCHES 5
#define MIN_CARS_PER_BRANCH 5
#define MAX_CARS_PER_BRANCH 10

#define CAD_TO_USD 0.8
#define CAD_TO_MXN 15.0
#define USD_TO_MXN 20.0

#define GROUP_1_BRANDS 5
#define GROUP_2_BRANDS 4
#define GROUP_3_BRANDS 6

#define MAX_MODEL_LENGTH 50
#define MAX_MANUFACTURER_LENGTH 50
#define MAX_CONDITION_LENGTH 10
#define MAX_COLOR_LENGTH 20
#define MAX_COUNTRY_LENGTH 20

typedef struct {
    int length;
    int width;
    int height;
} Dimensions;

typedef struct {
    char type[20];
    Dimensions dimensions;
} Chassis;

typedef struct {
    char make[20];
    char model[20];
    int year;
    char color[20];
    int mileage;
    char location[20];
    Chassis chassis;
    Dimensions exterior;
    int seats;
    char upholstery[20];
    float price;
} Car;

typedef struct {
    char name[20];
    char location[20];
    Car inventory[100];
    int count;
} Branch;

typedef struct {
    Branch branches[10];
    int count;
} Dealership;

// Function prototypes
int menu();
void sell_car(Dealership *dealership);
void buy_car(Dealership *dealership);
void transfer_car(Dealership *dealership);
void supply_cars(Dealership *dealership);
void print_branch_info(Branch *branch);
void print_car_info(Car *car) {
    printf("Make: %s\n", car->make);
    printf("Model: %s\n", car->model);
    printf("Year: %d\n", car->year);
    printf("Color: %s\n", car->color);
    printf("Mileage: %d\n", car->mileage);
    printf("Location: %s\n", car->location);
    printf("Chassis Type: %s\n", car->chassis.type);
    printf("Chassis Dimensions: L%d x W%d x H%d\n", car->chassis.dimensions.length, car->chassis.dimensions.width, car->chassis.dimensions.height);
    printf("Exterior Dimensions: L%d x W%d x H%d\n", car->exterior.length, car->exterior.width, car->exterior.height);
    printf("Seats: %d\n", car->seats);
    printf("Upholstery: %s\n", car->upholstery);
    printf("Price: %.2f\n", car->price);
}

int main() {
    Dealership dealership;
    dealership.count = 0;

    int choice;
    do {
        choice = menu();
        switch (choice) {
            case 1:
                sell_car(&dealership);
                break;
            case 2:
                buy_car(&dealership);
                break;
            case 3:
                transfer_car(&dealership);
                break;
            case 4:
                supply_cars(&dealership);
                break;
            case 5:
                printf("Goodbye!\n");
                break;
            default:
                printf("Invalid choice\n");
                break;
        }
    } while (choice != 5);

    return 0;
}

int menu() {
    int choice;
    printf("1. Sell a car\n");
    printf("2. Buy a car\n");
    printf("3. Transfer a car\n");
    printf("4. Supply cars\n");
    printf("5. Quit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);
    return choice;
}

void sell_car(Dealership *dealership) {
    int branch_choice, car_choice;
    printf("Which branch is selling the car?\n");
    for (int i = 0; i < dealership->count; i++) {
        printf("%d. %s\n", i + 1, dealership->branches[i].name);
    }
    printf("Enter your choice: ");
    scanf("%d", &branch_choice);
    branch_choice--;
    if (branch_choice < 0 || branch_choice >= dealership->count) {
        printf("Invalid branch choice\n");
        return;
    }
    Branch *branch = &dealership->branches[branch_choice];
    if (branch->count == 0) {
        printf("No cars to sell\n");
        return;
    }
   printf("Which car is being sold?\n");
for (int i = 0; i < branch->count; i++) {
printf("%d. %s %s\n", i + 1, branch->inventory[i].make, branch->inventory[i].model);
}
printf("Enter your choice: ");
scanf("%d", &car_choice);
car_choice--;
if (car_choice < 0 || car_choice >= branch->count) {
printf("Invalid car choice\n");
return;
}
Car *car = & branch->inventory[car_choice];
// Update inventory after selling car
for (int i = car_choice; i < branch->count - 1; i++) {
    branch->inventory[i] = branch->inventory[i + 1];
}
branch->count--;

printf("Car sold successfully!\n");
}

void buy_car(Dealership *dealership) {
printf("Enter branch ID: ");
int branch_id;
scanf("%d", &branch_id);
if (branch_id < 0 || branch_id >= dealership->count) {
printf("Invalid branch ID\n");
return;
}

Branch *branch = &dealership->branches[branch_id];

if (branch->count == 100) {
    printf("Branch inventory is full. Cannot buy more cars.\n");
    return;
}

Car car;

printf("Enter car make: ");
scanf("%s", car.make);

printf("Enter car model: ");
scanf("%s", car.model);

printf("Enter car year: ");
scanf("%d", &car.year);

printf("Enter car color: ");
scanf("%s", car.color);

printf("Enter car mileage: ");
scanf("%d", &car.mileage);

printf("Enter car location: ");
scanf("%s", car.location);

printf("Enter car type: ");
scanf("%s", car.chassis.type);

printf("Enter car dimensions (length width height): ");
scanf("%d %d %d", &car.chassis.dimensions.length, &car.chassis.dimensions.width, &car.chassis.dimensions.height);

printf("Enter car exterior dimensions (length width height): ");
scanf("%d %d %d", &car.exterior.length, &car.exterior.width, &car.exterior.height);

printf("Enter car seats: ");
scanf("%d", &car.seats);

printf("Enter car upholstery: ");
scanf("%s", car.upholstery);

printf("Enter car price: ");
scanf("%f", &car.price);

branch->inventory[branch->count] = car;
branch->count++;

printf("Car bought successfully!\n");
}

void transfer_car(Dealership *dealership) {
    printf("Enter source branch ID: ");
    int source_branch_id;
    scanf("%d", &source_branch_id);
    if (source_branch_id < 0 || source_branch_id >= dealership->count) {
        printf("Invalid source branch ID\n");
        return;
    }

    printf("Enter destination branch ID: ");
    int dest_branch_id;
    scanf("%d", &dest_branch_id);
    if (dest_branch_id < 0 || dest_branch_id >= dealership->count) {
        printf("Invalid destination branch ID\n");
        return;
    }

    Branch *source_branch = &dealership->branches[source_branch_id];
    Branch *dest_branch = &dealership->branches[dest_branch_id];

    if (source_branch->count == 0) {
        printf("No cars to transfer\n");
        return;
    }

    printf("Which car do you want to transfer?\n");
    for (int i = 0; i < source_branch->count; i++) {
        printf("%d. %s %s\n", i + 1, source_branch->inventory[i].make, source_branch->inventory[i].model);
    }
    printf("Enter your choice: ");
    int car_choice;
    scanf("%d", &car_choice);
    car_choice--;
    if (car_choice < 0 || car_choice >= source_branch->count) {
        printf("Invalid car choice\n");
        return;
    }

    Car *car = &source_branch->inventory[car_choice];

    // Update inventory in source branch after transferring car
    for (int i = car_choice; i < source_branch->count - 1; i++) {
        source_branch->inventory[i] = source_branch->inventory[i + 1];
    }
    source_branch->count--;

    // Check if destination branch has enough space to receive the car
    if (dest_branch->count == 100) {
        printf("Destination branch inventory is full. Cannot transfer car.\n");
        // Add the transferred car back to the source branch inventory
        source_branch->inventory[source_branch->count] = *car;
        source_branch->count++;
        return;
    }

    // Add the transferred car to the destination branch inventory
    dest_branch->inventory[dest_branch->count] = *car;
    dest_branch->count++;

    printf("Car transferred successfully!\n");
}

Answer: explanation:

Weekly leaderboard

Start filling in the gaps now
Log in