I just started learning C a while ago. Today I got a question where I had to take input for 2 matrices (the number of rows and columns were specified by user) and add them. I did the adding and other parts quite easily. But I am thinking of making it look in better format. So my idea was: Suppose user enters a matrix of 3x3 size. Lets suppose he chose following elements->
Matrix 1->
1 2 3
4 5 6
7 8 9
Matrix 2->
9 8 7
6 5 4
3 2 1
I want them to be displayed as->
| 1 2 3 + 9 8 7 |
| 4 5 6 + 6 5 4 |
| 7 8 9 + 3 2 1 |
(no, not the actual addition, just this format, then in next lines i give the answer to addition). But the problem is, I am unable to display that right hand side half part.
My output comes as following:
| 1 2 3 + 9 8 7 |
| 4 5 6 + |
| 7 8 9 + |
I have tried a lot to make remaining characters display in correct order, but got some or the other similar problem. So far, here is my code (its not most recent, I have tried messing it up a lot further but the latest one messes it up even further with introduction of many variables that I think arent even needed. So I will kind of post my best progress so far).
printf("| ");
i = 0; //A global int loop variable defined somewhere
width2 = width; //Another width variable in case I need it in second array, width is variable for number of elements in a row of array. In above example, width=3
for (ii = 0; ii < width * height; ii++) { //ii is just like i, another global int loop variable. Height is number of characters in a row (in above example 3)
if (ii != 0) {
if (ii % width == 0) {
printf(" |\n| ");
}
}
printf("%d ", row1[ii]); //For printing out first (left) set of numbers. row1 is where my matrix 1 array values are stored.
if (((ii + 1)*(width - 1)) % (width * 2) == 0) { //I think this condition is where things are going wrong.
printf(" + ");
for (i; i < width2; i++) {
if (i != 0) {
if (i % width2 == 0) {
printf(" |\n| ");
}
}
printf("%d ", row2[i]); //row2 has second matrix (right) array values
}
}
sec++; //Just another integer variable to have some control over loop process, didnt succeed much though
}
printf(" |\n\n");
Been trying this since 2 days and this is really giving me a headache. I dont mind if theres a better smaller code and the whole code needs to be replaced (as I am quite new to C).
gcc
warns for a "statement with no effect" on the line that says
for (i; i < width2; i++)
-- and lo, it is right to do so! Changing just the first i
to i=0
displays the second set of numbers. (You were so close!)
But it repeatedly displays the first 3 only, because the i
loop in there only loops once over width2
. It is possible to coax the correct values in there with maths such as
printf("%d ", row2[width2*(ii/width) + i]);
which works because ii
increases in steps of width
for each next row, and you want the row count times the right hand set width.
It may be easier to split the entire routine into three loops: the outer only needs to run on height
, and the inner first prints one row of set1 numbers, then one row of set2 numbers. I'll leave that as an exercise, though.