Jaskaranbir Singh
Jaskaranbir Singh

Reputation: 2034

Printing two arrays side by side with specific number of characters at a time

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).

Upvotes: 3

Views: 7003

Answers (4)

Nerdy
Nerdy

Reputation: 1099

In the second loop you are not reinitialising i so the condition i<width2 gets failed, so initialise i=0 in the for loop Change to:

if (((ii + 1)*(width - 1)) % (width * 2) == 0) {   
    printf("     +     ");
    for (i=0; i < width2; i++) {//set i=0 here
        if (i != 0) {
            if (i % width2 == 0) {
                printf(" |\n| ");
            }
        }
        printf("%d ", row2[i]);   
    }
}

Upvotes: 1

Jongware
Jongware

Reputation: 22478

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.

Upvotes: 3

Zelldon
Zelldon

Reputation: 5516

Create a seperate function to print the row for more readability see my example:

#include <stdio.h>


void printRow(int len, int row[]) {
    int i =0;
    for (i = 0; i < len; i++) {
        printf("%d\t", row[i]);
    }
}

int main(void) {
    int m1[3][3] = {{1,2,3}, {4,5,6}, {7, 8, 9}};
    int m2[3][3] = {{7, 8, 9}, {4,5,6}, {1,2,3}};
    int i = 0;
    int width = 3; //3x3

    for (i = 0; i < width; i++) {
        printf("|\t");
        printRow(width, m1[i]);
        printf("\t+\t");
        printRow(width, m2[i]);
        printf("\t|\n");
    }
    return 0;
}

Output:

|   1   2   3       +   7   8   9       |
|   4   5   6       +   4   5   6       |
|   7   8   9       +   1   2   3       |

Working Example: http://ideone.com/JfnnAS

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409364

Keep it simple, have one outer loop for the rows and two inner loops for the columns. It doesn't really matter if the sizes of the matrices are different, just add a simple check for that before in each inner loop.

Something like the following pseudo code:

size_t max_line_count = MAX(matrix1_line_count, matrix2_line_count);
for (size_t line = 0; line < max_line_count; ++line)
{
    size_t column;

    printf(" | ");

    // First matrix
    for (column = 0; column < matrix1_column_count; ++column)
    {
        if (line < matrix1_line_count)
            printf("%2d", matrix1[line][column]);
        else
            printf("  ");
    }

    printf("    +    ");

    // The second matrix
    for (column = 0; column < matrix2_column_count; ++column)
    {
        if (line < matrix2_line_count)
            printf("%2d", matrix2[line][column]);
        else
            printf("  ");
    }

    printf(" |\n");
}

The inner loops could be broken out into a separate function, to avoid code duplication.

And if the matrices are not arrays of arrays, but a big single array then use e.g matrix1[line * matrix1_column_count + column].

Upvotes: 4

Related Questions