Can't get the value from a jagged array in the place I want but I get it on an other place

Han
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.

Cid

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get the current SparkSession in any place of the codes?

How can I get latitude and location from a customer by making him to place a marker in google maps?

How can I get every character from an array and place it into another one?

Can't get the resolution I want with opencv

How can I simplify this code to get a correct decimal place for a number?

How can I get exact sub category name about a place from Google Places API in Android/Java?

How can I get the queue to snap-grid-flip into place?

I want to place five imagebuttons horizontally, but when I do I only get 4 buttons in some devices

Where do I properly place sqlite database so it wouldn't get removed on app update from appstore?

Is there a place where I can get all the popular and latest javascript library?

I want to get value from other cs file

how can I get the unit in last place(ULP) in PHP

How can I get the Unit Number to be displayed in place of the "ID"

i want to get a specific value from this php array

Using iOS storyboarding I can't place label in spot I want on view controller

I can't get the value I want from a combobox

Can't get the Android layout I want

Can't get the data I want on Python

How can I get values from address of Place Picker?

onPressed of an Image I want to place that image in other page's container in flutter, How can I achieve this?

How can I get the data from the specific place

i can't get the value from an input

I can't get the object value that i want

I can't get the output I want from the function. The output I get is NoneType

How do I get a query to return a "1" value in place of text

I want to get the name that is in the 1st place in a list

I want to get 3 random data (different from each other) using UseSelector/userReducer but I can't access the data

How can I get a value from an array?

i get the json data from the external file and i can console log them. but i cannot place the values on a google map