check the largest number

i am trying to make a program that finds the largest number compared to its neighbors and its almost done. However, its not printing out correct numbers. please help.

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
30
31
32
33
34
35
36
/* 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 ne [size][size]);
int main()
{
/* the intArray values you may change them if you wish */
int ne[size][size]=
{
   {22,9,1},
   {12,8,3},
   {4,0,12}
};
displayMatrix(ne);
return 0;  
}
void displayMatrix(int ne [size][size])
{
int i, j, m, n;
for (i=m-1;i<=m+1;i++)
{
for (j=n-1;j<=n+1;j++)
{
if (ne[i, j] > ne[m, n])
{
printf("%d in location (%d, %d)\n", ne[i, j], i, j);
}
}
}
getchar();
}
Last edited on
Line 29,31: ne[i, j] should be ne[i][j] - same for m, n
Neither m nor n are initialized before they are used on lines 25 and 27.
nice programme
ok so i fixed lines 29 and 31 however, the program is crashing now whereas when i didnt change lines 31 and 29 it printed numbers but they were way to big. and thanks naveenbunch :D

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
30
31
32
33
34
35
36
37
38
/* This program uses multidimensional arrays to find the largest
* number compared to their neighbors. I call this program ne.c */

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

#define size 5 /* constants */

void displayMatrix(int ne [size][size]);
int main()
{
/* the ne values you may change them if you wish */
int ne[size][size]=
{
   {1,0,1,0,1}, /* if you are going to change the values please change the values on the inside not the outside */
   {0,9,5,2,0}, 
   {1,12,8,3,1},
   {0,4,7,12,0},
   {1,0,1,0,1}
};
displayMatrix(ne);
return 0;  
}
void displayMatrix(int ne [size][size])
{
int i, j, m, n;
for (i=m-1;i<=m+1;i++)
{
for (j=n-1;j<=n+1;j++)
{
if (ne[i][j] > ne[m][n])
{
printf("%d in location (%d, %d)\n", ne[i][j], i, j);
}
}
}
getchar();
}

please help :(
jsmith wrote:
Neither m nor n are initialized before they are used on lines 25 and 27.
so i should give them values???
Yes. ALWAYS assign any variable to a value before using. As as implmemented, m and n are undefined and values can be anything within the int range.

Also, this is C++ so use std::cout rather than C printf.
ok and im using Dev-C++
ok so i change my program a little bit and the location numbers have gone done but the numbers that are larger then their neighbors are still way too high

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
30
31
32
33
34
35
36
37
38
39
40
41
/* This program uses multidimensional arrays to find the largest
* number compared to their neighbors. I call this program ne.c */

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

#define size 5 /* constants */

void displayMatrix(int ne [size][size]);
int main()
{
/* the ne values you may change them if you wish */
int ne[size][size]=
{
   {1,0,1,0,1}, /* if you are going to change the values please change the values on the inside not the outside */
   {0,9,5,2,0}, 
   {1,12,8,3,1},
   {0,4,7,12,0},
   {1,0,1,0,1}
};
displayMatrix(ne);
return 0;  
}
void displayMatrix(int ne [size][size])
{
int i, j, m, n;
m = size;
n = size;
for (i=m-1;i<=m+1;i++)
{
for (j=n-1;j<=n+1;j++)
{
if (ne[i, j] > ne[m, n])
{
printf("%d in location (%d, %d)\n", ne[i, j], i, j);
}
}
}
getchar();
}
Last edited on
Yes, well it's pointless to assign them any old values.

What do you want to the for loop on 29 to do, and what do you want to the for loop
on 31 to do? Then, look at what they actually do.
Write out what is happening:

First loop
i = (5 -1) = 4
j = (5 -1) = 4
ne[4,4] > ne[5, 5] ? TRUE

Second loop:
i = 4
j ++ = 4 + 1 = 5
j <= n + 1 = 5 < = 5 + 1? = true
ne[4, 5] > ne[5, 5] ? False

Third loop:
i = 4
j++ = 5 + 1 = 6
j <= n + 1 = 6 <= 6 ? true
ne[4,6] > ne[5,5] ? out of bounds on array (unknown results).

Work it on paper. Your for control loops are not setup correctly.
Thanks soooooooo much you guys rock!!!!
Topic archived. No new replies allowed.