PART A · PRESCRIBED EXERCISE 08
Matrix addition and subtraction
Understand the algorithm. Run the code.
Explain the result with confidence.
LAB A08 · 01
Understand the task.
Aim: Matrix addition and subtraction.
Element-wise matrix operations. Matrix addition and subtraction combine corresponding elements of two matrices with the same number of rows and columns.
Review the related theory in Unit 3.
LAB A08 · 02
Know what goes in and what comes out.
Rows and columns, each 1–5. Then all A entries row by row, followed by all B entries row by row. Entries are integers from -20 to 20. Both matrices use the same supplied dimensions.
Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.
LAB A08 · 03
Plan the steps before writing code.
- Read and validate the shared dimensions.
- Read A using nested row and column loops.
- Read B in the same way.
- For each position, display A[i][j] + B[i][j].
- For each position, display A[i][j] - B[i][j].
LAB A08 · 04
One complete program. Ready to run.
#include <stdio.h>
#define MAX 5
int main(void)
{
int a[MAX][MAX], b[MAX][MAX], rows, columns, i, j;
if (scanf("%d%d", &rows, &columns) != 2 || rows < 1 || rows > MAX || columns < 1 || columns > MAX) {
puts("Enter rows and columns from 1 to 5.");
return 1;
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
if (scanf("%d", &a[i][j]) != 1 || a[i][j] < -20 || a[i][j] > 20) {
puts("Enter matrix values from -20 to 20."); return 1;
}
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
if (scanf("%d", &b[i][j]) != 1 || b[i][j] < -20 || b[i][j] > 20) {
puts("Enter matrix values from -20 to 20."); return 1;
}
}
}
puts("Sum:");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) printf("%s%d", j ? " " : "", a[i][j] + b[i][j]);
putchar('\n');
}
puts("Difference (A - B):");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) printf("%s%d", j ? " " : "", a[i][j] - b[i][j]);
putchar('\n');
}
return 0;
}
Sum:
6 8
10 12
Difference (A - B):
-4 -4
-4 -4
Compile on your own computer
gcc A08-matrix-add-subtract.c -o programRun the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.
LAB A08 · 05
Connect each statement to its purpose.
- Two indexes identify an element: i is the row and j is the column.
- Corresponding cells are combined; this is different from matrix multiplication.
- Subtraction order is A - B, so reversing the operands changes the sign.
- Only the entered rows and columns are processed; unused capacity is not accessed.
LAB A08 · 06
Follow the sample calculation.
| Step or component | What happens |
|---|---|
| Cell [0][0] | 1 + 5 = 6; 1 - 5 = -4 |
| Cell [0][1] | 2 + 6 = 8; 2 - 6 = -4 |
| Second row | 3 + 7 = 10; 4 + 8 = 12; both differences -4 |
LAB A08 · 07
Check more than one input.
| Try this input or case | Expected behaviour |
|---|---|
| 1 × 1: A = -20, B = 20 | Sum 0; difference -40. |
| Any matrix minus itself | All difference entries are zero. |
| Rows 0 or columns 6 | Rejected before input storage. |
Time and storage
O(rows × columns) time. The two input matrices occupy O(rows × columns) logical storage within fixed 5 × 5 capacities.
Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.
LAB A08 · 08
Use the real C compiler.
Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.
Load the compiler when you’re ready to practise.
Your C workspace
Edit real C code and see real compiler output.
Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.
View / copy the selected source
#include <stdio.h>
#define MAX 5
int main(void)
{
int a[MAX][MAX], b[MAX][MAX], rows, columns, i, j;
if (scanf("%d%d", &rows, &columns) != 2 || rows < 1 || rows > MAX || columns < 1 || columns > MAX) {
puts("Enter rows and columns from 1 to 5.");
return 1;
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
if (scanf("%d", &a[i][j]) != 1 || a[i][j] < -20 || a[i][j] > 20) {
puts("Enter matrix values from -20 to 20."); return 1;
}
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
if (scanf("%d", &b[i][j]) != 1 || b[i][j] < -20 || b[i][j] > 20) {
puts("Enter matrix values from -20 to 20."); return 1;
}
}
}
puts("Sum:");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) printf("%s%d", j ? " " : "", a[i][j] + b[i][j]);
putchar('\n');
}
puts("Difference (A - B):");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) printf("%s%d", j ? " " : "", a[i][j] - b[i][j]);
putchar('\n');
}
return 0;
}
LAB A08 · 09
Explain why the program works.
Must the matrices be square?
No. Addition and subtraction work for rectangular matrices as long as both shapes match.
Why two loops?
The outer loop visits rows and the inner loop visits each column in a row.
Why print a newline after the inner loop?
It ends one displayed matrix row.
LAB A08 · 10
Build the next skill.
Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.