C Program to Multiply Matrices Using a Function
C Program to Multiply Matrices Using a Function
Several school-table operations need the same row-column product. Move it into a helper without hiding its required layout and output-storage ownership.
A fixed-column array parameter is adjusted to a pointer to a row array. Its declared row layout and the operation’s bounds contract must agree with the supplied storage.
Before this exercise, review array indexes and bounds and nested loops. The worked program below applies those ideas to this specific problem.
Explain
Explain why matrix function contract uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use the identity matrix as the right input while keeping the helper unchanged.
Matrix Function Contract: follow the data
Given data
Several school-table operations need the same row-column product. Move it into a helper without hiding its required layout and output-storage ownership.
Rule
A fixed-column array parameter is adjusted to a pointer to a row array. Its declared row layout and the operation’s bounds contract must agree with the supplied storage.
Observed result
19 22 43 50
Understand the operation
The helper assumes all three matrices have valid two-by-two storage. It initializes each output entry before adding its two products; the caller supplies separate result storage.
The parameter spelling does not copy complete matrices and does not automatically validate row count. A general API must supply dimensions and define whether any inputs may overlap the output.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
static void multiply(int left[2][2], int right[2][2], int result[2][2]) {
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) {
result[row][col] = 0;
for (int k = 0; k < 2; k++) result[row][col] += left[row][k] * right[k][col];
}
}
int main(void) {
int left[2][2] = {{1,2},{3,4}}, right[2][2] = {{5,6},{7,8}}, result[2][2];
multiply(left, right, result);
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) printf("%d%s", result[row][col], col == 1 ? "\n" : " ");
return 0;
}
Expected output
19 22
43 50
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
static void multiply(int left[2][2], int right[2][2], int result[2][2]) {
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) {
result[row][col] = 0;
for (int k = 0; k < 2; k++) result[row][col] += left[row][k] * right[k][col];
}
}
int main(void) {
int left[2][2] = {{1,2},{3,4}}, right[2][2] = {{5,6},{7,8}}, result[2][2];
multiply(left, right, result);
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) printf("%d%s", result[row][col], col == 1 ? "\n" : " ");
return 0;
}
The caller supplies three distinct arrays with the expected row layout.
The helper fills the result entries 19,22,43,50, which the caller prints after the function returns.
Follow the worked calculation
Start with the sample
The caller supplies three distinct arrays with the expected row layout.
Apply the rule
The helper fills the result entries 19,22,43,50, which the caller prints after the function returns.
Check the result
19 22 43 50
19 22 43 50
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
static void multiply(int left[2][2], int right[2][2], int result[2][2]) {
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) {
result[row][col] = 0;
for (int k = 0; k < 2; k++) result[row][col] += left[row][k] * right[k][col];
}
}
int main(void) {
int left[2][2] = {{1,2},{3,4}}, right[2][2] = {{5,6},{7,8}}, result[2][2];
multiply(left, right, result);
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) printf("%d%s", result[row][col], col == 1 ? "\n" : " ");
return 0;
}
Use the identity matrix as the right input while keeping the helper unchanged.
Worked answer
The same function computes the unchanged left matrix. Reusing the helper is valid because the shape and distinct-output contracts still hold.
C17 · complete program
#include <stdio.h>
static void multiply(int left[2][2], int right[2][2], int result[2][2]) {
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) {
result[row][col] = 0;
for (int k = 0; k < 2; k++) result[row][col] += left[row][k] * right[k][col];
}
}
int main(void) {
int left[2][2] = {{1,2},{3,4}}, right[2][2] = {{1,0},{0,1}}, result[2][2];
multiply(left, right, result);
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) printf("%d%s", result[row][col], col == 1 ? "\n" : " ");
return 0;
}
Expected output
1 2
3 4
Run in C compiler →Recognize the important boundaries
This simple helper requires output storage separate from the inputs; writing into an input could destroy values needed by later products.
A pointer to individual int values is not interchangeable with a pointer to a fixed two-int row for every array expression.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
A fixed-column array parameter is adjusted to a pointer to a row array. Its declared row layout and the operation’s bounds contract must agree with the supplied storage.
Continue with adjusted parameters and bounds.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.