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
|
void strassenMultiplication() {
int first[2][2], second[2][2], third[2][2], i, j;
int p1, p2, p3, p4, p5, p6, p7;
//input 4 elements in first matrix
printf("Enter the 4 elements of the first matrix: ");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
scanf("%d", &first[i][j]);
if (cin.fail()){
printf("you have entered a non-numeric data. Please Try Again.");
cin>>first[i][j];
}
//input 4 elements in second matrix
printf("Enter the 4 elements of the second matrix: ");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
scanf("%d", &second[i][j]);
//displays first matrix
printf("\nThe first matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%d\t", first[i][j]);
printf("\n");
}
//displays second matrix
printf("\nThe second matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%d\t", second[i][j]);
printf("\n");
}
//formulas for strassen multiplication
p1 = first[0][0] * (second[0][1] - second[1][1]);
p2 = (first[0][0] + first[0][1]) * second[1][1];
p3 = (first[1][0] + first[1][1]) * second[0][0];
p4 = first[1][1] * (second[1][0] - second[0][0]);
p5 = (first[0][0] + first[1][1]) * (second[0][0] + second[1][1]);
p6 = (first[0][1] - first[1][1]) * (second[1][0] + second[1][1]);
p7 = (first[0][0] - first[1][0]) * (second[0][0] + second[0][1]);
third[0][0] = p5 + p4 - p2 + p6;
third[0][1] = p1 + p2;
third[1][0] = p3 + p4;
third[1][1] = p1 + p5 - p3 - p7;
//displays result after multiplication
printf("\nThe result after the multiplication is: \n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%d\t", third[i][j]);
printf("\n");
}
printf("\n");
}
|