Home > OS >  How to print only elements that are larger than the previous ones in 2D array in C
How to print only elements that are larger than the previous ones in 2D array in C

Time:11-20

John is in a big city and he sees an area of buildings. make a program that will print the number of buildings that are visible by John if he is seeing from the west. The building(s) are only visible if it's not blocked by another buildings that are higher on the same height.

Format Input:

First line, input N which is the size of the area. Afterwards the user will input N x N the heights of the buildings.

Output:

The output will be the number of buildings that are visible.

Sample Input(1):

3

1 2 3

2 1 3

3 1 2

Sample Output(1):

3 | 1 2 3

2 | 2 1 3

1 | 3 1 2

Sample Input(2):

5

8 4 3 2 1

1 1 1 1 1

4 1 3 2 5

2 1 2 5 3

1 1 2 4 2

Sample Output(2):

1 | 8 4 3 2 1

1 | 1 1 1 1 1

2 | 4 1 3 2 5

2 | 2 1 2 5 3

3 | 1 1 2 4 2

This is my current code, it's unfinished

#include <stdio.h>

int main()
{
    int n;
    scanf("%d", &n);
    int building[n][n];
    
    for(int i = 0; i < n; i  )
    {
        for(int j = 0; j < n; j  )
        {
            scanf("%d", &building[i][j]);
        }
    }
    
    int max = 0;
    int count = 0;
    for(int i = 0; i < n; i  )
    {
        for(int j = 0; j < n; j  )
        {
            if(building[i][j] > max)
            {
                count  ;
                max = building[i][j];
            }   
        }
    }

    return 0;
}

What should I do next? or is the whole code wrong?

CodePudding user response:

I suggest you write a function visible() that determines the count of visible building for a given row. Then have caller loop over each row and generate the expected output:

#include <stdio.h>

#define N 3

int visible(int n, int building[n]) {
    int count = 0;
    int max = 0;
    for(int i = 0; i < n; i  ) {
        if(building[i] > max) {
            count  ;
            max = building[i];
        }
    }
    return count;
}

int main(void) {
    int building[N][N] = {
        { 1, 2, 3 },
        { 2, 1, 3 },
        { 3, 1, 2 }
    };
    for(int i = 0; i < N; i  ) {
        printf("%d |", visible(N, building[i]));
        for(int j = 0; j < N; j  ) {
            printf(" %d", building[i][j]);
        }
        printf("\n");
    }
}

and the out matches the expected output of your 1st test case:

3 | 1 2 3
2 | 2 1 3
1 | 3 1 2

If you want to do them all in one go, then you need to start with count and max reset to 0 in the outer loop. I am using a counts array that we initialize to 0 upfront but it's the same idea:

#include <stdio.h>
#include <string.h>

#define N 3

void visible_all(int n, int building[n][n], int counts[n]) {
    memset(counts, 0, n * sizeof(*counts));
    for(int i = 0; i < n; i  ) {
        int max = 0;
        for(int j = 0; j < n; j  ) {
            if(building[i][j] > max){
                counts[i]  ;
                max = building[i][j];
            }   
        }
    }
}

int main(void) {
    int building[N][N] = {
        { 1, 2, 3 },
        { 2, 1, 3 },
        { 3, 1, 2 }
    };
    int counts[N];
    visible_all(N,  building, counts);
    for(int i = 0; i < N; i  ) {
        printf("%d |", counts[i]);
        for(int j = 0; j < N; j  ) {
            printf(" %d", building[i][j]);
        }
        printf("\n");
    }
}

CodePudding user response:

Your presented code has two main problems.

int max = 0;
int count = 0;
for(int i = 0; i < n; i  )
{
    for(int j = 0; j < n; j  )
    {
        if(building[i][j] > max)
        {
            count  ;
            max = building[i][j];
        }   
    }
}

The first one is that the variable max should be set to the value of the first element of a row and count should be set at the same time to 1. Otherwise if the first element of a row is equal to or less than 0 then the variable count will not be increased for the first element of the row. And the user can enter negative values because you have an array with the signed element type int.

The second problem is that you need to reset the variables max and count in each iteration of the outer for loop.

And you need to output the calculated value and the current row of the array (as you showed yourself) in each iteration of the outer for loop.

You could write a separate function that calculates the number of visible buildings (elements) of a row of a two-dimensional array.

Here is a demonstration program.

#include <stdio.h>

size_t count_visible( const int a[], size_t n )
{
    size_t count = 0;

    if (n != 0)
    {
        int max = a[0];
        count = 1;

        for (size_t i = 1; i < n; i  )
        {
            if (max < a[i])
            {
                  count;
                max = a[i];
            }
        }
    }

    return count;
}

int main( void )
{
    size_t n = 0;

    if ( scanf( "%zu", &n ) == 1 && n != 0 )
    {
        int building[n][n];
    
        for ( size_t i = 0; i < n; i   )
        {
            for ( size_t j = 0; j < n; j   )
            {
                scanf( "%d", &building[i][j] );
            }
        }

        for ( size_t i = 0; i < n; i  )
        {
            printf( "%zu |", count_visible( building[i], n ) );
            for ( size_t j = 0; j < n; j   )
            {
                printf( " %d", building[i][j] );
            }
            putchar( '\n' );
        }
    }
}

If to enter numbers shown in your second test case then the result program output will look like

1 | 8 4 3 2 1
1 | 1 1 1 1 1
2 | 4 1 3 2 5
2 | 2 1 2 5 3
3 | 1 1 2 4 2
  • Related