You are ready to travel long distance by road for Spring break to visit your fam
ID: 3803467 • Letter: Y
Question
You are ready to travel long distance by road for Spring break to visit your family. Assume that an array called distances[] already has the distance to each gas station along the way in sorted order. Let us simplify and assume that your car can travel max 200 miles with a full tank of gas. You are starting the trip with a full tank as well. Now, your goal is to find the gas stations to refill your tank so that you can reach home with minimal # of gas stops (For example, if the array has 10.5, 34.5, 50.4, 89.4, 110.5, 150.2, 190.45, 230.1, 270.5, .... you need to make the first stop and put gas at the gas station @ 190.45, right?) BTW, it is OK to reach home with minimal gas or even run out of gas exactly when you pull into your home, but do not run out of gas along the way! Your program needs to output just the total # of gas stops. double distances[]; int numStations;//# of entries stored in distances[] arrayExplanation / Answer
C++ Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter the number of gas stations in way: ";
cin>>n;
double distance[n];
for(int i=0;i<n;i++)
{
cout<<"Enter the value:";
cin>>distance[i];
}
double fuel=200.0;
int i,j,k=0;
for(i=0;i<n;i++)
{
if(distance[i]>fuel)
{
fuel=200.0+distance[i-1];
k++;
}
}
cout<<"The number of gas stops would be:"<<k<<endl;
return 0;
}
Output:
Enter the number of gas stations in way: 20
Enter the value:10.5
Enter the value:34.5
Enter the value:90.4
Enter the value:190.45
Enter the value:230.1
Enter the value:350.1
Enter the value:390.3
Enter the value:420.5
Enter the value:490.25
Enter the value:520.35
Enter the value:610.5
Enter the value:690.5
Enter the value:740.4
Enter the value:810.5
Enter the value:900.1
Enter the value:1020.5
Enter the value:1100.4
Enter the value:1206.5
Enter the value:1300.0
Enter the value:1360.0
The number of gas stops would be:8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.