Find the max height and width in a "island" map

Viewed 66

I've been trying to do this exercise for a while but i cant seem to find where the problem may lie.

So the exercise goes like this:

We are given a matrix with 1's and 0's with 1 representing islands and 0 representing water We have to build a code that finds us

  1. The highest(more 1 in y direction)
  2. The widest (more 1 in x direction)
  3. The island with the biggest area

We also have to print the initial coordinates where this island starts(the first 2 are the dimensions of the matrix. the reading is done from a file)

5 6
1 1 0 0 0 0
0 0 1 1 0 0
0 0 1 1 0 1
0 0 0 0 0 1
1 0 1 0 0 1

Here it should print:

Max height 3: coordinates(2,5)

Max width 2: coordinates(0,0) (in case there are many with same width we just print one of them)

Max area 4:coordinates (1,2)

I have a basic idea on how to do it.

I tried to implement it but i cant seem to understand why its not working. For now im focusing on finding the max height and width. Im sorry in advance for the messy code(i'm a beginner so for now im more focused on solving the problem than making it look pretty)

This is what i have done so far

#include <stdio.h
#include <stdlib.h>
#include <ctype.h>

int main()
{
    FILE *file;
    file = fopen("file", "r");
    int n, m;
    int i, j;
    fscanf(file, "%d %d", &n, &m);
    int v[n][m];

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            fscanf(file, "%d", &v[i][j]);
        }
    }

    printf("Original Matrix:\n");

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            printf("%d ", v[i][j]);
        }

        printf("\n");
    }

    int maxheight = 0;
    int height = 0;
    int posx;
    int posy;
    i = 0;
    j = 0;

    while (i < n && j < m)
    {
        if (v[i][j] == 1)
        {
            height++;
            i++;
        }
        else if (v[i][j] != 1)
        {
            if (maxheight <= height)
            {
                maxheight = height;

                if (maxheight == height)
                {
                    posx = i;
                    posy = j;
                }
            }

            height = 0;
            i++;
        }

        if (i == n)
        {
            i = 0;
            j++;
            height = 0;
        }
    }

    printf("The biggest height =%d in position (%d ,%d)", maxheight, posx, posy);

    int width = 0;
    int maxwidth = 0;
    int posx1;
    int posy1;
    i = 0;
    j = 0;

    while (i < n && j < m)
    {
        if (v[i][j] == 1)
        {
            width++;
            j++;
        }
        else if (v[i][j] != 1)
        {
            if (maxwidth <= width)
            {
                maxwidth = width;

                if (maxwidth == width)
                {
                    posx1 = i;
                    posy1 = j;
                }
            }

            width = 0;
            j++;
        }

        if (j == m)
        {
            j = 0;
            i++;
            width = 0;
        }
    }

    printf("\nThe biggest width =%d in position (%d ,%d)", maxwidth, posx1, posy1 - maxwidth);

    return 0;
}
1 Answers

Think about the vertical max tests.
In the entire map was 1, codes reports the maxheight as 0.

Code needs to assess maxheight<=height after every height++;, not in the if(v[i][j]!=1) block.

Likewise for width.

Related