isn't printing out

My program isn't printing anything. 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include "genlib.h"

#define size 5

void displaymatrix(int ne[size][size);
int main()
{
    int ne[size][size]=
    {
        {1,0,1,0,1},
        {0,9,6,7,0},
        {0,4,8,6,0},
        {0,7,4,5,0},
        {0,1,0,1,0}
    };
    displaymatrix(ne);
    return 0;
}
void displaymatrix(int ne[size][size])
{
     int i, j, m, n;
     m=1;
     n=2;
     for (m=1; m<=6; m++)
     {
         while (n<=6)
         {
               for (i=0;i<=6;i++)
               {
                   while (j<=6)
                   {
                         if (ne[i, j] > ne[m, n]==true)
                         {
                              printf("%d in location (%d, %d)\n", ne[i, j], i, j);
                         } // DO NOT EXTEND AGAIN HERE, THE SCOPE ENDS SO GO *BACK*
                         if (n==5 && m==5)
                         {
                               break;
                         }
                         if (i==5)
                         {
                               i=0;
                               j+=1;
                         }
                         if (n==5)
                         {
                               n=0;
                               m+=1;
                         }
                   }
              }
         }
     }
     getchar();
}
Didn't have to copy my comment; that was a guideline to you specifically.
Anyway, on to the REAL problem (since the other real problem was solved, hopefully).
What output are you looking for? What is the objective?
i have to print out the numbers that are larger then their neighbors and the location of the numbers.
Neighbors in all directions?
yes
Just left, right, top, and bottom (when applicable)? What should happen at [0][0]?

Also, does this have to remain in C?
Last edited on
OK then....
I'll look from a theoretical point of view. Cycle through each element of the array using a nested for loop. Then check the positions [x][y+1], [x][y-1], [x-1][y] and [x+1][y] (I presume not diagonals) for being less than the current position. If they are all less, print out the position. So here's a theoretical analysis, in code:
1
2
3
4
5
6
7
8
9
10
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        if ((arr[i][j] > arr[i-1][j]) && (arr[i][j] > arr[i+1][j]) && (arr[i][j] > arr[i][j+1]) && (arr[i][j] > arr[i][j-1]))
        { // VERY LONG CONDITION
             cout << "The number " << arr[i][j] << " at position " << i << ", " << j << " matched.";
        }
     }
}


I can't make sense of any of your conditions, sorry, so I came up with an alternate solution.
Last edited on
thanks
Just be sure to check your indexes.
ok thanks
im really sorry but my program still isn't printing out anything its just blank. and i really dont know what to do. 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include "genlib.h"

#define size 5

void displaymatrix(int ne[size][size]);
int main()
{
    int ne[size][size]=
    {
        {1,0,1,0,1},
        {0,9,6,7,0},
        {0,4,8,6,0},
        {0,7,4,5,0},
        {0,1,0,1,0}
    };
    displaymatrix(ne);
    return 0;
}
void displaymatrix(int ne[size][size])
{
     int i, j, m, n;
     m=1;
     n=2;
     for (m=1; m<=6; m++)
     {
         while (n<=6)
         {
               for (i=0;i<=6;i++)
               {
                   while (j<=6)
                   {
                         if ((ne[i][j] > ne[i-1][j]) && (ne[i][j] > ne[i+1][j]) && (ne[i][j] > ne[i][j+1]) && (ne[i][j] > ne[i][j-1]))

                         {
                              printf("%d in location (%d, %d)\n", ne[i, j], i, j);
                         } 
                         if (n==5 && m==5)
                         {
                               break;
                         }
                         if (i==5)
                         {
                               i=0;
                               j+=1;
                         }
                         if (n==5)
                         {
                               n=0;
                               m+=1;
                         }
                   }
              }
         }
     }
     getchar();
} 


What's up with the three if statements on lines 38 to 50ish? That break statement is probably ending the loop prematurely and preventing any output.
You don't want to index multidimensional arrays like this : ne[i, j](line 36), this should be ne[i][j]
You don't need 4 nested loops here, 2 is enough. Try using the loop construct tummychow posted, but make sure you are not indexing out of bounds. (e.g. ne[0][5] and ne[-1][3] would most likely crash the program (sooner or later)).
Also read this :
http://www.cplusplus.com/doc/tutorial/arrays/#multidimensional_arrays
Last edited on
thank you guys soooooooooooooooooooooo much i it works. you guys ROCK!!!!!!
Topic archived. No new replies allowed.