diff --git a/Exercise 10 - Matrix Multiplication (Week 11)/Ex10.pdf b/Exercise 10 - Matrix Multiplication (Week 11)/Ex10.pdf new file mode 100644 index 0000000..f0779a4 Binary files /dev/null and b/Exercise 10 - Matrix Multiplication (Week 11)/Ex10.pdf differ diff --git a/Exercise 10 - Matrix Multiplication (Week 11)/Ex10.txt b/Exercise 10 - Matrix Multiplication (Week 11)/Ex10.txt new file mode 100644 index 0000000..f4fd5ae --- /dev/null +++ b/Exercise 10 - Matrix Multiplication (Week 11)/Ex10.txt @@ -0,0 +1,21 @@ +20 +25 97 +97 5 +5 79 +79 41 +41 67 +67 63 +63 7 +7 88 +88 96 +96 39 +39 3 +3 4 +4 73 +73 97 +97 54 +54 29 +29 73 +73 93 +93 39 +39 68 diff --git a/Exercise 10 - Matrix Multiplication (Week 11)/Ex10_Prof_Solution.pdf b/Exercise 10 - Matrix Multiplication (Week 11)/Ex10_Prof_Solution.pdf new file mode 100644 index 0000000..a5e3d76 Binary files /dev/null and b/Exercise 10 - Matrix Multiplication (Week 11)/Ex10_Prof_Solution.pdf differ diff --git a/Exercise 10 - Matrix Multiplication (Week 11)/ex10.c b/Exercise 10 - Matrix Multiplication (Week 11)/ex10.c new file mode 100644 index 0000000..1795dd6 --- /dev/null +++ b/Exercise 10 - Matrix Multiplication (Week 11)/ex10.c @@ -0,0 +1,97 @@ +/* License: AGPLv3 or later. https://www.gnu.org/licenses/licenses.html + * +* Exercise 10 - Matrix Multiplication (Week 11) +* Name : Manish +* Student Login : ***** +* +* Compile as: +* $ gcc -Wall -std=c11 -o ex10 ex10.c +*/ + +#include +#include +#include + + +int* array = NULL; +int* memo = NULL; +int matrices = 0; + +int best(int i, int j); +int min(int i, int j); + +int main(void) +{ + printf("Enter file name: "); + // Assuming filename/file path will not be longer than 256 characters + char filename[257]; + scanf("%s", filename); + + FILE* file = fopen(filename, "r"); + if (!file) + { + perror(filename); + exit(EXIT_FAILURE); + } + + if (fscanf(file, " %d ", &matrices) != EOF) + {; + array = malloc(sizeof(int) * (matrices + 1)); + memo = malloc(sizeof(int) * matrices); + if (matrices > 0 && (array == NULL || memo == NULL)) + { + fprintf(stderr, " Failed to allocate memory \n"); + return 1; + } + } + + // Read and push matrix dimensions to array + fscanf(file, " %d %d ", &array[0], &array[1]); // first matrix dimensions + int row ; + int col; + for (int i = 1; i < matrices; i++) + { + fscanf(file, " %d %d ", &row, &col); + // row of current matrix must match column of previous matrix + if (row == array[i]) + array[i] = row; + else + { + fprintf( + stderr, + " Rows , columns mismatch , multiplication not possible \n" + ); + exit(1); + } + array[i + 1] = col; + } + fclose(file); + // Initialize memorization array elements to 0 s + for ( int i =0; i < matrices * matrices ; i ++) + memo[i] = 0; + + // Compute and print minimum multiplications needed + printf ( "%d\n", best(1, matrices)); + + free ( array ) ; + + return 0; +} + +int best(int i, int j) +{ + if (i == j) + return 0; + if (memo[matrices * i + j] != 0) + return memo[matrices * i + j]; + int b = INT_MAX; + for (int k = i; k < j; k++) + b = min(b, (array[i-1] * array[j] * array[k]) + best(i, k) + best(k+1, j)); + memo[matrices * i + j] = b; + return b; +} + +int min(int i, int j) +{ + return i < j ? i : j; +} diff --git a/Exercise 10 - Matrix Multiplication (Week 11)/makefile b/Exercise 10 - Matrix Multiplication (Week 11)/makefile new file mode 100644 index 0000000..12fb4b4 --- /dev/null +++ b/Exercise 10 - Matrix Multiplication (Week 11)/makefile @@ -0,0 +1,2 @@ +ex10: ex10.c + gcc -Wall -std=c11 -o ex10 ex10.c