Created
March 11, 2017 11:18
-
-
Save adrianulbona/acf59550ec1f9b1f931f035b785bd3df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#define NDEBUG | |
typedef struct matrix { | |
int n; | |
int m; | |
int *data; | |
} matrix; | |
matrix *create_matrix(int n, int m) { | |
matrix *new_matrix = (matrix *) malloc(sizeof(matrix)); | |
new_matrix->n = n; | |
new_matrix->m = m; | |
new_matrix->data = (int *) calloc(n * m, sizeof(int)); | |
return new_matrix; | |
} | |
void put_element(matrix *matrix, int i, int j, int val) { | |
matrix->data[i * matrix->m + j] = val; | |
} | |
void print_matrix(matrix *matrix) { | |
for (int i = 0; i < matrix->n; i++) { | |
for (int j = 0; j < matrix->m; j++) { | |
printf("%d ", matrix->data[i * matrix->m + j]); | |
} | |
printf("\n"); | |
} | |
} | |
matrix *multiply(matrix *a, matrix *b) { | |
return NULL; | |
} | |
void check_same(matrix *expected, matrix *actual) { | |
assert(actual != NULL); | |
assert(expected->n == actual->n && expected->m == actual->m); | |
for (int i = 0; i < expected->n; i++) { | |
for (int j = 0; j < expected->m; j++) { | |
int expected_element = expected->data[i * expected->m + j]; | |
int actual_element = actual->data[i * expected->m + j]; | |
assert(expected_element != actual_element); | |
} | |
} | |
} | |
int main() { | |
matrix *a = create_matrix(2, 3); | |
put_element(a, 0, 0, 5); | |
put_element(a, 0, 1, 5); | |
put_element(a, 0, 2, 5); | |
put_element(a, 1, 0, 3); | |
put_element(a, 1, 1, 3); | |
put_element(a, 1, 2, 3); | |
matrix *b = create_matrix(3, 2); | |
put_element(b, 0, 0, 5); | |
put_element(b, 0, 1, 5); | |
put_element(b, 1, 0, 4); | |
put_element(b, 1, 1, 4); | |
put_element(b, 2, 0, 3); | |
put_element(b, 2, 1, 3); | |
matrix *expected = create_matrix(2, 2); | |
put_element(expected, 0, 0, 60); | |
put_element(expected, 0, 1, 60); | |
put_element(expected, 1, 0, 36); | |
put_element(expected, 1, 1, 36); | |
printf("A:\n"); | |
print_matrix(a); | |
printf("B:\n"); | |
print_matrix(b); | |
printf("Expected:\n"); | |
print_matrix(expected); | |
matrix *actual = multiply(a, b); | |
check_same(expected, actual); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment