开发者

run time error:index out of range exception was unhandled

开发者 https://www.devze.com 2023-01-31 22:10 出处:网络
in the code below i want to implement the floyd algorithm,the user enter some numbers in textboxand output should be the shortest distances that can go from node i to node j.but when i enter for examp

in the code below i want to implement the floyd algorithm,the user enter some numbers in textbox and output should be the shortest distances that can go from node i to node j.but when i enter for example 4 number it gives run time error:out of range on the line i commented below,and i debugged it,i understood that k,i just take 1 value and doesnt take another values less than n+1 but j can take all of the values less than n+1,what can i solve this problem?thanks so much

edited: thanks for your answers i corrected that

 private void button10_Click(object sender, EventArgs e)
 {

    string ab = textBox11.Text;

    int matrixDimention = Convert.ToInt32(ab);

    int[,] intValues = new int[matrixDimention, matrixDimention];

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

    for (int i = 0; i < splitValues.Length; i++)

        intValues[i / (matrixDimention), i % (matrixDimention)] = Convert.ToInt32(splitValues[i]);
    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=matrixDimension

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

////before this line i wrote the codes to get the numbers that user enter in textbox and put it in an 2d array
    for (int k = 0; k < n; k++)/// k just take 1 in debugging!!!

        for (int i = 0; i < n; i++)///i just take 1 in debugging!!!

            for (int j = 0; j < n; j++)///j took the values true

       /*i took that error on this line  */ if (intValues[i, j] > intValues[i, k] + intValues[k, j])
                {
                    intValues[i, j] = intValues[i, k] + intValues[k, j];

                    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);

                }
}


Most programming languages, including C#, start counting array cells from position 0.

This means that if you have an array of size N, you get indexes from 0 to N-1.

So all your loops should look like this:

for (int j = 0; j < n; j++)

AND, as I looked at the code again, I realized that you intValues is a 2D matrix with the size matrixDimention to each side. BUT n is of the size (int)Math.Pow(matrixDimention, 2) .

So if you try to access intValues when k passes matrixDimention you'll get out of bounds.


your matrixDimention is used for initializing intValues, but you using int n = (int)Math.Pow(matrixDimention, 2); for iterating over your matrix, you should just use matrixDimention in your for loops:

for (int k = 0; k < matrixDimention; k++)
 ...
0

精彩评论

暂无评论...
验证码 换一张
取 消