﻿ C# Sharp exercises: Calculate the sum of all the intgers of a rectangular matrix except those integers which are located below an intger of value 0 - w3resource

C# Sharp Exercises: Calculate the sum of all the intgers of a rectangular matrix except those integers which are located below an intger of value 0

C# Sharp Basic: Exercise-60 with Solution

Write a C# program to calculate the sum of all the intgers of a rectangular matrix except those integers which are located below an intger of value 0.

Sample Example:
matrix = [[0, 2, 3, 2],
[0, 6, 0, 1],
[4, 0, 3, 0]]
Eligible integers which will be participated to calculate the sum -
matrix = [[X, 2, 3, 2],
[X, 6, X, 1],
[X, X, X, X]]
Therefore sum will be: 2 + 3 + 2 + 6 + 1 = 14

Sample Solution:

C# Sharp Code:

``````using System;
public class Example
{
public static int sum_matrix_elements(int[][] my_matrix)
{
int x = 0;
for (int i = 0; i < my_matrix[0].Length; i++)
for (int j = 0; j < my_matrix.Length && my_matrix[j][i] > 0; j++)
x += my_matrix[j][i];

return x;
}

public static void Main()
{
Console.WriteLine(sum_matrix_elements(
new int[][] {
new int[]{0, 2, 3, 2},
new int[]{0, 6, 0, 1},
new int[]{4, 0, 3, 0}
}));
Console.WriteLine(sum_matrix_elements(
new int[][] {
new int[]{1, 2, 1, 0 },
new int[]{0, 5, 0, 0},
new int[]{1, 1, 3, 10 }
}));
Console.WriteLine(sum_matrix_elements(
new int[][] {
new int[]{1, 1},
new int[]{2, 2},
new int[]{3, 3},
new int[]{4, 4}
}));
}
}
``````

Sample Output:

```14
10
20
```

Flowchart:

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

﻿

New Content: Composer: Dependency manager for PHP, R Programming