Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Calculate the sum of all the integers 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 integers 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:

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

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to check whether it is possible to create a strictly increasing sequence from a given sequence of integers as an array.
Next: Write a C# program to sort the integers in ascending order without moving the number -5.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.