Code:
#include <stdio.h>
#include <stdlib.h>
#define TOTAL_RENTALS 6
#define ALL_CARS 4
typedef enum areas {north = 1, center, south} areas;
typedef struct
{
int model;
areas area;
int rental_length;
int day_pay;
int age;
} rentals;
rentals all_rentals[TOTAL_RENTALS] =
{
{ 1111, north, 011, 20, 23 },
{ 2222, south, 015, 25, 35 },
{ 3333, center, 016, 23, 39 },
{ 4444, north, 011, 25, 57 },
{ 2222, center, 010, 23, 45 },
{ 4444, center, 003, 20, 52 },
};
typedef struct
{
int model;
char mnfctr[20];
char model_name[15];
int stock;
} car;
car cars[ALL_CARS] =
{
{ 1111, "Rols-Roys", "R-1", 010 },
{ 2222, "Mercedes", "M-1", 020 },
{ 3333, "Mercedes", "M-2", 057 },
{ 4444, "Jajuar", "J-1", 103 }
};
int process[TOTAL_RENTALS] = { 0 };
int sum_rental_length[TOTAL_RENTALS]={0};
void hi_rent_model(int sum_rental_length[TOTAL_RENTALS], car cars[ALL_CARS]);
void title (void);
int main(void)
{
hi_rent_model(sum_rental_length, cars);
return 0;
}
void hi_rent_model(int sum_rental_length[TOTAL_RENTALS], car cars[ALL_CARS])
{
int i, j,temp;
for(i = 0;i < TOTAL_RENTALS;i++)
{
if(!process[i])
{
sum_rental_length[i] += all_rentals[i].rental_length;
for(j = i+1;j < TOTAL_RENTALS;j++)
{
if(all_rentals[i].model == all_rentals[j].model)
{
sum_rental_length[i] += all_rentals[j].rental_length;
process[j] = 1;
}
}
}
}
for (i=0;i<TOTAL_RENTALS;++i)
for (j=i+1;j>TOTAL_RENTALS;++j)
{
if (sum_rental_length[i]> sum_rental_length[j])
{
temp=sum_rental_length[j];
sum_rental_length[j]=sum_rental_length[i];
sum_rental_length[i]=temp;
}
}
for(i = 0;i < TOTAL_RENTALS;i++)
{
if(sum_rental_length[i] != 0)
printf("Model %d had a total of %d days\n", all_rentals[i].model, sum_rental_length[i]);
}
title();
printf ("\n %2d %17s %13s %8d\n ",cars[0].model, cars[0].model_name, cars[0].mnfctr, sum_rental_length[0] );
}
void title (void)
{
puts ("============================================================\n\r");
puts ("model number | model name | manufacturer | total rental days\n\r");
puts ("============================================================\n\r");
}