Posted on 2006-05-22
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

    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

    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

    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();
    LVL 6

    Expert Comment

    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

    >>>> 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

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    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.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now