I don't know how to finish it.
This is what is it suppose to do.
With this text file.

[/img]
This is what is it suppose to do.
With this text file.

Code:
16 Atlanta Birmingham Boston Chicago Dallas Detroit Kansas City Los Angeles Memphis Minneapolis Omaha Orlando Richmond San Francisco Seattle St. Louis Atlanta,Chicago:718 Atlanta,Dallas:781 Atlanta,Orlando:439 Birmingham,Atlanta:146 Birmingham,Detroit:723 Birmingham,Richmond:678 Boston,Atlanta:1099 Boston,Detroit:716 Boston,Memphis:1311 Chicago,Atlanta:718 Chicago,Boston:983 Chicago,Kansas City:526 Dallas,Atlanta:781 Dallas,Kansas City:554 Dallas,San Francisco:1734 Detroit,Boston:716 Detroit,Memphis:743 Detroit,Minneapolis:689 Kansas City,Chicago:526 Kansas City,Dallas:554 Kansas City,St. Louis:249 Los Angeles,Dallas:1437 Los Angeles,San Francisco:382 Los Angeles,Seattle:1135 Memphis,Boston:1311 Memphis,Detroit:743 Memphis,Omaha:704 Minneapolis,Detroit:689 Minneapolis,Richmond:1206 Minneapolis,St. Louis:562 Omaha,Kansas City:183 Omaha,Memphis:704 Omaha,San Francisco:1668 Orlando,Atlanta:439 Orlando,Memphis:826 Orlando,St. Louis:993 Richmond,Atlanta:532 Richmond,Birmingham:678 Richmond,Minneapolis:1206 San Francisco,Dallas:1734 San Francisco,Los Angeles:382 San Francisco,Omaha:1668 San Francisco,Seattle:808 Seattle,Dallas:2201 Seattle,Los Angeles:1135 Seattle,San Francisco:808 St. Louis,Birmingham:499 St. Louis,Chicago:297 St. Louis,Kansas City:249 St. Louis,Minneapolis:562
Code:
// Finds the shortest path between 2 user inputed points.
using System;
using System.IO;
public class Proj5{
public static int[,] mileage;
public static bool[] visited;
public static int[] prev;
public static int[] dist;
public static int size;
public static string[] cities;
public static void Main(){
Console.Write("Enter the name of the input file: ");
string file = Console.ReadLine();
Console.WriteLine();
StreamReader sr = new StreamReader(file);
size = Convert.ToInt32(sr.ReadLine());
sr.ReadLine();
cities = new string[size];
mileage = new int[size,size];
visited = new bool[size];
prev = new int[size];
dist = new int[size];
for(int i = 0; i < size; i++) {
string line = sr.ReadLine();
cities[i] = line;
}
char[] delim = {',' , ':'};
// int[] miles = new int[size];
string meh = "";
while (meh != null) {
if (meh == "")
{
}
else
{
string[] tokens = meh.Split(delim);
int x = Proj5.index(tokens[0]);
int y = Proj5.index(tokens[1]);
mileage[x,y] = Convert.ToInt32(tokens[2]);
}
meh = sr.ReadLine();
}
/*for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
Console.Write(mileage[i,j] + "\t");
}
Console.Write("\n");
} */
sr.Close();
//for (int i = 0; i < size; i++) {
// Console.WriteLine(cities[i]);
//}
char k = 'Y';
while(k == 'Y') {
bool first = false;
bool second = false;
string cityFirst = " ";
string citySecond = " ";
while (first == false || second == false) {
Console.Write("Enter the first city: ");
cityFirst = Console.ReadLine();
Console.Write("Enter the second city: ");
citySecond = Console.ReadLine();
Console.WriteLine();
for (int i = 0; i < size; i++) {
if (cityFirst == cities[i]) {
first = true;
}
}
for (int i = 0; i < size; i++) {
if (citySecond == cities[i]) {
second = true;
}
}
if (first == true && second == true) {
break;
}
Console.WriteLine("Invalid city name(s)");
Console.WriteLine();
Console.Write("Another pair? (Y/N): ");
k = Convert.ToChar(Console.ReadLine());
if (k == 'Y') {
}
if (k == 'N'){
break;
}
}
if (k == 'N'){
break;
}
Console.WriteLine("Valid");
// This is where all the code goes after everything is valid
Console.Write("Another pair? (Y/N): ");
k = Convert.ToChar(Console.ReadLine());
}
// ask
//should read the input file, create the arrays,
//get pairs of cities from the user, and call
//appropriate methods to get the value of the shortest path
}
/*
* if the string equals one of the words in cities array, then it will return its index
*
* The parameters are a single string and it is being compared to the cities array
* The placement in the array is being returned
*/
public static int index(string name){
for (int i = 0; i < cities.Length; i++) {
if (name == cities[i]) return i;
}
//should return the index of name in the cities array
//REMOVE WHEN COMPLETE
return -1;
}
public static void init(string start)
{
for (int i= 0; i < size; i++) {
visited[i] = false;
dist[i] = 0;
prev[i] = -1;
}
dist[Proj5.index(start)] = 0;
// Set distance to -1 for all cities
//Set distance to 0 for the start city
//Set visited to false for all cities
//Set previous to -1 for all cities
//should initialize the values in the dist and prev arrays
//start is the name of the starting city
}
/*
public static int minIndex()
{
//should return the index of the unvisited city
//with minimum dist
//REMOVE WHEN COMPLETE
return 0;
}
*/
/*
* Checks to see if every node has been visited
*
* There are no parameters
* It returns true or false depending on whether they have all been visted or not
*/
public static bool done()
{
//returns whether all cities have been visited
for (int i = 0; i < size; i++)
{
if (visited[i] == false) return false;
}
//REMOVE WHEN COMPLETE
return true;
}
/*
* prints out the path from start to stop and the distance
*
* The parameters are the starting city and the ending city
* the writeline of the distance and previous city is being returned.
*
*/
/*
public static void print(string start, string stop)
{
// after everything
// backward through previous array to get final path
//prints the shortest path from start city
//to stop city, plus the total distance
}
*/
/*
* implements dijkstra's algorithm starting at start city
*
* The start city is the only parameter
* the previous array is being returned
*/
public static void dijkstra(string start)
{
Proj5.init(start);
while(Proj5.done() == false) {
}
//call the init, minIndex, and done methods to help you
}
}
Comment