473,732 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1999
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
1867
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 an Access 2000 table like this: QUESTION_NUMBER QUESTION 1 Question 1 text.... 2 Question 2 text....
0
1538
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 that would Populate another Listbox/datagrid. Then it would cycle through the selections made in the populated listbox/datagrid and do some calculations. The calculations made would have to add the first two numbers in the populated...
0
1404
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 that would Populate another Listbox/datagrid. Then it would cycle through the selections made in the populated listbox/datagrid and do some calculations. The calculations made would have to add the first two numbers in the populated...
13
5024
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 several questions / senarios: * I created several stored procedures that take parameters and inserts the data into the appropriate tables. This was done for easy access/use from client side apps (i.e. web-based).
2
4720
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 question. I have a web survey that I am working on and have successfully dynamically taken the info from a database, displayed it on the screen and then taken the users answers and inserted them into a
6
1822
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 Request.Querystring("id") = "" then TidF=Request.Form("TidF") Else
3
1286
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. Basically i need to test to see what value is set (where to retrieve the data from) so ive done it like this: If Request.Querystring("id") = "" then TidF=Request.Form("TidF")
2
2080
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 buttonStart_Click(System::Object^ sender, System::EventArgs^ e){ for (int i=1; i<=10; i++){ String^ strValue; FunctionName(&strValue); int interval=1000; // time...
3
1951
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 1 and question2 is selected this code is working fine. But if None of answer of either question selected, it is not iterating through outer loop. Please help me to point out the error. <html> <head> <style type="text/css"> body...
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.