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

In C programming language, with the test cases: Question 2 (15 marks) The upward

ID: 3590869 • Letter: I

Question

In C programming language, with the test cases:

Question 2 (15 marks) The upward velocity of a rocket can be computed by the following formula: gt mo-qt where v=upward velocity u the velocity at which fuel is expelled relative to the rocket, mo the initial mass of the rocket at timet-0, q = the fuel consumption rate, g = the downward acceleration of gravity (assumed constant 9.81 m/s, and t is time in seconds (s). Develop a program that presents the characteristics of four different rockets and requests to user to select one of the rockets to compute the velocity of the rocket for a given time. The program then displays results. The characteristics of the four rockets are u m (kg) q m/s /s Rocket 2000.0 150000.0 2700.0 Rocket 2 1596.0 300000.0 5367.0 Rocket 3 3267.0 543135.0 8900.0 Rocket 4 984.0 5468.089.5 Respect the following guidelines in creating your program. .Use a C structure: Define a structure type ROCKET which contains a member for each rocket characteristic, that is a member name for the rocket name, a member u (initial velocity, a member m0 (initial rocket mass), and a member q (rate of fuel consumption). In the function main, create an array of structure variables (of type ROCKET). The array should contain 4 elements. You can initialise the array in its declaration as follows o o ROCKET rockets [4] = "Rocket 1", 2000.0, 150000.0, 2700.0, { "Rocket 2" , 1596 . O , 300000 . O , 5367 . 0 } , {"Rocket 3", 3267.0, 543135.0, 8900.0, {"Rocket 4", 984.0, 5468.0, 89.5] o DO NOT include the speed v or the time t in your structure .Define a symbolic constant G as 9.81 (gravity acceleration). .The function main shall call the function selectRocket to display the contents of the array and ask the user to make a selection (i.e. using a number from 1 to 4). The address to the array is

Explanation / Answer

#include<stdio.h>
#include<math.h>
//Symbolic constant for downward acceleration of gravity
#define G 9.81f
//Structure ROCKET definition
typedef struct ROCKET
{
//To store rocket name
char rocketName[10];
//To store velocity at which fuel is expelled
float u;
//Initial mass of the rocket at time t = 0
float m0;
//Fuel consumption rate
float q;
}ROCKET;

//Function to return user choice
int displayRockets(ROCKET r[], int no)
{
int x, index;
//Loops till number of record specified as in the parameter no
for(x = 0; x < no; x++)
{
//Display the rocket information
printf(" %s: u = %.2f m0 = %.2f q = %.2f", r[x].rocketName, r[x].u, r[x].m0, r[x].q);
}//End of for loop
//Accept user choice
printf(" Please select a rocket (1 to 4): ");
scanf("%d", &index);
//Validates user choice
//Checks if the index is between 1 and 4 then return index - 1
//index - 1 because structure array index position starts from zero
if(index >= 1 && index <= 4)
return index - 1;
//Otherwise it is invalid index return -1
else
return -1;
}//End of function

//Function to calculate and return upward velocity
double computeSpeed(ROCKET rocket, float t)
{
//To store velocity
float velocity;
//Calculate velocity
velocity = rocket.u * log(rocket.m0 / (rocket.m0 - rocket.q * t)) - G * t;
//Returns velocity
return velocity;
}//End of function

//main function definition
int main()
{
int index;
float t, v;
//Creates an array of structure size 4
//Initializes data
ROCKET rockets[4] =
{
{"Rocket 1", 2000.0, 150000.0, 2700.0},
{"Rocket 2", 1596.0, 300000.0, 5367.0},
{"Rocket 3", 3267.0, 543135.0, 8900.0},
{"Rocket 4", 984.0, 5468.0, 89.5}
};
//Calls the function displayRockets() to display rocket information and accept user choice
index = displayRockets(rockets, 4);
//Checks if index returned bu the function displayRockets() is -1 then display error message
if(index == -1)
printf(" Invalid rocket number!");
//If valid rocket number
else
{
//Accepts time from the user
printf(" Give the time t (s): ");
scanf("%f", &t);
//Calls the function computeSpeed() to calculate velocity
v = computeSpeed(rockets[index], t);
//Displays rocket information with velocity
printf(" Selected Rocket: ");
printf(" Name: %s", rockets[index].rocketName);
printf(" Speed of fuel u: %.2f m/s", rockets[index].u);
printf(" Initial mass m0: %.2f kg", rockets[index].m0);
printf(" Rate of fuel consumption: %.2f kg/s", rockets[index].q);
printf(" After %.2f seconds, the speed v of the rocket is %.4f m/s", t, v);
}//End of else
}//End of main function

Sample Run 1:

Rocket 1: u = 2000.00 m0 = 150000.00 q = 2700.00
Rocket 2: u = 1596.00 m0 = 300000.00 q = 5367.00
Rocket 3: u = 3267.00 m0 = 543135.00 q = 8900.00
Rocket 4: u = 984.00 m0 = 5468.00 q = 89.50
Please select a rocket (1 to 4): 1

Give the time t (s): 11.3

Selected Rocket:
Name: Rocket 1
Speed of fuel u: 2000.00 m/s
Initial mass m0: 150000.00 kg
Rate of fuel consumption: 2700.00 kg/s
After 11.30 seconds, the speed v of the rocket is 343.9522 m/s

Sample Run 2:

Rocket 1: u = 2000.00 m0 = 150000.00 q = 2700.00

Rocket 2: u = 1596.00 m0 = 300000.00 q = 5367.00
Rocket 3: u = 3267.00 m0 = 543135.00 q = 8900.00
Rocket 4: u = 984.00 m0 = 5468.00 q = 89.50
Please select a rocket (1 to 4): 2

Give the time t (s): 31.20

Selected Rocket:
Name: Rocket 2
Speed of fuel u: 1596.00 m/s
Initial mass m0: 300000.00 kg
Rate of fuel consumption: 5367.00 kg/s
After 31.20 seconds, the speed v of the rocket is 997.5816 m/s

Sample Run 3:


Rocket 1: u = 2000.00 m0 = 150000.00 q = 2700.00
Rocket 2: u = 1596.00 m0 = 300000.00 q = 5367.00
Rocket 3: u = 3267.00 m0 = 543135.00 q = 8900.00
Rocket 4: u = 984.00 m0 = 5468.00 q = 89.50
Please select a rocket (1 to 4): 3

Give the time t (s): 15.20

Selected Rocket:
Name: Rocket 3
Speed of fuel u: 3267.00 m/s
Initial mass m0: 543135.00 kg
Rate of fuel consumption: 8900.00 kg/s
After 15.20 seconds, the speed v of the rocket is 786.7077 m/s