Comparing array

Posted on 1998-09-08
Medium Priority
Last Modified: 2010-04-15

I have a 2-d array, a[i][j]  in which I need to compare with another 1-d array, b[k]. For each element in the 2-d array, I compare it with every element in the 1-d array. In the comparison, I find the element in b[k] which gives the minimum value of a[i][j]-b[k] and assign the value of b[k] to a[i][j].

What I did was :

Change the 2-d array to 1-d array
Find the difference between each of the new 1-d array and b[k] and store it in a temporary 2-d array.
Find the minimum value in the temporary 2-d array.
Assign value of b[k] which gives the minimum value in temporary 2-d array to the new 1-d array.
Change the new 1-d array back to 2-d array.

I find this very inefficient. How can I implement it in a more efficient way?

Question by:peiyoke
  • 2
  • 2
  • 2
  • +1

Expert Comment

ID: 1252545
Initial thought: sort array b[k] first.  Then use bsearch() for each element of a[i][j]

Expert Comment

ID: 1252546
I mean binary search the closest b[k] element to each a[i][j]
LVL 85

Expert Comment

ID: 1252547
Depending on the sizes of the arrays, you may also want to sort a[i][j]
But otherwise, I see no purpose in converting the 2-d array to 1-d array.

Do you really mean minimum a[i][j]-b[k] or minimum abs(a[i][j]-b[k])?
If the former, you'd just take the maximum b[k] for all a
Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!


Author Comment

ID: 1252548
There is no way to sort b[k] because it is not a single value. When I said that I find the (a[i][j]-b[k]), it is just a representation of what I am doing.

I convert the 2-d array to 1-d array so that later on in the temporary 2-d array, 1 dimension can be used to keep track of the elements from the a[i][j] and another dimension to keep track of elements from b[k].

Is there another way of doing this?
LVL 85

Expert Comment

ID: 1252549
If you give us just a representation of what you are doing, then all we can give you is a representation of an answer.
Can you explain any more about what you're trying to do?
Not being a single value may not be an obstacle to sorting, but
sorting may not be appropriat of all possible representations of things you may be doing.
At worse, it seems you could just look at each element of a[i][j] in turn,
and evaluate (a[i][j]-b[k]) for all k, keeping track of the minmimum with still no need for any auxilliary array,
Or depending on what (a[i][j]-b[k]) really represets, there may be a way of saving some of the effort of multiple evaluations, but it's hard to guess whether this may be possble without furthur information.

Author Comment

ID: 1252550
The array a[i][j] and b[k] are both structure with a few members. A formula is applied to find the most similar element in b[k] which can represent each element in a[i][j]. After obtaining the most similar element, each value of a[i][j] will be substituted by the value of b[k].

In other words, I am trying to classify the elements in a[i][j] into b[k]. After classifying the elements into their respective groups, I need to code the elements using the index of b[k]. Then, I need to decode it again.

Accepted Solution

onki earned 20 total points
ID: 1252551
Three "for-loops" can help this problem.
First, declare a variable which always stored the minimum value of a[i][j]-b[k].

   int min=999999;   /*initialization; set a very big value so that the first comparison always true*/
   int min_i, min_j, min_k;


   /*in the program, set three for-loops*/
   for (i=0;i<=9;i++)  {   /*assuming that the array size is 10x10*/
       for (j=0;j<=9;j++)  {
              for (k=0;k<=9;k++)  {   /*assuming the 1-d array size be 10*/
                       if(a[i][j]-b[k]<min) {
    /*after each checking, those values of i,j,k that leads to minimum a[i][j]-b[k] will be stored in min_i,min_j and min_k*/
    /* then assign the value of b[k] to a[i][j]*/


Featured Post

We Need Your Input!

WatchGuard is currently running a beta program for our new macOS Host Sensor for our Threat Detection and Response service. We're looking for more macOS users to help provide insight and feedback to help us make the product even better. Please sign up for our beta program today!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

597 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