'formatting the output in a C program from two functions on same line

expected
The above is the current output. Below is the expected output

current

How can I modify my program to get the output in the expected format? What could be the easiest
way to do it?

Currently, I am printing the FIFO output in a separate function and Banker output in a different
function. Which is causing the display like above. Each function loops over an array and prints all that information

c


Solution 1:[1]

For each table, define a function which prints only one row. Then you can get the desired output by calling this function in the loop. Here is a simplified example:

#include <stdio.h>

#define LEN(array) ((int) (sizeof (array) / sizeof (array)[0]))

struct Ta {
    const char *key;
    int value;
};

struct Tb {
    const char *key;
    int value;
};

struct Ta tableA[2];
struct Tb tableB[2];

void PrintTa(const struct Ta *x)
{
    printf("%s", x->key);
    printf("  ");
    printf("%d", x->value);
}


void PrintTb(const struct Tb *x)
{
    printf("%s", x->key);
    printf("  ");
    printf("%d", x->value);
}


int main(void)
{
    int i;

    tableA[0].key = "foo";
    tableA[0].value = 0;
    tableA[1].key = "bar";
    tableA[1].value = 1;

    tableB[0].key = "baz";
    tableB[0].value = 2;
    tableB[1].key = "qux";
    tableB[1].value = 3;

    /*vertical layout*/
    for (i = 0; i < LEN(tableA); i++) {
        PrintTa(&tableA[i]);
        putchar('\n');
    }
    for (i = 0; i < LEN(tableB); i++) {
        PrintTb(&tableB[i]);
        putchar('\n');
    }

    putchar('\n');

    /*horizontal layout*/
    for (i = 0; i < LEN(tableA); i++) {
        PrintTa(&tableA[i]);
        printf("  ");
        PrintTb(&tableB[i]);
        putchar('\n');
    }

    return 0;
}

This will print the tables in two different layouts:

foo  0
bar  1
baz  2
qux  3

foo  0  baz  2
bar  1  qux  3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 August Karlstrom