Calculating the stops between stations.
★★★The Victoria line is a London Underground line that runs between Brixton in south London and Walthamstow Central in the north-east, via the West End. It is printed in light blue on the Tube map and is one of the only two lines on the network to run completely underground. There are sixteen stations on the line. This program outputs the number of stops between two stations on a ticket on the Victoria line.
Write a program to output how many stops there are between two stations on the Victoria line.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
get_station that:"Invalid station.".calculate_stops that:get_station is called to read the first station.get_station is called again for the second station. Stations can be entered in any order.calculate_stops is called to calculate the number of stops.Enter station:
Stockwell
Enter station:
Pimlico
Stockwell to Pimlico is 2 stops.
Enter station:
Brixton
Enter station:
Stockwell
Brixton to Stockwell is 1 stop.
Enter station:
Paddington
Invalid station.
Enter station:
Stockwell
Enter station:
Pimlico
Stockwell to Pimlico is 2 stops.
// London Underground program
using System;
class Submission
{
---
// -------------------------
// Subprograms
// -------------------------
// Read a valid station name
static string get_station(string[] stations)
{
---
Console.WriteLine("Enter station:");
string station = Console.ReadLine();
while (Array.IndexOf(stations, station) == -1)
{
---
Console.WriteLine("Invalid station.");
Console.WriteLine("Enter station:");
station = Console.ReadLine();
---
}
---
return station;
---
}
---
// Calculate the number of stops between two stations
static int calculate_stops(string[] stations, string[] ticket)
{
---
int start_at = Array.IndexOf(stations, ticket[0]);
int stop_at = Array.IndexOf(stations, ticket[1]);
int stops = Math.Abs(start_at - stop_at);
return stops;
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
// Victoria line
string[] stations = {"Brixton", "Stockwell", "Vauxhall", "Pimlico", "Victoria", "Green Park", "Oxford Circus", "Warren Street", "Euston", "King's Cross", "Highbury & Islington", "Finsbury Park", "Seven Sisters", "Tottenham Hale", "Blackhorse Road", "Walthamstow Central"};
string[] ticket = {"", ""}; // The two stations on the ticket
ticket[0] = get_station(stations);
ticket[1] = get_station(stations);
int stops = calculate_stops(stations, ticket);
---
if (stops == 1)
{
---
Console.WriteLine($"{ticket[0]} to {ticket[1]} is {stops} stop.");
---
}
---
else
{
---
Console.WriteLine($"{ticket[0]} to {ticket[1]} is {stops} stops.");
---
}
---
}
}