#include<iostream>
#include<stdlib.h>
using namespace std;

/* Trace d'une matrice carree */

double trace(double** M, int n){
  int i;
  double tr;

  tr = 0.;
  for (i = 0; i < n; i++)
    tr += M[i][i];
  return tr;
}

int main(){
  const int n = 3;
  double** M;
  int i,j;
 
  M = (double**)malloc(n * sizeof(double*));
  for (i = 0; i < n; i++)
    M[i] = (double*)malloc(n * sizeof(double));
  for (i = 0; i < n; i++){
    for (j = 0; j < n; j++){
      M[i][j] = (i+1)*(j+2);
      cout << M[i][j] << " ";
    }
    cout << endl;
  }
  cout << "Trace : " << trace(M,n) << endl;
  for (i = 0; i < n; i++)
    free(M[i]);
  free(M);
  return 0;
}
