Comparing array

Posted on 1998-09-08
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 84

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.


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 84

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

840 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