473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Looping question

Having just started using C again after some years off, i've been
stumped by a problem i think someone more experienced could probably
solve pretty easily. I have these 4 objects (vectors), and i want to
find a combination of these 4 objects into two pairs, where the sum of
a specific attribute of each vector in each pair comes closest to some
base value. So all i want to do is run through the permutations of
these 4 objects and find the combination that results in two pairs
looking as close as possible to some base value.

The only thing i could think of creating a temporary vector,
initiating it by just dumping in one permutation, then looping over my
vector with the 4 objects in it, doing different permutations,
comparing those to the values in the temporary vector, and if this
permutation is better, pushing back the current pair values into the
temporary vector. After all the looping is done, i would just use the
temporary vector as my best fits.

Could anyone think of a possible solution that is simpler? I'm
currently trying to implement this solution but it's not quite
working. Any comments would be greatly appreciated.

Oct 23 '08 #1
1 1961
Robocop said:
Having just started using C again after some years off, i've been
stumped by a problem i think someone more experienced could probably
solve pretty easily. I have these 4 objects (vectors), and i want to
find a combination of these 4 objects into two pairs, where the sum of
a specific attribute of each vector in each pair comes closest to some
base value. So all i want to do is run through the permutations of
these 4 objects and find the combination that results in two pairs
looking as close as possible to some base value.

The only thing i could think of creating a temporary vector,
initiating it by just dumping in one permutation, then looping over my
vector with the 4 objects in it, doing different permutations,
comparing those to the values in the temporary vector, and if this
permutation is better, pushing back the current pair values into the
temporary vector. After all the looping is done, i would just use the
temporary vector as my best fits.

Could anyone think of a possible solution that is simpler? I'm
currently trying to implement this solution but it's not quite
working. Any comments would be greatly appreciated.

If you absolutely must have a best fit, then brute force is the only way -
try everything, see what works best. Sounds like you're already doing
that.

If you can get away with a "good enough for rock n' roll" fit, however,
your options widen considerably. For example, you might want to consider a
straight Monte Carlo, or shotgun hillclimbing, or a genetic algorithm.

But you want simple, right? Well, I think that puts us right back to brute
force again.

If you have C code that isn't working, I suggest you throw out as much of
the code as you can without affecting the brokenness that you're
experiencing (but don't throw out error-checking, or we'll only tell you
to put it back in!). Very often, performing this reduction exercise will
allow you to find the bug yourself. If it doesn't, however, at least it
will leave you with a program suitable for posting to comp.lang.c so that
we can take a look and maybe spot the problem.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 23 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Colin Steadman | last post by:
I've create a simple survey using ASP. Its finished and works, but one aspect of my coding is annoying me and I cant figure out a better way of doing it. Basically the questions are stored in...
0
by: | last post by:
Greets All, Question on data in datagrid /listbox and looping? I’m trying to decide the best way to write this code. 1 would like the user to make several selections from one listbox/combo box...
0
by: | last post by:
Greets All, Question on data in datagrid /listbox and looping? I’m trying to decide the best way to write this code. 1 would like the user to make several selections from one listbox/combo box...
13
by: JayCallas | last post by:
I know this question has been asked. And the usual answer is don't use cursors or any other looping method. Instead, try to find a solution that uses set-based queries. But this brings up...
2
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
6
by: Luke - eat.lemons | last post by:
Hi, Im pretty new to asp so all light on this question would be great. Basically i need to test to see what value is set (where to retrieve the data from) so ive done it like this: If...
3
by: Luke - eat.lemons | last post by:
Sorry for the post in this NG but im short on time to get this working and i haven't seem to of got a response anywhere else. Im pretty new to asp so all light on this question would be great. ...
2
by: Davaa | last post by:
Dear all, I am a student making a MS Form application in C++. I would ask a question about "Timer". Sample code which I am developing is below. private: System::Void...
3
by: chiku1523 | last post by:
Hi, Please find the following code. In function setAnswers, I am looping with each question. I have inner loop, which is looping for each answers of the questions. If any of the answer for question...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.