Starting from:
$30

$24

Programming HW 05 Solution

Q1) You are expected to implement a car crash simulator program. There will be two cars. You will get car names as character, car speeds as double, and car weights as integer from console. You can observe program outputs and check your result according to web site http://goo.gl/qnTFb8 .




Assume that crash is an inelastic collision . So, you are expected use the formulas on the web site to find the new speed after the crash.




You are expected to use enumeration while checking state of simulation. PLAY, CRASH, END will be your simulator’s states. The system is on PLAY state when simulator started and it continues to do so while each car gets closer until they crash each other. When they crash, CRASH state is activated until end of simulation. After cars finish their routes, they do not further change their places. At this time, END state is activated to finish simulation.




You can code the functions below to implement simulator. Use the design according to functions below.







Define road length (e.g., 50) as macro and define simulators’s state (object_state) using enumerated type.







Move Function: Moves each car according to their speed. Updates output parameters.




Change object1 and object2 to ‘X’ character when the crash happens. Change speed1 and speed2 (inelastic collision) as well. Note that after crash, they will move together and their position and speed will be same. Do not forget to update game_state variable value when necessary, according to the situation.




void make_move(char *object1, double *position1, double *speed1, int weight1, char *object2, double *position2, double *speed2, int weight2, object_state




*game_state);







Crash Time Calculator Function: Returns crash time as double. When a crash is going to happen, use the function to determine crash time.




double car_crash_time(double position1, double position2, double speed1, double speed2);







Game Print Function: Prints road, each car’s location and letter symbols to the console. When game_state is PLAY show two cars, otherwise just display one so that they have same position value at CRASH and END states.




void print_game_state(char object1, double position1, char object2, double position2, object_state game_state);




Q2) Write a C program that provides the following output values using the following input values.

You must use following functions.




Definitons


Functions


Returns maximum value
int max_array(int
array[?], int n);
Returns second maximum value
int second_max_array(int array[?], int n);
Returns sum of all
array
int sum_all_array
(int array[?], int n);
Returns count of a
value
int count_array(int array[?], int n, int value);
Prints location of
searched
bool search_array
(int array[?], int n, int value);
value















Input Value Example




myarray={ 6,8,3,3,12,8,3,8,2 }







Output Values Example




++++++++++++++++++++

Maksimum array is 12

++++++++++++++++++++

Maksimum second array is 8




++++++++++++++++++++

Sum of all array is 53

++++++++++++++++++++

Count of value 6 is 1

Count of value 8 is 3

Count of value 3 is 3

++++++++++++++++++++

2 is at [8]

8 is at [1]

12 is at [4]

++++++++++++++++++++







General:

Obey honour code principles.
Obey coding convention.



Read your homework carefully and follow the directives about the I/O format (data filenames, file formats, etc.) and submission format strictly. Violating any of these directives will be penalized.



Your submission should include the following file and NOTHING MORE (no data files, object files, etc.):
HW05_<student name_<student surname_<student number_part1.c




Do not use non-English characters in any part of your homework (in body, file name, etc.).
Deliver the printout of your work until the last submission date.

More products