#include< stdio.h>
#include< stdlib.h>
#include < iostream>
using namespace std;
typedef struct matrix
{
int line;
int column;
int** data;
}matrix;
struct matrix creer_matrice(int row, int column){
struct matrix m;
m.row = row;
m.column = column;
m.data = (int**)malloc(m.line*sizeof(int *));
for(int i = 0; i< m.row; ++i){
// initialize the boxes to 0 with calloc
m.data[i] = (int *)calloc(m.column,sizeof(int));
}
return m;
}
void afficher_matrice(matrix M){
for(int i = 0; i < M.line; i++) {
for(int j = 0; j < M.colonne; j++) {
cost < < M.data[i][j] ;
cost < < "\t";
}
//line break
cost < < "\n";
}
cost < < "\n";
}
struct product matrix(matrix A, matrix B)
{
matrix C = creer_matrice(A.row, B.column);
for(int I = 0; I < A.line; I++)
for(int J = 0; J < B.column; J++)
{
C.data[I][J] = 0;
for(int K = 0; K < A.column; K++)
{
C.data[I][J] += A.data[I][K] * B.data[K][J];
}
}
return C;
}
int main(int argc, char *argv[])
{
/*
matrix A
*/
matrix A = creer_matrice(4,3);
srand(time(NULL));
for(int i=0; i< A.line; i++)
for(int j=0; j< A.column; j++)
//Generate random numbers between 0 and 20 and subtract 10
//to get negative numbers
//e.g.: if rand generates 74 then 74-100 = -36
A.data[i][j] =rand()%20-10;
afficher_matrice(A);
/*
2nd matrix
*/
matrix B = creer_matrice(3.5);
srand(time(NULL));
for(int i=0; i< B.line; i++)
for(int j=0; j< B.column; j++)
//Generate random numbers between 0 and 20 and subtract 10
//to get negative numbers
//e.g.: if rand generates 74 then 74-100 = -36
B.data[i][j] =rand()%20-10;
afficher_matrice(B);
//product
//matrix C = product(A,B);
//afficher_matrice(C);
afficher_matrice(power(A));
system("pause");
}