I have a 2D Array in C# with user input to fill the Array. I need help to find the sum for every column and row.
var input = Console.ReadLine();
var n = int.Parse(input);
int[,] intArr = new int[n, 3];
for (int i = 0; i < n; i )
{
input = Console.ReadLine();
var parts = input.Split(' ');
for (int j = 0; j < 3; j )
{
intArr[i, j] = int.Parse(parts[j]);
}
}
if (n == 1)
{
Console.WriteLine("Sum of column " Convert.ToString(n) ": " (intArr[n - 1, 0] intArr[n - 1, 1] intArr[n - 1, 2]));
Console.WriteLine("Row1: " (intArr[n - 1, 0]));
Console.WriteLine("Row2: " (intArr[n - 1, 1]));
Console.WriteLine("Row3: " (intArr[n - 1, 2]));
}
if (n == 2)
{
Console.WriteLine("Sum of column " Convert.ToString(n - 1) ": " (intArr[n - 2, 0] intArr[n - 2, 1] intArr[n - 2, 2]));
Console.WriteLine("Sum of column " Convert.ToString(n) ": " (intArr[n - 1, 0] intArr[n - 1, 1] intArr[n - 1, 2]));
Console.WriteLine("Row 1: " (intArr[n - 2, 0] intArr[n - 1, 0]));
Console.WriteLine("Row 2: " (intArr[n - 2, 1] intArr[n - 1, 1]));
Console.WriteLine("Row 3: " (intArr[n - 1, 2] intArr[n - 1, 1]));
}
}
User input:
2
1 2 3
4 5 6
The output:
Sum of column 1: 6
Sum of column 2: 15
Row 1: 5
Row 2: 7
Row 3: 11
I want this output for N columns enter by user, but I am stuck! Thank you!
CodePudding user response:
Not sure, is your problem that you get an undesired result or that you have a model with a fixed number of elements? An array is a fixed size data container, so if you want any number of columns the architecture perhaps is where you want to change away from array. Anyway, problem 1 is that you let user chose how many rows they want, but now how many column, so presuming there will be 3 conflicts with you data. So you have on array the .getUpperBound() for that which takes a range parameter and this way we can use a dynamic strucure
//From:
for (int i = 0; i < n; i )
{
input = Console.ReadLine();
var parts = input.Split(' ');
for (int j = 0; j < 3; j )
{
intArr[i, j] = int.Parse(parts[j]);
}
}
//TO:
var rows = new List<int[]>();
for (int i = 0; i < n; i )
{
input = Console.ReadLine();
var parts = input.Split(' ');
var listOf = new List<int>();
for (int j = 0; j < parts.GetUpperBound(0); j )
{
listOf.Add(int.Parse(parts[j]));
}
rows.Add(listOf.ToArray());
}
from https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate?view=net-5.0 You get the aggregation capability
[Fact]
public void SumExerciseTest()
{
/*
The output:
Sum of column 1: 6
Sum of column 2: 15
Row 1: 5
Row 2: 7
Row 3: 11 => 9
*/
var testData = new[] { "1 2 3", "4 5 6" };
var input = "2"; // Console.ReadLine();
var n = int.Parse(input);
var rows = new List<int[]>(n);
for (int i = 0; i < n; i )
{
input = testData[i];
var parts = input.Split(' ');
var listOf = new List<int>();
for (int j = 0; j <= parts.GetUpperBound(0); j )
{
listOf.Add(int.Parse(parts[j]));
}
rows.Add(listOf.ToArray());
}
var rowSums = new List<int>(n);
foreach (int[] row in rows)
{
rowSums.Add(row.Aggregate((result, item) => result item));
}
int maxLength = 0;
var rowLengths = new List<int>(n);
foreach (int[] row in rows)
{
rowLengths.Add(row.Length);
}
maxLength = rowLengths.Max();
int[] columnSums = new int[maxLength];
for (int idx = 0; idx < columnSums.Length; idx ){
foreach (var row in rows)
{
if (row.GetUpperBound(0) >= idx)
{
columnSums[idx] = row[idx];
}
}
}
int counter = 0;
//Outputting:
foreach(var sum in rowSums)
{
counter = 1;
System.Diagnostics.Debug.WriteLine($"Row {counter}: {sum}");
}
counter = 0;
foreach(var sum in columnSums)
{
counter = 1;
System.Diagnostics.Debug.WriteLine($"Column {counter}: {sum}");
}
CodePudding user response:
Well, let's implement (extract) methods for summing arbitrary column and row:
private static int SumColumn(int[,] array, int column) {
if (array == null)
throw new ArgumentNullException(nameof(array));
if (column < 0 || column >= array.GetLength(1))
throw new ArgumentOutOfRangeException(nameof(column));
int result = 0;
for (int r = 0; r < array.GetLength(0); r)
result = array[r, column];
return result;
}
private static int SumRow(int[,] array, int row) {
if (array == null)
throw new ArgumentNullException(nameof(array));
if (row < 0 || row >= array.GetLength(0))
throw new ArgumentOutOfRangeException(nameof(row));
int result = 0;
for (int c = 0; c < array.GetLength(1); c)
result = array[row, c];
return result;
}
then you can put
for (int c = 0; c < intArr.GetLength(1); c)
Console.WriteLine($" Sum of column {c 1}: {SumColumn(intArr, c)}");
for (int r = 0; r < intArr.GetLength(0); r)
Console.WriteLine($" Sum of row {r 1}: {SumRow(intArr, r)}");