1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <algorithm>
#include <stdlib.h>
using namespace std;
void fill(int& value){
static int val = 0;
val++;
value = val;
}
void zero(int& value){
value = 0;
}
void mult(int* **A,int* **B,int* **result, const int SIZE) {
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
for (int k = 0; k < SIZE; k++)
(*result)[i][j] += B[0][i][k] * (*A)[k][j];
}
int main(){
int *vector;
const int SIZE = 3;
//reserve memory:
vector = (int*) malloc(sizeof(int) * SIZE);
for_each(vector,vector + SIZE,fill); //fill
//print
for(int i = 0 ; i < SIZE ; i++)
cout << vector[i] << ", ";
cout << endl << endl << endl;
//reserve memory for a pair of matrices:
int**matrix;
matrix = (int**) malloc(sizeof(int*) * SIZE );
for(int i = 0 ; i < SIZE ; i++ )
matrix[i] = (int*) malloc(sizeof(int) * SIZE);
int**result;
result = (int**) malloc(sizeof(int*) * SIZE );
for(int i = 0 ; i < SIZE ; i++ )
result[i] = (int*) malloc(sizeof(int) * SIZE);
//***************************
//fill matrix:
for_each(matrix[0],matrix[0] + SIZE,fill); for_each(result[0],result[0] + SIZE,zero);
for_each(matrix[1],matrix[1] + SIZE,fill); for_each(result[1],result[1] + SIZE,zero);
for_each(matrix[2],matrix[2] + SIZE,fill); for_each(result[2],result[2] + SIZE,zero);
//print it
for(int i = 0 ; i < SIZE ; i++){
for(int j = 0 ; j < SIZE ; j++)
cout << matrix[i][j] << ", ";
cout << endl;
}
cout << endl << endl << endl;
mult(&matrix, &matrix, &result, SIZE);
//print it
for(int i = 0 ; i < SIZE ; i++){
for(int j = 0 ; j < SIZE ; j++)
cout << result[i][j] << ", ";
cout << endl;
}
cout << endl << endl << endl;
system("pause");
return 0;
}
| |