Sum the values on a diagonal of a 3x3x3 matrix : Multi Dimensional Array : Data Structure C# Examples


C# Examples » Data Structure » Multi Dimensional Array »

 

Sum the values on a diagonal of a 3x3x3 matrix









    
using  System;  
  
class  MainClass  {    
    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);  
    }    
}
    
   
  
   



Output

Sum of first diagonal: 42


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Structure
» Multi Dimensional Array