'How can I print a table if the format below?

I've been racking my brain on how I would be able to print my array in the format below(image)

format of table

I have done the necessary calculations in my code to have an array for the numbers {18,23,22,21...} but I'm trying to figure out a way to format them so they are under the corresponding years. The furthest left column are the first 3 numbers of a year and the top row is the last number. For example if this data was calculated for year starting in 2010, the numbers would start under the 0 column, and so on.

So far I could only come up with hard-coding the spaces so everything lines up, but as the year is a user input I can't figure out how to automatically have the data start under the corresponding year. I appreciate any ideas you can offer.

    printf("        0   1   2   3   4   5   6   7   8   9\n");
    printf("%c%c%c\n", year1[0], year1[1], year1[2]);
    printf("%c%c%c\n", year2[0], year2[1], year2[2]);
    printf("%c%c%c\n", year3[0], year3[1], year3[2]);

where the indexed arrays would print the first 3 numbers of the year similar to the picture.

c


Solution 1:[1]

Highlights of the code below:

  • The year is broken into two parts using division and modulo operators. The variable blanks, which is set to the last digit of the year, is the number of columns that need to be skipped on the first line. The variable row is the upper digits of the year, and is used as the row label.

  • The for loop goes through each element of the array.

    • The array element is printed near the middle of the body of the loop.

    • Before printing the array element:

      • When a new row is being started (col == 0), the row label is printed
      • When the blanks need to be displayed (blanks > 0):
        • print the necessary spaces
        • update the column number so that col reflects the current location on the line
        • set blanks to 0, so that this only happens once
    • After printing the array element:

      • Update the column number
      • When the last column is reached, output a newline and start a new row

Here's the code:

#include <stdio.h>

void showTable(int year, int array[], int length)
{
    // print the column headers
    printf("      0   1   2   3   4   5   6   7   8   9\n");

    // initialize some variables
    int blanks = year % 10;     // number of entries to skip on the first row
    int row = year / 10;        // upper digits of the year, used as the row label
    int col = 0;                // column number 0 to 9

    // loop through the elements of the array
    for (int i = 0; i < length; i++)
    {
        // print the year prefix when the column number is 0
        if (col == 0)
            printf("%3d", row);

        // print blank spaces to reach the starting column on the first line
        // this is only done once
        if (blanks > 0)
        {
            printf("%*s", blanks * 4, "");
            col += blanks;
            blanks = 0;
        }

        // print a number from the array
        printf(" %3d", array[i]);

        // update the column, if we've reached the last column, start a new row
        col++;
        if (col > 9) {
            printf("\n");
            col = 0;
            row++;
        }
    }

    // output a final newline, if needed
    if (col != 0)
        putchar('\n');
}

int main(void)
{
    int year = 2015;
    int values[] = { 18, 23, 22, 21, 20, 18, 24, 23, 22, 20, 
                     19, 18, 24, 22, 21, 20, 19, 24, 23, 22 };
    showTable(year, values, sizeof(values) / sizeof(values[0]));
}

Solution 2:[2]

it is not correct code, but can give educational idea how to solve your task

int i = 2010;
while ()
{
    if (input_year < i);
        printf("   ");
    else 
        printf("%d", calculated_data);
    i++;
}

and please show your code, if you want some more ideas how to improve your code

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 user3386109
Solution 2 Ben Nidia