Sunday, July 21, 2024
HomeC ProgrammingA Grid of Random Stars, Half V

A Grid of Random Stars, Half V


The 2 issues I acknowledged within the code from final week’s Lesson had been that scan_column() and find_right() don’t should be separate capabilities. Additionally, the code fails to seek out all of the rectangles within the grid, which is unhealthy. Time to repair the code!

Pulling the code from the 2 capabilities into the predominant() perform was simple. Every perform is principally a for assertion with an if take a look at inside — two traces. No worth must be returned because the if take a look at triggers whether or not the subsequent for loop is executed.

Additional, by transferring the capabilities into the principle nested loop, the issue with not discovering the subsequent rectangle in the identical row is straight away solved! Keep in mind, my authentic assumption was that the scan_column() and find_right() capabilities could be referred to as recursively. They don’t should be so long as the loops proceed to work inside the principle program loop.

Right here is the up to date, consolidated code, which is slightly extensive on this web page:

2024_06_15-Lesson.c


#embrace <stdio.h>
#embrace <stdlib.h>
#embrace <time.h>

#outline ROWS 16
#outline COLS ROWS*2
#outline SIZE COLS*ROWS
#outline PROB 5

struct rect {
    int high;
    int backside;
    int left;
    int proper;
};

void output_grid(char *g,struct rect r)
{
    int row,col;

    for( row=0; row<ROWS; row++ )
    {
        for( col=0; col<COLS; col++ )
        
                    (col==r.proper && (row>r.high && row<r.backside) )
                   )
                putchar('
        putchar('n');
    }
}

int predominant()
{
    char *grid;
    int row,col,depend;
    struct rect r;

    
    srand( (unsigned)time(NULL) );

    
    grid = malloc( sizeof(char) * SIZE );
    if( grid==NULL )
    {
        fprintf(stderr,"Unable to allocate memoryn");
        exit(1);
    }

    
    for( row=0; row<ROWS; row++ )
    {
        for( col=0; col<COLS; col++ )
        {
            if( rand() % PROB )
                *(grid+row*COLS+col) = '.';
            else
                *(grid+row*COLS+col) = '*';
        }
    }

    depend = 0;
    
    for( r.high=0; r.high<ROWS-1; r.high++ )
    {
        for( r.left=0; r.left<COLS; r.left++ )
        {
            
            if( *(grid+r.high*COLS+r.left) == '*' )
            {
                
                for( r.backside=r.high+1; r.backside<ROWS; r.backside++ )
                {
                    if( *(grid+r.backside*COLS+r.left) == '*' )
                    {
                        for( r.proper=r.left+1; r.proper<COLS; r.proper++ )
                        {
                            if( *(grid+r.high*COLS+r.proper)=='*' && *(grid+r.backside*COLS+r.proper)=='*' )
                            {
                                depend++;
                                printf("Rectangle %d:n",depend);
                                output_grid(grid,r);
                            }
                        }
                    }
                }
            }
        }
    }
    printf("Discovered %d rectanglesn",depend);

    return 0;
}

No modifications had been wanted for the output_grid() perform. My purpose right here is simply to include the scan_column() and find_right() and make sure that all of the rectangles are discovered. In any other case, the code runs the identical because it did earlier than, outputting subsequent rectangles as they’re discovered within the grid. Right here is partial output:

Rectangle 1:
.*-------------**......*...*...*
.|*....*.......|..*..*..........
.|.*...........|...*.*....**....
.*-------------*...*.*...**.....
.....................*..*.......
....*..*........*..........*....
...........**..................*
.*.......*..**..****.*.....*....
.....*..***...........*.*....*..
..*.*....**....*....*....*..*..*
....*...*.....*......*.**.......
..*.....**.......*.**.*......*.*
*.....*.***....*........*....*..
...*.........*..................
*..*.*.*...*.*.*...........**.*.
...*...**.*........*.....**.....
Rectangle 2:
.*----------*.***......*...*...*
.|*....*....|.....*..*..........
.|.*........|..*...*.*....**....
.|.........*|..*...*.*...**.....
.|..........|........*..*.......
.|..*..*....|...*..........*....
.|.........*|..................*
.*----------**..****.*.....*....
.....*..***...........*.*....*..
..*.*....**....*....*....*..*..*
....*...*.....*......*.**.......
..*.....**.......*.**.*......*.*
*.....*.***....*........*....*..
...*.........*..................
*..*.*.*...*.*.*...........**.*.
...*...**.*........*.....**.....

Rectangle 82:
.*……….*.***……*…*…*
..*….*……….*..*……….
…*………..*…*.*….**….
.*………*…*…*.*…**…..
…………………*..*…….
….*..*……..*……….*….
………..**………………*
.*…….*..**..****.*…..*….
…..*..***………..*.*….*..
..*.*….**….*….*….*..*..*
….*…*…..*……*.**…….
..*…..**…….*.**.*……*.*
*…..*.***….*……..*….*..
…*———*………………
*..*———*.*………..**.*.
…*…**.*……..*…..**…..
Rectangle 83:
.*……….*.***……*…*…*
..*….*……….*..*……….
…*………..*…*.*….**….
.*………*…*…*.*…**…..
…………………*..*…….
….*..*……..*……….*….
………..**………………*
.*…….*..**..****.*…..*….
…..*..***………..*.*….*..
..*.*….**….*….*….*..*..*
….*…*…..*……*.**…….
..*…..**…….*.**.*……*.*
*…..*.***….*……..*….*..
…*………*………………
*..*—*…*.*.*………..**.*.
…*—**.*……..*…..**…..
Discovered 83 rectangles

The entire of 83 rectangles appears extra acceptable. I additionally confirmed the quantity by printing the output and checking every location. Sure, the pc does the job far quicker than a human.

My activity is actually full. I’m capable of generate a grid of random asterisks and use the pc to find them, highlighting the output. Being a programmer nerd, nevertheless, I’m not fairly achieved but. I wish to create output just like Determine 1, although in ASCII.

Determine 1. Output like this is able to be most popular, however in ASCII after all.

In subsequent week’s Lesson, I shorten the output to show the unique grid after which one other that reveals all of the triangles, or as lots of them as might be mapped in textual content output.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments