Posted on 2006-05-22
Medium Priority
Last Modified: 2010-04-01
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.
Question by:jess_82
LVL 86

Expert Comment

ID: 16736176
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?

Author Comment

ID: 16736627
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.
LVL 86

Accepted Solution

jkr earned 2000 total points
ID: 16736946
Onve you have the neighboring values, just add them to a valarray and call 'max()', e.g. like

#include <valarray>
using namespace std;

valarray<int> va;


for (int i = 0; i < num_neighbors; ++i) va[i] = nerighbor[i];

int max_neighbor = va.max();

Expert Comment

ID: 16738847
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.

LVL 39

Expert Comment

ID: 16740350
>>>> 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


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question