0

Two-dimensional array consists of integer elements. Write the product of the elements of each line, greater than the arithmetic mean of the elements of the line into the new array minarray.

The code below returns corect sum and product but idk why it gives incorrect value to first element (index [0]) while second and third elements in minarray are correct. I have no idea where i have made a mistake.

My code

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

namespace Multidimensional3Tasks
{
  public static class Tasks
  {
     public static int[] Task3(int[,] array)
        {
            int rows = array.GetUpperBound(0) + 1;
            int columns = array.Length / rows;

            int[] minarray = new int[rows];
            
            for (int i = 0; i < rows; i++)
            {
                int sum = 0;
                for (int j = 0; j < columns; j++)
                {
                    sum += array[i, j];
                    int product = 1;
                    if (sum / rows < array[i, j])
                    {
                       product *= array[i, j];
                       minarray[i] = product;
                    }
                }
            }
            return minarray;
        }
    }
}

The test

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

namespace Multidimensional3Tasks.Tests
{
    [TestFixture]
    public class TaskTests
    {
     [Test]
        public void Task3_ReturnsCorrectValue()
        {
            int[,] array1 = new int[,] { { 10, 6, 5 },
                                        { 12, 3, 30 },
                                        { 6, 9 ,6 } };
            int[,] array2 = new int[,] { { 15, 20, 5 },
                                        { 15, 3, 9 },
                                        { 12, 35 ,6 } };
            int[,] array3 = new int[,] { { 2, 0, 7 },
                                        { 13, 15, 31 },
                                        { 10, 5 ,0 } };

            int[] expected1 = { 10, 30, 9 };
            int[] expected2 = { 300, 15, 35 };
            int[] expected3 = { 7, 31, 10 };

            var actual1 = Tasks.Task3(array1);
            var actual2 = Tasks.Task3(array2);
            var actual3 = Tasks.Task3(array3);

            Assert.AreEqual(expected1, actual1, "Task3 returns incorrect value.");
            Assert.AreEqual(expected2, actual2, "Task3 returns incorrect value.");
            Assert.AreEqual(expected3, actual3, "Task3 returns incorrect value.");
        }
    }
}
  • 2
    Now is a great time to [start using a debugger](https://stackoverflow.com/q/25385173/328193). This allows you to step through the code, line by line, as it executes and observe the exact runtime behavior and changing values of your variables. When you do this, which specific operation first produces an unexpected result? What were the exact runtime values used in that operation? What was the result? What result was expected? Why? – David May 16 '22 at 13:18
  • I'm newbie in programming, but i'll do my best to make myself clear. Values differ at index [0] in first test where expected result is 10 but I get 6. I think that`s because during second 'for' 'sum' is different through each iteration (10, 16, 21) but 'rows' always equals 3. During the last iteration when 'sum' = 21 and 'average' = 7 program doesn't go inside 'if' and leaves previous value for product ('sum' = 16, 'sum/rows' = 5, 'product' = 6). This problem isn't observed for second and third loop where values for product are both correct. – Alina Lutsenko May 16 '22 at 17:41
  • If you're not familiar with how to use a debugger, now is exactly the time to start looking into that. [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) is also a helpful article on the subject, from a high-level general mindset of debugging. Your goal is not to describe what you think the code should be doing or how you think the code should be failing, but to specifically test and observe the results of each individual operation in the code to identify the exact thing that isn't producing the result you expect. – David May 16 '22 at 18:07

0 Answers0