Wednesday, May 1, 2024
HomeC ProgrammingVertical Desk Output | C For Dummies Weblog

Vertical Desk Output | C For Dummies Weblog


Producing a desk the place the gadgets march left-to-right after which down the web page is uncomplicated with stream output. What’s harder is outputting a desk the place the information drops in columns top-down earlier than going left-to-right. Performing this feat requires no display/cursor manipulation, solely intelligent coding.

The important thing to getting the output appropriate is to know two tidbits in regards to the information: the whole variety of gadgets and both the rows or columns desired. You should calculate the grid dimension, then output values within the stream to fill the desk.

When given the variety of values to output and columns desired, I calculate the rows as: rows = values/columns+1 The +1 ensures that the ultimate row is output on condition that these are integer values.

In final week’s Lesson, the calculation made to generate a left-to-right desk is: (x*columns)+y) For the top-to-bottom desk, the calculation is x+(y*rows) — nearly the alternative building. The code from the earlier Lesson have to be modified additional as a result of the clean gadgets seem on the finish of a row, not on the backside of a column.

2022_09_17-Lesson.c


#embody <stdio.h>

int foremost()
{
    const int columns = 9;
    const int values = 100;
    int x,y,rows,depend,v;

    x = 0;                
    depend = values;
    rows = values/columns+1;    
    whereas( x<rows )
    {
        
        for( y=0; y<columns; y++ )
        {
            
            v = x+(y*rows);            
            if( v<values )            
            {
                printf("%3d",v);
                count--;                
                if( !depend )            
                    break;
            }
            if( y<columns-1)            
                putchar('t');
        }
        x++;
        putchar('n');
    }


    return(0);
}

Constants for the columns and variety of values are set as proven on Strains 5 and 6. Play with these numbers to generate completely different dimension desk preparations.

As with final week’s instance, rows are output first as stream output works left-to-right no matter how the desk’s contents lay out. The whereas loop handles rows with the for loop going throughout with column values.

The worth output is inside the vary of zero by fixed values. It’s calculated at Line 18 as: v = x+(y*rows). The present row is added to the column worth multiplied by the whole variety of rows. This expression works out how the column values bounce throughout the desk, as proven within the output in Determine 1.

Determine 1. The desk’s values march down rows first, then throughout columns.

The if check at Line 20 ensures that the ultimate column doesn’t overflow. If the worth calculated v is lower than the whole values, the worth is output and the whole depend is decremented. You’ll be able to see when this situation is trigged in Determine 1, no worth to the proper of 88 seems within the fifth row, and on down the ultimate column.

Keep in mind that worth might be an index right into a buffer or the component quantity in an array. That is how information collected might be output in a top-down column desk with out offending stream output’s left-to-right means of doing issues.

I discover such a desk association to be extra spectacular than final week’s instance and the code offered for this month’s Train. Face it, left-to-right tables aren’t tough to generate with stream output. This top-to-bottom desk raises the bar, nevertheless it’s potential solely when you recognize the whole variety of gadgets and the column or row depend.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments