Berke Tabak
Berke Tabak

Reputation: 21

Finding sum a multi-dimensional array's reverse diagonal elements

this is my multi dimensional array.

1-2-3-4

5-6-7-8

9-10-11-12

13-14-15-16

And i want find sum of reverse diagonal elements (From:right top To:left bottom) (4 -> 7 -> 10 -> 13)

Here is my code, it gives an error but i still can't find why.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sketchboard
{
    class Program
    {

        static int reversediagonaladder (int[,] a)
        {
            int sum = 0;

            for(int row=a.GetLength(0) ; row>0 ;  row--)
            {
                for(int column=a.GetLength(1) ; column>0 ; column--)
                {
                    if (row == column)
                        sum += a[row , column];
                }

            } 


            Console.WriteLine($"{sum}");
            return sum;
        }

        static void Main(string[] args)
        {

            int[,] a ={ {1,2,3,4},
                        {5,6,7,8},
                        {9,10,11,12},
                        {13,14,15,16 } };

            reversediagonaladder(a);

           


            Console.ReadLine(); // added for hold console on screen
        }
    }
}

Upvotes: 0

Views: 1127

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39142

If you don't want to deal with the -1, use GetUpperBound instead.

Here's an example doing as Self suggested with just one for loop:

public static void Main(string[] args)
{
    int[,] a ={ {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
                {13,14,15,16} };

    int sum = reversediagonaladder(a);
    Console.WriteLine("sum = " + sum);

    Console.WriteLine();
    Console.WriteLine("Press Enter to Quit");
    Console.ReadLine();
}

static int reversediagonaladder(int[,] a)
{
    // code assumes a SQUARE matrix
    int sum = 0;
    for (int row=0, col=a.GetUpperBound(1); row<=a.GetUpperBound(0) && col>=0; row++, col--)
    {
        sum += a[row, col];
    }
    return sum;
}

Upvotes: 1

Rand Random
Rand Random

Reputation: 7440

Your loop starts with a.GetLength(1) or a.GetLength(0) in both cases this will resolve to 4 but there is no index with 4 since indexes start with 0.

So index will be 0, 1, 2 or 3 which equals to a length of 4.

To fix your problem you need to substract 1 from the length.

Currently you are also skipping the index 0, I don't know why you are doing so, maybe it is an error aswell, to fix this you have to change the break condidtion of the for loop from > 0 to >= 0.

This should work

for (int row = a.GetLength(0) - 1; row >= 0; row--)
{
    for (int column = a.GetLength(1) - 1; column >= 0; column--)
    {
        if (row == column)
            sum += a[row , column];
    }
}

Edit:

Here a version which actually does what the question claims it does, it also gets rid of the double for loop which should run more performant

var column = a.GetLength(1);
for (int row = 0; row < a.GetLength(0); row++)
{
    column--;
    var item = a[row, column];
    sum += item;
}

Here you can see it in action:

https://dotnetfiddle.net/S5imGs

Upvotes: 1

Related Questions