Solved

Comparing array

Posted on 1998-09-08
7
340 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
IT, Stop Being Called Into Every Meeting

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!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

747 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

12 Experts available now in Live!

Get 1:1 Help Now