Solved

Comparing array

Posted on 1998-09-08
7
344 Views
Last Modified: 2010-04-15
Hi,

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?

regards,
PY
0
Comment
Question by:peiyoke
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Expert Comment

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

Expert Comment

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

Expert Comment

by:ozo
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:peiyoke
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?
0
 
LVL 84

Expert Comment

by:ozo
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.
0
 

Author Comment

by:peiyoke
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.
0
 

Accepted Solution

by:
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) {
                             min=a[i][j]-b[k];
                             min_i=i;
                             min_j=j;
                             min_k=k;
                        }
               }
        }
    }
    /*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]*/
     a[min_i][min_j]=b[min_k];

NOTE: THE ABOVE CODE ONLY HELP TO EXPLAIN MY IDEA, IT MAY NOT BE A VALID VERSION
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 68
Concatenate two strings Last and First Name 10 60
Want to delete all my personal data 13 151
Secondary drive is failing... D Volume 11 47
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
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 and use switch statements in the C programming language.

680 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