Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Language I have written this program, and now I have to put it in a loop so that it finds the best winning design:

"C Language"

I have written this program, and now I have to put it in a loop so that it finds the best "winning design":

I am struggling with the loop and the array. Copy and paste my code and you'll understand what my program does.

Here is an example:

Sample Run #1

What is the total length of the track, in feet? 

1000

What is the maximum length of a train, in feet? 

42

Your ride can have at most 112 people on it at one time. This can be achieved with trains of 4 cars. AVG Ratio: 0.451

My program already does that.

What I need now is to do this following step

Here what my professor said:

Here are some numbers that you can have your Assignment 2.b program trace-print for you, using the inputs from sample 1. TL N_cars N_trains Total_cars Cumul_car_len Peop Ratio 10 1 25 25 250 100 0.40000 18 2 13 26 234 104 0.44444 26 3 9 27 234 108 0.46153 34 4 7 28 238 112 0.47059 42 5 5 25 210 100 0.47619 So, you need to find the winning design by comparing the People result. Each time through the loop, you could store the ratio in an array, as, ratio_array[N_cars-1] = ratio; Later, as a separate step, have a loop that adds up the array contents and, lastly, divides by the number of entries. For the case above, the average ratio then comes out to be 0.45055

Here is the pseudocode that my professor explains in class to do so (note: the variables names might be different):

for TL=10; TL<=MaxTrainLength; TL+=8

{ Ncars++

for (NumberCars=1; NumberCars<(MaxTrainLength-10)/8+1; NumberCars++)

TrainLength=(NumberCars-1)*8+10

sum=0

for j=0; j

{sum+= Ratio_Array[j]

avg_ratio=sum/FNNcars

(Note: The numbers 8 and 10 are define variables which are in my code: FIRST_CAR_LENGTH and NORMAL_CAR_LENGTH)

And here is my code so far:

#include

#define FIRST_CAR_LENGTH 10

#define NORMAL_CAR_LENGTH 8

#define CAR_CAPACITY 4

int main()

{

//Variables

int track, train, cars, n_trains, people, cumul_car_len, Train_Length;

//Prompt user to enter the total length of the track.

printf("What is the total length of the track, in feet? ");

scanf("%d",&track);

//Prompt user to enter the maximum length of the train.

printf("What is the maximum length of a train, in feet? ");

scanf("%d",&train);

//Compute the number of cars, number of trains, and number of people.

cars = (train-FIRST_CAR_LENGTH)/NORMAL_CAR_LENGTH+1;

n_trains = track/(4*train);

people = CAR_CAPACITY*cars*n_trains;

//Print the user the number of people.

printf("Your ride can have at most %d people on it at one time. ",people);

printf("This can be achieved with trains of %d cars. ", n_trains);

cumul_car_len = n_trains*train;

float ratio = (float) people / cumul_car_len;

printf("AVG ratio: %f ", ratio);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Discuss what would be covered in a first interview with an athlete.

Answered: 1 week ago

Question

Choosing Your Topic Researching the Topic

Answered: 1 week ago

Question

The Power of Public Speaking Clarifying the

Answered: 1 week ago