1

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

2
  • Can you pls be more specific with you question? Commented Jul 18, 2013 at 5:07
  • How can I make the program return the correct values, because at the moment it just prints every number Commented Jul 18, 2013 at 5:17

4 Answers 4

1

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

3 Comments

now that i use your full code, it works! :) There must be something wrong in another part of my code :/
oh never mind i just did not read your function properly... sorry, But thank very much
Your solution is incorrect if neighbours have the same value.
0

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.

Comments

0

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);

3 Comments

so then what can i do to change that?
but if i do that won't the program print all numbers as it returns TRUE?
no, because it will return FALSE first if it finds that there are any neighbours that are equal or larger. It only returns TRUE if it has compared itself with all neighbours and found that they are all smaller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.