-1

Currently this displays a table with one row that just shows 0's and throws an IndexOutOfRangeException for the Console.Write(string.Format("{0,6} |", arr[j, i])); I want to print the information in each text file in a table format.

// StringToDouble is a method that converts the contents of the text file to double
public static double[] Year = StringToDouble("Year.txt");     
public static double[] TMax = StringToDouble("WS1_TMax.txt");
public static double[] TMin = StringToDouble("WS1_TMin.txt");
public static double[] AF = StringToDouble("WS1_AF.txt");
public static double[] Rain = StringToDouble("WS1_Rain.txt");
public static double[] Sun = StringToDouble("WS1_Sun.txt");

static void Main(string[] args)
{
    double[,] arr = new double[6, 1021]; // 1021 is the upper bound of my array

    int rowLength = arr.GetLength(0);
    int colLength = arr.GetLength(1);

    for (int i = 0; i < rowLength; i++)
    {
        for (int j = 0; j < colLength; j++)
        {
            // The arrays Year[], TMax[], etc... are declared outside the Main
            arr[i, 1] = Year[j];
            arr[i, 2] = TMax[j];
            arr[i, 3] = TMin[j];
            arr[i, 4] = AF[j];
            arr[i, 5] = Rain[j];
            arr[i, 6] = Sun[j];
        }
    }

    Console.WriteLine("  Years|  TMax |  TMin |  AF   |  Rain |  Sun    |");
    for (int i = 0; i < rowLength; i++)
    {
        for (int j = 0; j < colLength; j++)
        {
            Console.Write(string.Format("{0,6} |", arr[j, i]));
        }
        Console.Write(Environment.NewLine);
    }
    Console.ReadKey();
}
MrLogic
  • 9
  • 2
  • For one thing, the declaration of your array of doubles seems to be missing the identifier (`arr`?). – Peter - Reinstate Monica Apr 10 '16 at 20:13
  • Thanks, I did have it in my original code... Just didn't copy it over correctly. – MrLogic Apr 12 '16 at 16:44
  • Possible duplicate of: http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it – Mikanikal Apr 12 '16 at 17:02
  • Your code is incomplete still. You say that year, TMax, etc... are all declared outside the Main. How do we know they are declared with the correct number of elements? That could be the problem if they are not declared correctly. The other issue is, if you're going to use specific numbers to declare your arrays, just use those numbers in your For loops as well. No point trying to use GetLength(0) to get the length of an array if you declared it specifically. – Mikanikal Apr 12 '16 at 17:06
  • Is there a reason you add things to the array using [i,j], but when you display the results you use [j,i]? You define your array based on rowlength, colLength. When you do your Console.Writeline, you access the array by [colLength, rowLength] - the opposite way you use it write. – thorkia Apr 12 '16 at 19:42

1 Answers1

0

Your error message tells you where the problem is.

Console.Write(string.Format("{0,6} |", arr[j, i]));

You create a 2 dimensional array [6,1021].

The you create 2 variables to control writing to it:

int rowLength = arr.GetLength(0); //This will be 6
int colLength = arr.GetLength(1); //This will be 1021

But when you go to read the values, you access them backwards:

for (int i = 0; i < rowLength; i++)
{
   for (int j = 0; j < colLength; j++)
   {
      Console.Write(string.Format("{0,6} |", arr[j, i]));
   }
   Console.Write(Environment.NewLine);
}

i max is 6, and j is 1021. You will get an exception, because the max value of j is greater than the number of entries in the first dimension of the array (6).

Swap the variables like this:

for (int i = 0; i < rowLength; i++)
{
   for (int j = 0; j < colLength; j++)
   {
      Console.Write(string.Format("{0,6} |", arr[i, j]));
   }
   Console.Write(Environment.NewLine);
}
thorkia
  • 1,972
  • 1
  • 20
  • 26