Quiz 11
Quiz 11
#include <stdio.h>
_____________________________________________ Question 5: [12 marks] [estimated time 15 minutes]
#include <math.h>
Write a C program that:
_____________________________________________
double circumference(double r) { return 2 * M_PI * r; } Declare two 2x2 matrices, input elements of both
_____________________________________________
double area(double r) { return M_PI * r * r; } matrices from user and multiply the matrices using
volume(double r) { return (4.0 / 3.0) * M_PI * pow(r, 3); } pointers
double_____________________________________________
Input: First matrix: 1122
_____________________________________________
int main() { Second matrix: 1 1 2 2
double radius; Output: 3 3
_____________________________________________
int choice; 6 6
double (*functions[])(double) = { circumference, area, volume
_____________________________________________ };
_____________________________________________
#include <stdio.h>
_____________________________________________
printf("Enter radius: "); _____________________________________________
void multiplyMatrices(int (*A)[2], int (*B)[2], int
scanf("%lf", &radius);
_____________________________________________ (*C)[2]) {
_____________________________________________
for (int i = 0; i < 2; i++) {
printf("Choose an option:\n1. Circumference\n2. Area\n3. Volume\n");
_____________________________________________ _____________________________________________
for (int j = 0; j < 2; j++) {
scanf("%d", &choice); C[i][j] = 0;
_____________________________________________ _____________________________________________
for (int k = 0; k < 2; k++) {
if (choice >= 1 && choice <= 3) {
_____________________________________________ C[i][j] += A[i][k] * B[k][j];
_____________________________________________
printf("Result: %.2f\n", functions[choice - 1](radius)); }
_____________________________________________
} else { _____________________________________________
}
printf("Invalid choice!\n"); }
} _____________________________________________ _____________________________________________
}
_____________________________________________ _____________________________________________
return 0; int main() {
} _____________________________________________ _____________________________________________
int A[2][2], B[2][2], C[2][2];
_____________________________________________ _____________________________________________
printf("Enter elements of first matrix:\n");
_____________________________________________ _____________________________________________
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
_____________________________________________ _____________________________________________
scanf("%d", &A[i][j]);
_____________________________________________ _____________________________________________
printf("Enter elements of second matrix:\n");
_____________________________________________ _____________________________________________
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
_____________________________________________ _____________________________________________
scanf("%d", &B[i][j]);
_____________________________________________ _____________________________________________
multiplyMatrices(A, B, C);
_____________________________________________ _____________________________________________
printf("Resultant Matrix:\n");
_____________________________________________ _____________________________________________
for (int i = 0; i < 2; i++) {
_____________________________________________ _____________________________________________
for (int j = 0; j < 2; j++) {
printf("%d ", C[i][j]);
_____________________________________________ _____________________________________________
}
_____________________________________________ _____________________________________________
printf("\n");
}
_____________________________________________ _____________________________________________
return 0;
_____________________________________________ _____________________________________________
}
_____________________________________________ _____________________________________________
_____________________________________________ _____________________________________________
_____________________________________________ _____________________________________________
_____________________________________________ _____________________________________________
_____________________________________________ _____________________________________________
_____________________________________________ _____________________________________________