I am writing a program to get a multidimensional array from the user and than find out which of the elements in the array are larger than their "neighbors". Something just isn't working out though and I could use some help.
/*Library Inclusions*/
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
/*Constants*/
#define size 100
#define smallest -32766
/*Function Prototypes*/
int getRow(void);
int getColumn(void);
void getElements(int ne[size][size],int row,int column,int n1,int n2);
void initValues(int ne[size][size],int row, int column);
void showArray(int ne[size][size],int row,int column);
void showLargest(int ne[size][size],int row,int column,int n1,int n2);
bool checkArray(int ne[size][size],int row,int column,int n1,int n2);
/*Main Program*/
int main()
{
int ne[size][size];
int row,column;
int n1, n2;
n1=1;
n2=2;
row=getRow();
column=getColumn();
initValues(ne,row,column);
getElements(ne,row,column,n1,n2);
showArray(ne,row,column);
if (checkArray(ne, row, column, n1, n2)==(true))
showLargest(ne,row,column, n1, n2);
printf("\n");
}
/* Function: getRow
* Usage: getRow();
* ----------------
* This function gets the effective size of the
* rows in the array from the user.
*/
int getRow(void)
{
int row;
printf("Please define the array.\n");
printf("Number of elements in each row? ");
row=GetInteger();
return row;
}
/* Function: getColumn
* Usage: getColumn();
* -------------------
* This function gets the effective size of the
* columns in the array from the user.
*/
int getColumn(void)
{
int column;
printf("Number of elements in each column? ");
column=GetInteger();
printf("\n");
return column;
}
/* Function: getElements
* Usage: getElements(ne,row,column,n1, n2);
* ----------------------------------
* This function reads elements into an integer
* multidimmensional array by reading each input
* from the user one at a time.
*/
void getElements(int ne[size][size],int row,int column,int n1, int n2)
{
int i,j;
for (i=1;i<=row;i++)
{
printf("Row %d\n",i);
for (j=1;j<=column;j++)
{
printf("Enter element at (%d,%d): ",i-1,j-1);
ne[i][j]=GetInteger();
n1=i;
n2=j;
checkArray(ne,row,column, n1, n2);
}
printf("\n");
}
}
/* Function: initValues
* Usage: initValues(ne,row,column);
* ---------------------------------
* This function initializes the all the elements
* in the array to be smaller than the original
* user-defined array, so they would not interfere
* with the neighbors' check.
*/
void initValues(int ne[size][size],int row,int column)
{
int i,j;
for (i=0;i<size;i++)
{
for (j=0;j<size;j++)
{
ne[i][j]=smallest;
}
}
}
/* Function: showArray
* Usage: showArray(ne, row, column);
*-----------------------------------
* This function displays the array
* previously enterd by the user.
*/
void showArray(int ne[size][size],int row,int column)
{
printf("%d\t%d\t%d\n\n%d\t%d\t%d\n\n%d\t%d\t%d\n\n", ne[1][1], ne[1][2], ne[1][3], ne[2][1], ne[2][2], ne[2][3], ne[3][1], ne[3][2], ne[3][3]);
printf("The elements that are larger than their neighbors are: \n");
}
/* Function: showLargest
* Usage: showLargest(ne, row, column, n1, n2);
*-----------------------------------
* This function displays the
* elements which are larger than their neighbors.
*/
void showLargest(int ne[size][size],int row,int column,int n1, int n2)
{
int i, j;
i=n1;
j=n2;
printf("%d in location (%d,%d)\n", ne[i][j]);
}
/* Function: checkArray
* Usage: checkArray(ne, row, column, n1, n2);
*-----------------------------------
* This function checks whether
* an element is larger than its neighbors.
*/
bool checkArray(int ne[size][size],int row,int column,int n1, int n2)
{
int i, j;
i=n1;
j=n2;
if(ne[i][j]>=(ne[i+1][j+1] && ne[i-1][j-1] && ne[i+1][j] && ne[i][j+1]))
if(ne[i][j]>=(ne[i-1][j] && ne[i][j-1] && ne[i-1][j+1] && ne[i+1][j-1]))
{
return true;
}
}