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.");
}
}
}