Apparent
Apparent

Reputation: 95

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.

Upvotes: 0

Views: 2065

Answers (3)

Andreas Wenzel
Andreas Wenzel

Reputation: 25385

Since another answer has already provided a full solution, I will now provide my own full solution.

The advantage of my solution is that less if checks are necessary, because I have placed the code which jumps several columns outside the main loop. That way, it is not necessary to constantly check inside the main loop whether we must jump several columns. Also, I make more use of the control flow of the program, so that in the main loop, the only conditions that I must check for are

  1. whether the end of the line has been reached, and
  2. whether the end of the array has been reached.

However, the disadvantage of my solution is that my two nested for loops are harder to understand, because they have a non-standard structure.

#include <stdio.h>

void print_table( int start_year, int values[], int num_values )
{
    int col;
    int written = 0;

    //don't print anything if array is empty
    if ( num_values <= 0 )
        return;

    //calculate quotient and remainder of dividing
    //start_year by 10
    int quotient  = start_year / 10;
    int remainder = start_year % 10;

    //print header
    printf("       0   1   2   3   4   5   6   7   8   9\n");

    //prepare first line for data
    printf( "%4d", quotient );
    for ( col = 0; col < remainder; col++ )
    {
        printf( "    " );
    }

    //process one row per loop iteration
    for ( int row = 1; ; row++ )
    {
        //process one column per loop iteration
        for ( ; col < 10; col++ )
        {
            printf( " %3d", values[written++] );

            //check whether we have reached the end of the array
            if ( written == num_values )
            {
                putchar( '\n' );
                return;
            }
        }

        //prepare next line for data
        printf( "\n%4d", quotient + row );
        col = 0;
    }
}

int main( void )
{
    int arr[] = {
        18, 23, 22, 21, 20,
        18, 24, 23, 22, 20, 19, 18, 24, 22, 21,
        20, 19, 24, 23, 22
    };

    print_table( 2015, arr, sizeof arr / sizeof *arr );
}

This program has the following output:

       0   1   2   3   4   5   6   7   8   9
 201                      18  23  22  21  20
 202  18  24  23  22  20  19  18  24  22  21
 203  20  19  24  23  22

Upvotes: 3

user3386109
user3386109

Reputation: 34839

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]));
}

Upvotes: 1

user17668636
user17668636

Reputation:

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

Upvotes: 0

Related Questions