Floyd Warshall Algorithm, Unrecognized errors

416 views Asked by At

Having problems in taking input to this program. Also, don't know how to fill in those blank functionstextBox1_TextChanged, textBox9_TextChanged. Form1_Load.Also what are these; (object sender, EventArgs e)? Help would be appreciated! This is how far I could get with the least experience I have in c#.

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;

 namespace Floyd_Warshall
 {
 public partial class Form1 : Form
 {
    string ab="";
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void textBox9_TextChanged(object sender, EventArgs e)
    {
        ab = textBox9.Text;
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }
    private void button10_Click(object sender, EventArgs e)
    {
        int matrixDimention = Convert.ToInt32(ab);
        int[,] intValues = new int[matrixDimention, matrixDimention];
        string[] splitValues = textBox1.Text.Split(',');
        int[,] pathS = new int[matrixDimention, matrixDimention];
        for (int i = 0; i < splitValues.Length; i++)
        {
            intValues[i / (matrixDimention), i % (matrixDimention)] =  Convert.ToInt32(splitValues[i]);
            pathS[i / (matrixDimention), i % (matrixDimention)] = -1;
        }
            string displayString = "";
        for (int inner = 0; inner < intValues.GetLength(0); inner++)
        {
            for (int outer = 0; outer < intValues.GetLength(0); outer++)
                displayString += String.Format("{0}\t", intValues[inner, outer]);
                displayString += Environment.NewLine;
        }
        int n = (int)Math.Pow(matrixDimention, 2);
        string strn = n.ToString();

        MessageBox.Show("matrix" + strn + "in" + strn + "is\n\n\n" + displayString);

 for (int k = 1; k < n+1; k++)
 for (int i = 1; i < n+1; i++)
    for (int j = 1; j < n+1; j++)
        if (intValues[i, j] > intValues[i, k] + intValues[k, j]){                
            intValues[i, j] = intValues[i, k] + intValues[k, j];
            pathS[i,j] = k;
            string str_intvalues = intValues[i, j].ToString();
            MessageBox.Show("Shortest Path from i to j is: " + str_intvalues);
        }
        else{                
            string str_intvalues = intValues[i, j].ToString();
            MessageBox.Show("Shortest Path from i to j is: " + str_intvalues);
        }
    }


    }
}
1

There are 1 answers

0
Soner Gönül On BEST ANSWER

After a lot of comments, I'm so glad finally we learn what is the error and which line gets it.

But I'm still suspect about your splitValues[i] is " " or "". Anyway, I try to answer both case.

If your splitValues[i] is "", that means one of your array value is empty string after that line;

string[] splitValues = textBox1.Text.Split(',');

In this case, you can use StringSplitOptions.RemoveEmptyEntries enumeration like;

string[] splitValues = textBox1.Text.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);

If your splitValues[i] is " ", then you need to replace your spaces in your string with String.Replace method like;

string[] splitValues = textBox1.Text.Replace(" ", "").Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);