Link to home
Start Free TrialLog in
Avatar of jess_82
jess_82

asked on

Array

I need help writing a program which will compare numbers in an array. The comparison would be to the number right, left (if possible), diagonal(if possible), up(if possible), down(if possible). For example in the given data below

70       12       78
10       35       45
15      20       29

I would like to be able to compare 70 with 12, 10, 35. If 70 is larger than any one of these values I would like to output 70. Then 12 be compared with 70, 78, 10, 35, 45. If 12 is the largest among all of them then I would like to be able to print 12. So on so forth. I am using Visual C++. Any help will be greatly appreaciated. Thanks.
Avatar of jkr
jkr
Flag of Germany image

Break down your problem into the two main parts, which are

1. Find all neighboring values in your 2D array
2. Find the maximum in these and compare it to your current value

The 2ns part is trivial. The 1st part is the interesting one, you should create a function that takes the matrix dimensions and returns the indices of all neghbors, then you can grab the values for the comparison.

Since that sounds a bit like homework - which part is it that you are having difficulties with?
Avatar of jess_82
jess_82

ASKER

Hello Jkr,
I m using an another method I thought about. I am padding some zeros in the array and then comparing each value with eight neighbors. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your solution is not good enough. I think it is worst performance solution. Let us think again. When 70 is printed (i.e 70 is largest among its neighbors) you should skip its neighbors (12,10,35), you do not have to pay attention to it. With this hueristic, you will increase the performance significantly.

 
>>>> If 12 is the largest among all of them then I would like to be able to print 12.

You know, 12 isn't largest among { 12, 70, 78, 10, 35, 45 }. Would you like to print nothing in that case or the largest *neighbor* ? And how looks the output? Like that?

  70      78      78
  70      78      45
  35      45      45

Regards, Alex