matrix add and subtract in c – dead nifty, innit
intro
Oi, you lot! New to C and fancy summat a bit tasty? We’re gonna sling together a lil program to add and subtract two matrices—ya know, them grid things with numbers where ya mash em together or take one off t’other. It’s dead simple but proper handy—like when I was tryna tally up my pub tab with my mate’s (got it wrong, too smashed). Great for getting your head round 2D arrays in C without a right faff. Let’s have a butcher’s!
code bit
Here’s the code—nowt posh, just a quick job I chucked together. Shoved it in a box so it don’t look like a total shambles on your blog. take a gander:
void addMatrix(int a[][3], int b[][3], int result[][3], int rows, int cols) {
for(int i=0;i
}
}
}
void subMatrix(int a[][3], int b[][3], int result[][3], int rows, int cols) {
for(int i=0;i
}
}
}
void showMatrix(int matrix[][3], int rows, int cols) { // show it off
for(int i=0;i
}
printf("\n");
}
}
int main() { // nearly did void, ha!
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}}; // first grid
int b[3][3]={{9,8,7},{6,5,4},{3,2,1}}; // second grid
int addResult[3][3],subResult[3][3]; // buckets for results
int rows=3,cols=3; // size, innit
addMatrix(a,b,addResult,rows,cols); // mash em
subMatrix(a,b,subResult,rows,cols); // take em apart
printf("added matrix:\n");
showMatrix(addResult,rows,cols);
printf("subtracted matrix:\n");
showMatrix(subResult,rows,cols); // forgot ; once, ugh!
return 0;
}
what ya get
Here’s what it chucks out when ya run it. I hard-coded two 3x3 matrices—worked bang on, mate!
10 10 10
10 10 10
10 10 10
subtracted matrix:
-8 -6 -4
-2 0 2
4 6 8
how it works, kinda
Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of pork scratchings, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, swear down!
1. that `#include
This is me nicking my toolkit. `
2. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!
3. boxes: `int a[3][3]` and pals
These are my lil buckets. `a[3][3]={{1,2,3},{4,5,6},{7,8,9}}` and `b[3][3]={{9,8,7},{6,5,4},{3,2,1}}` are the two matrices—hard-coded 3x3 grids. `addResult` and `subResult` are empty 3x3 pots for the answers. `rows=3,cols=3` set the size.
4. `addMatrix` funtion
`addMatrix(int a[][3], int b[][3], int result[][3], int rows, int cols)` mashes em. Nested `for` loops run through each spot—`result[i][j]=a[i][j]+b[i][j]` adds the numbers at `i,j`. Like piling two pint tabs together!
5. `subMatrix` funtion
`subMatrix(int a[][3], int b[][3], int result[][3], int rows, int cols)` takes one off t’other. Same deal—nested `for` loops, but `result[i][j]=a[i][j]-b[i][j]` subtracts. Like taking yer mate’s tab off yours!
6. `showMatrix` funtion
`showMatrix(int matrix[][3], int rows, int cols)` shows the grid. Nested `for` loops print each `matrix[i][j]` with a space, `printf("\n")` jumps to a new line per row. Like flashing yer tally sheet!
7. chucking it out: `printf` and calls
`addMatrix` and `subMatrix` do the work, then `printf("added matrix:\n")` and `showMatrix(addResult,rows,cols)` show the sum, same for subtract with `subResult`. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!
8. `return 0` to scarper
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.
And that’s yer lot! A cracking lil program to add and subtract matrices. Faff with it—chuck in daft grids, tweak it to say “mashed grids” if ya fancy. First time I ran this, I got all 10s and thought I’d cracked it—dead chuffed, eh! You’re a star already, mate—keep smashing it! 😊