static void Main(string[] args)
{
int rowIn = 0;
int[][] parts2 = new int[15][];//jagged output Array
//The txt-file has 1 number in the top line and every next line has 1 number more, all seperated by a space
foreach (string line in File.ReadAllLines("inputProblem18.txt"))//put the txt file in the same directory as the exe file
{
int columnIn = 0;
string[] parts = line.Split(' ');
int[] nums = new int[parts.Length];
foreach (string part in parts)
{
nums[columnIn] = Int32.Parse(parts[columnIn]);
parts2[rowIn] = nums;//fill the jagged output array
Console.Write(nums[columnIn] + " ");
columnIn++;
}
Console.WriteLine();
rowIn++;
}
for (int rowOut = 14; rowOut >= 1; rowOut--)
{
for (int columnOut = rowOut-1; columnOut >= 0; columnOut--)
{
parts2[rowOut - 1][columnOut] += Math.Max(parts2[rowOut][columnOut], parts2[rowOut][columnOut + 1]);
Console.Write(parts2[rowOut - 1][columnOut] + " ");//I got the answere here
Console.Write(rowOut - 1 + " " + columnOut + " ");
}
Console.WriteLine();
}
// The line below is causing me the error
Console.WriteLine("The greatest sum from top to bottom is: {1}", parts2[0][0]);
Console.ReadKey();
}
I'm working on Project Euler, a nice site with nice programming problems. I used a jagged array for the first time in my live :-) so I think I don't understand it quit well.
I want to display the answer, the value of a jagged array at position [0][0] at the end of the file but I got an error and I don't know why. In the for loop I got the right value.
This is the error message :
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
You are using {1}
to display parts2[0][0]
. Since this is the first variable, this should be {0}
, not 1. The argument list can be considered as an array and starts at index 0.
Change this :
// Notice this ----------------------------------------v
Console.WriteLine("The greatest sum from top to bottom is: {1}", parts2[0][0]);
// Boom !
// Index (zero based) must be greater than or equal
// to zero and less than the size of the argument list.
To this :
// Notice this ----------------------------------------v
Console.WriteLine("The greatest sum from top to bottom is: {0}", parts2[0][0]);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments