Sum the values on a diagonal of a 3x3x3 matrix : Array Dimension : Collections Data Structure C# Source Code


Custom Search

C# Source Code » Collections Data Structure » Array Dimension »

 

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

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Collections Data Structure
» Array Dimension