efficiency

I'm trying to make that finds and prints out the largest numbers compared to all their neighbors. But i cant think of an efficient function that checks them. Please help I'm trying to get this off my things to do >_>
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 /* This program uses multidimensional arrays to find the largest
* number compared to their neighbors. I call this program multidimensional.c */

#include <stdio.h>    /* librarys */
#include "genlib.h"

#define size 3 /* constants */

void displayMatrix(int intArray [size][size]);
main()
{
/* the intArray values you may change the values is you wish */
int intArray[size][size]=
{
   {22,9,1},
   {12,8,3},
   {4,0,12}
};  
}
void displayMatrix(int intArray [size][size])
{
     printf("hello");
int row, column, x;
int num[size];
x=0;
getchar();
}
Last edited on
oh, and now my program isn't printing out anything...someone please help me X_X
Your main() function does nothing other than declare a 2D array of ints and then exit.
yes because i thought i had to declare int intArray[size][size}
is that wrong?
1) don't use #define to make constant. Use constants instead:

1
2
//#define size 3  // bad
static const int size = 3;  // good 


2) main should return an int. You don't have it returning anything:

1
2
//main()  // bad
int main()  // good 


3) What jsmith is saying is, your displayMatrix function is never being called, therefore that code is never being run. Any code you have written inside of displayMatrix will only execute when you call it:
Last edited on
oh i forgot the printf("hello"); is used to test if the program is printing out anything.
ohhhhh ok i forgot about that. lol. :D
ok thanks guys for helping me with that. But what is a good and efficient way to check if the number is larger then the neighboring numbers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* This program uses multidimensional arrays to find the largest
* number compared to their neighbors. I call this program multidimensional.c */

#include <stdio.h>    /* librarys */
#include "genlib.h"

static const int size = 3; /* constants */

void displayMatrix(int intArray [size][size]);
int main()
{
/* the intArray values you may change the values is you wish */
int intArray[size][size]=
{
   {22,9,1},
   {12,8,3},
   {4,0,12}
};
displayMatrix(intArray);
return 0;  
}
void displayMatrix(int intArray [size][size])
{
printf("hello");
int row, column, x;
int num[size];
x=0;
getchar();
}
Last edited on
1
2
3
4
5
6
int largest = number;
for( __all_neighbors__ )
{
  if( neighbor > largest )
    largest = neighbor;
}
there is only one problem with that. I have to print out all the numbers that are larger then their neighbors and their location in the matrix.
Last edited on
1
2
3
4
5
6
7
for( __all_neighbors__ )
{
  if( neighbor > number )
  {
    print_neighbor_and_its_position_in_the_matrix();
  }
}
Actually, is this C? C allows functions to not have a return type. They default to int, I believe.
yes I am using Dev-c++ 4.9.9.2 and thanks Disch :D
Topic archived. No new replies allowed.