Guest User

Untitled

a guest
May 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4. //function prototype
  5. int SumDiagonal(int ** array2d, int array2dsize);
  6.  
  7. int main()
  8. {
  9.     //declare a pointer to a pointer (2d array)
  10.     int **x;
  11.    
  12.     x = new int*[4]; //initialize as an pointer of size 4
  13.    
  14.     for(int i=0;i<4;i++)
  15.             x[i] = new int[4];//initialize each 4 pointers to arrays (2d array created)
  16.    
  17.     cout<<"The array is\n";
  18.        
  19.     for(int i=0;i<4;i++)
  20.     {   for(int j = 0;j<4;j++)
  21.             {
  22.             x[i][j] = j + 1;//put appropriate values
  23.             cout<<x[i][j]<<" ";//output values
  24.             }
  25.         cout<<endl;
  26.     }
  27.  
  28.     cout<<"The sum of the diagonal is:"<<SumDiagonal(x,4);//calculate sum
  29.    
  30.    
  31.    
  32.     return 0;//DONE
  33. }
  34.  
  35. int SumDiagonal(int **array2d, int array2dsize)
  36.     {
  37.     int sum=0;//sum is zero
  38.    
  39.     for(int i=0;i<array2dsize;i++)
  40.         sum+=array2d[i][i]; //add the diagonal elements (same index)
  41.    
  42.     return sum;//return the value
  43.     }
Add Comment
Please, Sign In to add comment