I need the program in C Language. Ferry Loading Before bridges were common, ferr
ID: 3822940 • Letter: I
Question
I need the program in C Language.
Ferry Loading Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry. There is an l-meter-long ferry that crosses the river. A car may arrive at either river bank to be transported by the ferry to the opposite bank. The ferry travels continuously back and forth between the banks so long as it is carrying a car or there is at least one car waiting at cither bank. Whenever the ferry arrives at one of the banks, it unloads its cargo and loads up cars that are waiting to cross as long as they fit on its deck. The cars are loaded in the order of their arrival and the ferry's deck accommodates only one lane of cars. The ferry is initially on the left bank where it had mechanical problems and it took quite some time to fix it. In the meantime, lines of cars formed on both banks that wait to cross the river. The first line of input contains c, the number of test cases. Each test case begins with the number l, a space and then the number m. m lines follow describing the cars that arrive in this order to be transported. Each line gives the length of a car (in centimeters), and the bank at which the car awaits the ferry ("left" or "right"). For each test case, output one line giving the number of times the ferry has to cross the river in order to serve all waiting cars. Sample input 4 20 4 380 left 720 left 1340 right 1040 left 15 4 380 left 720 left 1340 right 1040 left 15 4 380 left 720 left 1340 left 1040 left 15 4 380 right 720 right 1340 right 1040 right Output for sample input 3 3 5 6Explanation / Answer
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void)
{
int tc,ferrySize, noOfCars;
char **bank;
int i=0;
int *crossing;
int *cars;
int tmp;
char ferrySide[7]="left";
scanf("%d", &tc); //read number of testcases
crossing=(int *)malloc(sizeof(int) * tc);
for(i=0;i<tc;i++)
crossing[i]=0;
for(int i=0;i<tc;i++)
{
crossing[i]=1;
scanf("%d %d", &ferrySize, &noOfCars);
ferrySize *= 100; //convert into cm
cars=(int*)malloc(sizeof(int) * noOfCars);
bank=(char **) malloc(sizeof(char *) * noOfCars);
for(int j=0;j<noOfCars;j++)
{
bank[j]=(char *)malloc(sizeof(char) * 7);
scanf("%d %s", &cars[j],bank[j]);
}
tmp=ferrySize;
strcpy(ferrySide,"left");
for(int j=0;j<noOfCars;){
if(cars[j] < tmp && strcmp(ferrySide,bank[j])==0){
tmp=tmp-cars[j];
j++;
}
else{
//The ferrysize is full. Hence change the ferry side and unload the cars
// or the ferry has to go to other side. so change the side
crossing[i]++;
if(strcmp(ferrySide,"left") == 0)
strcpy(ferrySide,"right");
else
strcpy(ferrySide,"left");
tmp=ferrySize;
}
}
free(cars);
free(bank);
}
for(int i=0;i<tc;i++)
{
printf("TC:%d, ferry Crossings over river: %d ", i+1, crossing[i]);
}
return 0;
}
Output:
4
20 4
380 left
720 left
1340 right
1040 left
15 4
380 left
720 left
1340 right
1040 left
15 4
380 left
720 left
1340 left
1040 left
15 4
380 right
720 right
1340 right
1040 right
TC:1, ferry Crossings over river: 3
TC:2, ferry Crossings over river: 3
TC:3, ferry Crossings over river: 5
TC:4, ferry Crossings over river: 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.