Sum the values on a diagonal of a 3x3x3 matrix : Array Dimension « Collections Data Structure « C# / C Sharp






Sum the values on a diagonal of a 3x3x3 matrix

Sum the values on a diagonal of a 3x3x3 matrix
 
// Sum the values on a diagonal of a 3x3x3 matrix. 

using System; 
 
public class ThreeDMatrix {  
  public static void Main() {  
    int[,,] m = new int[3, 3, 3]; 
    int sum = 0; 
    int n = 1; 
 
    for(int x=0; x < 3; x++) {
      for(int y=0; y < 3; y++) {
        for(int z=0; z < 3; z++) { 
          m[x, y, z] = n++; 
        }  
      }  
    }  
 
 
    sum = m[0,0,0] + m[1,1,1] + m[2, 2, 2]; 
 
    Console.WriteLine("Sum of first diagonal: " + sum); 
  }  
}

           
         
  








Related examples in the same category

1.Demonstrate a two-dimensional arrayDemonstrate a two-dimensional array
2.Initialize a two-dimensional arrayInitialize a two-dimensional array
3.Demonstrate jagged arraysDemonstrate jagged arrays
4.Use the Length array property on a 3-D arrayUse the Length array property on a 3-D array
5.Call GetLength for two dimenional array
6.Demonstrate Length with jagged arraysDemonstrate Length with jagged arrays
7.illustrates the use of a two-dimensional rectangular arrayillustrates the use of a two-dimensional rectangular array
8.initialize a two-dimensional rectangular array, and use the array properties and methodsinitialize a two-dimensional rectangular array, and use the array properties and methods
9.the use of a three-dimensional rectangular arraythe use of a three-dimensional rectangular array
10.the use of a jagged arraythe use of a jagged array
11.Uses a two-dimensional array to store grades for studentsUses a two-dimensional array to store grades for students
12.Uses a jagged array to store sales figuresUses a jagged array to store sales figures
13.Multidimensional and Jagged Arrays:Jagged ArraysMultidimensional and Jagged Arrays:Jagged Arrays
14.Multi dimensional Arrays 1Multi dimensional Arrays 1
15.Catch OutOfMemoryException
16.Catch IndexOutOfRangeException Exception
17.Defines a 2D Array