Question

The instructions are to Write a program that uses a two - dimensional array to find the elements that are larger than all their neighbors.

For example if my input is:

1   2   11
13  5   6
7   6   9

The output would be: 11, 13, 9

For some reason though it displays no numbers at all. Could someone help correct the code.

Here is the code:

#include <stdio.h>                                          
#include "genlib.h"
#include "simpio.h"

#define N 3                                                 

bool neighbourCheck(int array[N+2][N+2], int i, int j);     
void getArray(int array[N+2][N+2]);

main()                                                  
{
      int array[N+2][N+2], i, j;

      printf("This program will ask you to enter nine integers of an aray.\n");
      getArray(array);
      for (i=1; i<N+1; i++)
      {
                for(j=1; j<N+1; j++)
                {
                         if(neighbourCheck(array, i, j)==TRUE)
                         {
                                        printf("%d\t", array[i][j]);            
                         }        
                }
      }
      getchar();
}

void getArray(int array[N+2][N+2])
{
      int i, j;

      for(j=0;j<=N+1;j++)
      {
                i=0;
                array[i][j]=-1;
                i=4;
                array[i][j]=-1;                   
      }
      for(i=1;i<N+1;i++)
      {
                j=0;
                array[i][j]=-1;
                j=4;
                array[i][j]=-1;                 
      }
      for(i=1;i<N+1;i++)
      {
                for(j=1;j<4;j++)
                {
                                printf("\nEnter a positive integer: ");
                                array[i][j]=GetInteger();                
                }
      }   
}

bool neighbourCheck(int array[N+2][N+2], int i, int j)                      
{
     int l, m;

     for(l=i-1; l<i+1; l++)
     {
                for(m=j-1; m<j-1; m++)
                {
                         if(array[l][m]>=array[i][j])
                         {
                                        return(FALSE);                            
                         }
                         return(TRUE);
                }
     }
}  

Thank you :D

Was it helpful?

Solution

You have to change the part

for(l=i-1; l<i+1; l++)
{
        for(m=j-1; m<j-1; m++)

into:

for(l=i-1;l<=i+1;l++)
{
        for(m=j-1;m<=j-1;m++)

Cheers!

OTHER TIPS

I dont think you are checking all the neighbors.

bool neighbourCheck(int array[N+2][N+2], int i, int j)                      
{
   int l, m;
   for(l=i-1; l<i+1; l++)
   {
            for(m=j-1; m<j-1; m++)
            {
                     if(array[l][m]>=array[i][j])
                     {
                                    return(FALSE);                            
                     }
                     return(TRUE);
            }
   }
}  

If you want to check (1,1) you have to compare it with {(0,0),(0,1),(0,2),(1,0),(1,2),(2,0),(2,1),(2,2)}. But i dont thik your loop checks everything.

bool neighbourCheck(int array[N+2][N+2], int i, int j)                      
{
    int l, m;
    for(l=i-1; l<=i+1; l++)
    {
        for(m=j-1; m<=j+1; m++)
        {
            // Returns false if any of the neighbour element is greater then & equal to current element.
            if(array[l][m]>=array[i][j]) return(FALSE);                          

        }
    } 
    //You will reach end
    return(TRUE);
}

The above code will compare with itself, diagonal neighbours but still the condition is > it will avoid confusions.

If you just want to avoid comparisions with diagonal neighbours then insted of for loops you can use direct if else statement.

Its working fine... This is the complete code.

#include <stdio.h>   
#include<stdbool.h>                         
#define N 3                                                 
bool neighbourCheck(int array[N+2][N+2], int i, int j);     
void getArray(int array[N+2][N+2]);
int main()                                                  
{
  int array[N+2][N+2], i, j;
  printf("This program will ask you to enter nine integers of an aray.\n");
  getArray(array);
  for (i=1; i<N+1; i++)
  {
      for(j=1; j<N+1; j++)
      {
           if(neighbourCheck(array, i, j))
               printf("%d\t", array[i][j]);             
      }
  }
  getchar();
}

void getArray(int array[N+2][N+2])
{
  int i, j;

  for(j=0;j<=N+1;j++)
  {
            i=0;
            array[i][j]=-1;
            i=4;
            array[i][j]=-1;                   
  }
  for(i=1;i<N+1;i++)
  {
            j=0;
            array[i][j]=-1;
            j=4;
            array[i][j]=-1;                 
  }
  for(i=1;i<N+1;i++)
  {
            for(j=1;j<4;j++)
            {
                            printf("\nEnter a positive integer: ");
                           scanf("%d",&array[i][j]);             
            }
  }   
}

bool neighbourCheck(int array[N+2][N+2], int i, int j)                      
{
  int l, m;
  for(l=i-1; l<=i+1; l++)
  {
    for(m=j-1; m<=j+1; m++)
    {
        // Returns false if any of the neighbour element is greater then and equal to current element.
        if(array[l][m]>=array[i][j]) return false;                          

    }
} 
//You will reach end
return true;
}

for(m=j-1; m < j-1; m++)

in the above statement for all kinds of j values ,condition always false so it can't enter into loop.

In the neighbour check function the following code

for(l=i-1; l<i+1; l++)
{
  for(m=j-1; m<j-1; m++)
  {
    if(array[l][m]>=array[i][j])
    {
      return(FALSE);                            
    }
    return(TRUE);
  }
}

means that the values of l and m will include i and j at that point the test becomes

if(array[i][j]>=array[i][j])

which will always be TRUE, and you will return FALSE

Also, looking further, the return TRUE doesn't appear to be where it should. You should only return TRUE after testing all possible combinations, not just the first one. Though that makes it surprising that you print no numbers.

In response to the comment, one possible approach is the following replacement:

for(l=i-1; l<i+1; l++)
{
  for(m=j-1; m<j+1; m++)
  {
    if (l == i && m == j)
      continue;

    if(array[l][m]>=array[i][j])
    {
      return(FALSE);                            
    }
  }
}
return(TRUE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top