Code:
/*Program to calculate the impact force
on a given choice of vehicles*/
float safety_rating=100;
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
//Defining the structure obstacle
typedef struct
{
char obs_name[15];
float max_std;
}obstacle_t; // Using typedef to make an alias for structure obstacle name
//Defining the structure vehicle
typedef struct
{
char name[20];
int manufacture_date;
float top_speed;
int mass;
int no_of_seats;
}vehicle_t; // Using typedef to create an alias for structure vehicle name
//Function Prototypes declared
int Menu();
void Vehicle_initilization(vehicle_t[]);
void Obstacle_initilization(obstacle_t[]);
void Different_cases(int,vehicle_t[]);
int obstacle_selection(obstacle_t[]);
void Values(int,int*,float*);
float Selection(float,int,obstacle_t[],int,vehicle_t[],int,int*,int*);
int Enter_values(float,float*,float*,float,vehicle_t[],int,int*,int*);
int Enter_passengers(vehicle_t[],int);
int Enter_belted_passengers(int*);
void Randomize_conditions(float*,float,float*,float,int);
float Impact_force(int,float,float);
void Print_force(float);
void print_vehicle(vehicle_t[],int);
void print_obstacle(obstacle_t);
int age_criteria(vehicle_t[],int);
int Randomize_passengers(vehicle_t[],int);
int Randomize_belted_passengers(int*);
int vehicle_age(vehicle_t[],int);
float Safety_rating(int,float force);
void print_safety_rating(float);
float Randomize_car_damage(float);
void print_Car_damage(float);
void Passenger_safety_rating(float,int*,int*);
//Main function
int main()
{
//Seeding the random function for distinct values each time
srand((unsigned)time(NULL));
//Variables declared
int i,ch,ch2,ch3,obstch,mass,no_of_passengers,no_of_belted_passengers;
float topspeed,force,safety_rating,overall_car_damage;
//Variables of structure type declared and initialized
vehicle_t vehicle[3];
obstacle_t obstacle[3];
//do-while loop for repeating the program
do
{
system ("cls");
//do-while loop for repeating the vehicle selection for wrong input
do
{
ch=Menu();
}while(ch<=0||ch>3);
//Calling different function and passing the required values/parameters
Vehicle_initilization(vehicle);
Obstacle_initilization(obstacle);
Different_cases(ch,vehicle);
obstch=obstacle_selection(obstacle);
Values(ch,&mass,&topspeed);
do
{
safety_rating=Selection(topspeed,mass,obstacle,obstch,vehicle,ch,&no_of_passengers,&no_of_belted_passengers);
print_safety_rating(safety_rating);
printf("\n\nDo you want to proceed? [1.)yes 2.) No]: ");
scanf("%d",&ch3);
system("cls");
}while(ch3>1&&ch3<3);
overall_car_damage=Randomize_car_damage(safety_rating);
print_vehicle(vehicle,ch);
print_safety_rating(safety_rating);
print_Car_damage(overall_car_damage);
//Asking for the repetition of the simulation
printf("\n\nDo you want to simulate again? [1.Yes 2.No] : ");
scanf("%d",&ch2);
}while(ch2==1);
printf("\nThanks for using the program");
//Delay the termination
Sleep(2*1000);
return 0;
}
//Function for the vehicle menu display/selection
int Menu()
{
//Variable declared
int ch;
printf("%-18s%-17s%-15s%-19s%-10s\n","Vehicle Name","First Mftd.","Top speed","No. seats","Mass\n");
printf("%-18s%-17s%-15s%-19s%-10s\n","1.)Toyota Camry","|1991","|200 km/h","|5","|1100kg");
printf("%-18s%-17s%-15s%-19s%-10s\n","2.)Bugatti Veyron","|2005","|415 km/h","|2","|1888kg");
printf("%-18s%-17s%-15s%-19s%-10s\n","3.)Enviro400 Bus","|2005","|129 km/h","|90","|18000kg\n");
//Ask for vehicle selection
printf("Choose a vehicle(1,2,3): ");
scanf("%d",&ch);
//Checking for wrong input/error prompt
if(ch<=0||ch>3)
{
printf("Wrong Choice!! Try Again\n\n");
}
//Return selection
return ch;
}
float Safety_rating(int age,float force)
{
float age_criteria,force_criteria,overall_safety_rating;
age_criteria=0.25*((age-2)*(age-2));
force_criteria=force/10000;
overall_safety_rating= safety_rating-age_criteria-force_criteria;
return overall_safety_rating;
}
void print_safety_rating(float safety_rating)
{
if(safety_rating<0)
{
safety_rating=0;
}
else if(safety_rating>100)
{
safety_rating=100;
}
printf("\nYour overall safety rating is: %f \n",safety_rating);
}
void Passenger_safety_rating(float safety_rating,int *no_of_passengers,int *no_of_belted_passengers)
{
float passenger_safety_rating,notbelted_p_safety_rating;
passenger_safety_rating=safety_rating;
notbelted_p_safety_rating=safety_rating;
if(*no_of_belted_passengers<*no_of_passengers)
{
passenger_safety_rating=safety_rating/4;
notbelted_p_safety_rating=safety_rating/9;
}
printf("\nEach Passenger has a %.3f chance of survival \n",passenger_safety_rating);
printf("\nEach Passenger without a seatbelt has a %.3f chance of survival \n",notbelted_p_safety_rating);
}
float Randomize_car_damage(float safety_rating)
{
float overall_car_damage,damage_limit;
damage_limit=0.25*(100-safety_rating);
overall_car_damage=(rand()/(float)RAND_MAX*(damage_limit-1))+1;
return overall_car_damage;
}
void print_Car_damage(float overall_car_damage)
{
printf("\nThe Overall Damage suffered by the vehicle is: %f \n",overall_car_damage);
}
int vehicle_age(vehicle_t vehicle[],int vehchoice)
{
int age,criteria;
criteria=age_criteria(vehicle,vehchoice);
do
{
printf("\nEnter the age of the vehicle: ");
scanf("%d",&age);
if(age>criteria||age<0)
{
printf("Inappropriate input!! Please try again");
}
}while(age>criteria||age<0);
return age;
}
int age_criteria(vehicle_t vehicle_age[],int choice)
{
int criteria,current_year;
time_t now= time(NULL);
struct tm *t = localtime(&now);
current_year=t->tm_year+1900;
criteria=current_year-vehicle_age[choice-1].manufacture_date;
return criteria;
}
void Vehicle_initilization(vehicle_t vehicle[])
{
vehicle[0]=(vehicle_t){"Toyota Camry",1991,200,1100,5};
vehicle[1]=(vehicle_t){"Bugatti Veyron",2005,415,1888,2};
vehicle[2]=(vehicle_t){"Enviro400 Bus",2005,129,18000,90};
}
void Obstacle_initilization(obstacle_t obstacle[])
{
obstacle[0]=(obstacle_t){"Water Tanks",7};
obstacle[1]=(obstacle_t){"Tree",2};
obstacle[2]=(obstacle_t){"Wall",3};
}
//Function for various vehicle choices
void Different_cases(int ch,vehicle_t vehicle[])
{
/*Switch-case for assigning different structure elements
to appropriate choice of the user*/
switch(ch)
{
case 1: printf("\nYou have chosen:\n");
print_vehicle(vehicle,ch);
break;
case 2: printf("\nYou have chosen:\n");
print_vehicle(vehicle,ch);
break;
case 3: printf("\nYou have chosen:\n");
print_vehicle(vehicle,ch);
break;
default: printf("\nWrong Choice!!\n");
break;
}
}
//Function for various obstacle choices and selection
int obstacle_selection(obstacle_t obstacle[])
{
//Variable declared
int choice;
printf("\n%-18s%-20s\n","Obstacle Name","Max stopping dist.\n");
printf("%-18s%-20s\n","1.)Water Tank","7");
printf("%-18s%-20s\n","2.)Tree","2");
printf("%-18s%-20s\n","3.)Wall","3\n");
do
{
printf("Choose an obstacle 1.),2.),3.) : ");
scanf("%d",&choice);
if(choice<=0||choice>3)
{
printf("Wrong Choice!! Try again\n");
}
else
{
//Switch-case for assigning different values to choice of the user
switch(choice)
{
case 1: print_obstacle(obstacle[0]);
break;
case 2: print_obstacle(obstacle[1]);
break;
case 3: print_obstacle(obstacle[2]);
break;
default: printf("Wrong Choice!!\n");
break;
}
}
}while(choice<=0||choice>3);
//Returning the choice
return choice;
}
//Function to assign values to mass and topspeed for different choices
void Values(int ch,int *mass,float *topspeed)
{
//choice 1
if(ch==1)
{
*mass=1100;
*topspeed=200;
}
//choice 2
else if(ch==2)
{
*mass=1888;
*topspeed=415;
}
//choice 3
else if(ch==3)
{
*mass==18000;
*topspeed=129;
}
}
//Function to prompt for manual or auto entry of the values
float Selection(float topspeed,int mass,obstacle_t obstacle[],int obstchoice,vehicle_t vehicle[],int vehchoice,int *no_of_passengers,int *no_of_belted_passengers)
{
int c,criteria,age;
float velocity,sd,force,max_std,safety_rating;
criteria=age_criteria(vehicle,vehchoice);
//Assigning the user's choice for maximum distance to max_std
max_std=obstacle[obstchoice-1].max_std;
printf("\n\nDo you want to enter the values manually? [1.yes 2. no] : ");
scanf("%d",&c);
//if c=1, enter manually
if(c==1)
{
//Calling the Enter_values function
*no_of_passengers=Enter_passengers(vehicle,vehchoice);
*no_of_belted_passengers=Enter_belted_passengers(&no_of_passengers);
age=Enter_values(topspeed,&velocity,&sd,max_std,vehicle,vehchoice,&no_of_passengers,&no_of_belted_passengers);
//Calling the Impact force_function and assigning force to variable
force=Impact_force(mass,velocity,sd);
Print_force(force);
}
//else if c=2, get randomized values
else if(c==2)
{
//Calling randomize function
Randomize_conditions(&velocity,topspeed,&sd,max_std,criteria);
*no_of_passengers=Randomize_passengers(vehicle,vehchoice);
*no_of_belted_passengers=Randomize_belted_passengers(&no_of_passengers);
force=Impact_force(mass,velocity,sd);
Print_force(force);
}
safety_rating=Safety_rating(age,force);
return safety_rating;
}
//Function to enter the values manually
int Enter_values(float topspeed,float *velocity,float *sd,float max_std,vehicle_t vehicle[],int vehchoice,int *no_of_passengers,int *no_of_belted_passengers)
{
int age,criteria;
//do-while loop for error prompt
do
{
printf("\nEnter the velocity(km/h): ");
scanf("%f",velocity);
//Command to check if velocity is within the limits(error prompt)
if(*velocity>topspeed||*velocity<=0)
{
printf("\nOver/Under the speed limit, try again!!\n");
}
}while(*velocity>topspeed||*velocity<=0);
//do-while loop for error prompt
do
{
printf("\nEnter the stopping distance(m): ");
scanf("%f",sd);
//Command to check if the stopping distance is within the specified max limits
if(*sd>max_std||*sd<=0)
{
printf("\nWrong Input!!Over/Under the Stopping Limit,Try Again\n");
}
}while(*sd>max_std||*sd<=0);
//Converting velocity to metres/second
*velocity = *velocity*(0.278);
age=vehicle_age(vehicle,vehchoice);
system("cls");
printf("\nYour Velocity is: %f m/s\n",*velocity);
printf("\nYour stopping distance is: %f ms\n",*sd);
printf("\nYour vehicle age is: %d years\n",age);
printf("\nNumber of passengers in the vehicle are: %d \n",*no_of_passengers);
printf("\nNumber of passengers with their seatbelts on: %d \n",*no_of_belted_passengers);
return age;
}
//Function to assign random values
void Randomize_conditions(float *velocity,float max_velocity,float *sd,float max_std,int criteria)
{
int age,*vehage;
vehage=&age;
//Getting random value for velocity
*velocity= (rand()/(float)RAND_MAX*(max_velocity-1))+1;
//Getting random value for stopping distance
*sd=(rand()/(float)RAND_MAX*(max_std-1))+1;
*vehage=(rand()%criteria)+1;
//Converting the velocity to m/s
*velocity=*velocity*(0.278);
system("cls");
printf("\nYour Velocity is: %f m/s\n",*velocity);
printf("\nYour stopping distance is: %f ms\n",*sd);
printf("\nYour vehicle age is: %d years\n",*vehage);
}
int Enter_passengers(vehicle_t vehicle[],int vehchoice)
{
int no_of_passengers;
do
{
printf("\nEnter the number of passengers: ");
scanf("%d",&no_of_passengers);
if(no_of_passengers>vehicle[vehchoice-1].no_of_seats||no_of_passengers<0)
{
printf("Inappropriate input!! Please try again\n");
}
}while(no_of_passengers>vehicle[vehchoice-1].no_of_seats||no_of_passengers<0);
return no_of_passengers;
}
int Enter_belted_passengers(int *no_of_passengers)
{
int no_of_belted_passengers;
do
{
printf("\nEnter the number of seatbelt wearing passengers: ");
scanf("%d",&no_of_belted_passengers);
if(no_of_belted_passengers>*no_of_passengers||no_of_belted_passengers<0)
{
printf("Inappropriate input!! Please try again\n");
}
}while(no_of_belted_passengers>*no_of_passengers||no_of_belted_passengers<0);
return no_of_belted_passengers;
}
int Randomize_passengers(vehicle_t vehicle[],int choice)
{
int no_of_passengers;
no_of_passengers=(rand()%vehicle[choice-1].no_of_seats)+1;
printf("\nNumber of passengers in the vehicle are: %d \n",no_of_passengers);
return no_of_passengers;
}
int Randomize_belted_passengers(int *no_of_passengers)
{
int no_of_belted_passengers;
no_of_belted_passengers=(rand()%*no_of_passengers)+1;
printf("\nNumber of passengers with their seatbelt on: %d \n",no_of_belted_passengers);
return no_of_belted_passengers;
}
//Function to calculate the impact force
float Impact_force(int mass,float velocity,float sd)
{
float force;
//Working formula
force=(0.5*mass*velocity*velocity)/sd;
//Returning the value
return force;
}
//Function to print the value of force calculated
void Print_force(float force)
{
printf("\nThe calculated Force is: %f N\n",force);
}
//Function to print the structured vehicle choice
void print_vehicle(vehicle_t vehicle[],int choice)
{
system("cls");
printf("Vehicle: %s\n",vehicle[choice-1].name);
printf("Manufacture date: %d\n",vehicle[choice-1].manufacture_date);
printf("Vehicle top speed: %.2f Km/h\n",vehicle[choice-1].top_speed);
printf("Vehicle mass: %d Kg\n",vehicle[choice-1].mass);
printf("Number of passanger seats: %d\n",vehicle[choice-1].no_of_seats);
}
//Function to print the chosen obstacle
void print_obstacle(obstacle_t obstacles)
{
printf("\nThe obstacle chosen is: %s\n",obstacles.obs_name);
printf("Maximum stopping distance for %s is %f\n",obstacles.obs_name,obstacles.max_std);
}