473,385 Members | 1,908 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,385 software developers and data experts.

Combination Testing to a predefined value

What I want to do is write a program to test lexicographical
combinations of various sets of numbers to a predefined number and
write that true values to a text file.

Example:

Number choosen between 1 and 50

Number set = 6

Value test = 150

Save to = 150.txt

So what happens is you are using 50 numbers in sets of 6 to test the
sets to equal to 150 and

save to file 150.txt

01 02 03 04 05 06 = 21 False -> Discard
01 10 20 30 40 49 = 150 True -> Store number set in text file
Any ideas would be appreciated as I am fairly new to C++ and would like
to make the program flexible to test any numbers and number sets.

Thanks

May 10 '06 #1
4 2293
ki***********@gmail.com wrote:
What I want to do is write a program to test lexicographical
combinations of various sets of numbers to a predefined number and
write that true values to a text file.

Example:

Number choosen between 1 and 50

Number set = 6

Value test = 150

Save to = 150.txt

So what happens is you are using 50 numbers in sets of 6 to test the
sets to equal to 150 and

save to file 150.txt

01 02 03 04 05 06 = 21 False -> Discard
01 10 20 30 40 49 = 150 True -> Store number set in text file
Any ideas would be appreciated as I am fairly new to C++ and would like
to make the program flexible to test any numbers and number sets.

Thanks


It is not at all clear to me what you are trying to do. What is the
"number set"? What is the "value test"? Are both of those supplied by
the user? Are the "number sets" supplied by the user? Do you always
just add the numbers together to see if the sum is equal to the "value
test"? Etc.

Please clarify, and perhaps we can help more.

Cheers! --M

May 10 '06 #2
The number sets , value test and number range will always be supplied
by the user.

If the user wants to test a number set of 5 numbers between 1 and 50
that will give the total of 100 ,then they will provide that.

So it will be
Range: 1 to 50
Number Set: 5
Value: 100
20 30 35 5 10 = 100 .

For now it will be only be adding the numbers. The other thing is that
each test will be a unigue set so that there is no confusion with
repeat sets of numbers
Only the number set will be stored in the text file.

Hope this helps

May 11 '06 #3
In message <11*********************@i39g2000cwa.googlegroups. com>,
ki***********@gmail.com writes
The number sets , value test and number range will always be supplied
by the user.

If the user wants to test a number set of 5 numbers between 1 and 50
that will give the total of 100 ,then they will provide that.

So it will be
Range: 1 to 50
Number Set: 5
Value: 100
20 30 35 5 10 = 100 .

For now it will be only be adding the numbers. The other thing is that
each test will be a unigue set so that there is no confusion with
repeat sets of numbers

So you're looking for an algorithm to find unique *partitions* of the
total into sets of numbers each within the given range?

This sounds like a general algorithms problem, rather than anything
specific to C++. How about asking somewhere like comp.programming?

--
Richard Herring
May 16 '06 #4
In article <11********************@e56g2000cwe.googlegroups.c om>,
ki***********@gmail.com wrote:
What I want to do is write a program to test lexicographical
combinations of various sets of numbers to a predefined number and
write that true values to a text file.

Example:

Number choosen between 1 and 50

Number set = 6

Value test = 150

Save to = 150.txt

So what happens is you are using 50 numbers in sets of 6 to test the
sets to equal to 150 and

save to file 150.txt

01 02 03 04 05 06 = 21 False -> Discard
01 10 20 30 40 49 = 150 True -> Store number set in text file
Any ideas would be appreciated as I am fairly new to C++ and would like
to make the program flexible to test any numbers and number sets.


Personally I would start by writing a generic algorithm with the
following signature and semantics:

template<class Function, class BidirectionalIterator, class Size>
Function
for_each_combination(BidirectionalIterator first,
BidirectionalIterator last,
Size k, Function f);

Requires: [first, last) is a valid range.

Effects: For each combination of elements in [first, last) taken k at a
time, permutes those elements into the range [first, first+k) and calls
f(first, first+k). If k < 1, no calls to f are made. If k >
last-first, it is truncated to last-first.

Notes: The effects clause does not imply random access. Iterator
addition used only for concise statements.

Returns f.

This is a valuable general purpose tool.

Then I would write a test functor more geared to your problem which has
an operator taking a range of int's, sums them up (perhaps using
std::accumulate), compares the sum to the proper number (perhaps given
during the functor's constructor), and if equal, process the desired
range in the desired way.

Finally a driver program could create the array, and call
for_each_combination with the appropriately created test functor.

The implementation of for_each_combination is definitely the hard part
in this exercise. But fwiw, there are 183723 combinations of numbers in
the range [1, 50], where six of them add up to 150 (out of a total of
nearly 16 million combinations).

There are cheaper algorithms for this particular computation. But
having the for_each_combination function in your tool box is worth the
time creating it. It is much more general, and if nothing else, would
help you test a more specialized but higher performing algorithm for
this problem.

-Howard
May 16 '06 #5

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

Similar topics

1
by: Jim Hefferon | last post by:
Hello, Does anyone have suggestions for unit testing CGI scripts? For instance, how can I set the FieldStorage to have certain values? Do I have to go with a dummy FieldStorage class? And is...
3
by: Andrew | last post by:
Hi, I am thinking about ways for efficient techniques for isolation testing. Here is the problem as I see it: Complex OO systems are designed with massive number of dependencies between modules...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
3
by: gdogg1587 | last post by:
Greetings. I'm working on a program that will "descramble" words. Think of a word scramble game where there is a list of characters, and several blank spaces to denote the word(s) that you are...
1
by: lwoods | last post by:
Sorry for previous email...hit the wrong key! Original Email: I ran the following code: <?php $a=get_defined_constants(); foreach($a as $key=>$value){ echo "$key = $value<br/>"; }
0
by: im20 | last post by:
I am helping a polish up a client's PPC campaign and when of the problems I have run into is that the real estate backend system he is using, does not allow the ability to link directly to a...
2
by: kernel_panic | last post by:
Consider the following code noting the two uses of sizeof(): unsafe struct A { int value; } unsafe struct B { fixed byte value; // Error: 'A' does not have a
0
by: sukatoa | last post by:
My friend ask me about how to copy the value of the key combination Alt - Tab in other key.... he is also a programmer, yet always opening 4 to 7 programs/tools/ diff browsers for testing.... ...
2
by: =?windows-1252?Q?=22=C1lvaro_G=2E_Vicario=22?= | last post by:
Is there any way to tell PHP predefined variables ($GLOBALS, $argv, $argc, $_GET, $_POST…) from *global* user-defined variables? Neither $GLOBALS nor get_defined_vars() put user data apart. I’m...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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...

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.