Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
12 13 14 15 16
10 50 11 60 10
5  10 12 16 13
20 70 18 80 14
13  5 12 20 11


Peaks are:{50,60,70,80}

What I have tried:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
FILE *pToFile = fopen("C:/Users/Win10User/Desktop/ana.txt","r");

int **A;int i,j; 
while (!feof (pToFile))
{
for(i=1,j=1;i<=3,j<=8;i++,j++);
fscanf (pToFile, "%d", &A[i][j]); 
printf ("%d ",A[i][j]);
if (A[i][j]>A[i-1][j]&& A[i][j]>A[i][j-1] && A[i][j]>A[i-1][j+1] && A[i][j]>A[i-1][j-1] && A[i][j]>A[i+1][j] && A[i][j]>A[i][j+1]&& A[i][j]>A[i+1][j-1] && A[i][j]>A[i+1][j+1]) 
{
printf("%d ",A[i][j]);
}
return (EXIT_SUCCESS);
} 
Posted
Updated 14-Apr-17 9:02am
v2
Comments
Richard MacCutchan 14-Apr-17 8:56am    
Your integer pointer A does not point to anything. And that if statement should guarantee problems for you.
Member 13127135 23-Apr-17 9:09am    
Thank you

So ... so far you've read the inputs in and don'e nothing with them?

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
I'll give you a couple of clues though:
1) Think about the problem: what is a peak? Presumably, it's a value which is higher than all the eight values around it:
14 15 16
11 60 10
12 16 13
So start by considering this as a two dimensional array, and allocate A as just that - at the moment, you create a pointer-to-a-pointer-to-a-integer, but you never allocate any actual memory for the values to be stored in.
2) When you consider this as a 2D array, you can write a function which takes the array and two indexes : X and Y. It "looks" at the eight locations around that point (making sure to ignore out-of-bounds indexes) and returns "true" or "false" depending on it being a peak or not. Then all you need is a pair of nested loops and a "if" statement...

Give it a try - it's not really complicated at all!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
You should use the debugger to see what your code is doing.
C++
for(i=1,j=1;i<=3,j<=8;i++,j++)

A loop with double counter does not exist, I guess you wanted to do nested loops.
C Programming Tutorial - 39: Nested Loops - YouTube[^]

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900