Matrix Calculation in C++: Sum of the First Diagonal

using namespace std;
#include< stdio.h>
#include< stdlib.h>
#include < iostream>
#include < time.h>

int somme_diagonale(int **M,int n){
    int sum=0;
    for(int i = 0; i <   ; i++){
              sum += M[i][i];
    }
    return sum;  
}
void display(int** Mat, int N, int M){
      for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++) {
            Cost < < Mat[i][j];
            Cost < < "\t";
        }
        Cost < < "\n";
     }
      Cost < < "\n";
}
main()
{
      int n=3;
      int** M  = new int* [ n ];
      for (int i=0; i < n; i++)
      M[i] = new int[ n ];
      srand(time(NULL));
      for(int i=0; i< n; i++)
      for(int j=0; j< n; j++)
M[i][j] =rand()%10;
      display(M,n,n);
      printf("sum of the first diagonal: %d\n",somme_diagonale(M,n));
      system("pause");
}
Advertisement

AdBlock Detected

Please disable your ad blocker and refresh the window to use this website.