I found a neat little block of code which may help solve a problem for me but I'm not sure how to get it working for my needs. Basically, I want to take an int[] and fill it with three values which are 301, 305, & 347. What this has helped me to do is to build a list of all the various combinations using these three integers, with repetitions. So far I've gotten it to dump a copy of what it produces to a text file. I had to do this because this easily fills up the buffer with Console.WriteLine. I'll be the first to admit that I really don't know this code that well, except that I can make it work so far. But please don't think for a moment that I really know what I'm doing.
It's presenting me with a few puzzles which I'm not sure about. I have a watch on variables 'c' and 'vf'. The first time through the loop 'c' is equal to 301, okay cool that's what I expected. But when I look at the value for 'vf' I see 51 '3', which I assume means 53, the ascii value for the number 3, and so forth. So I gather that instead of seeing what I thought it would be which is the entire value of 301, I'm getting a breakdown of the first element in the int[] array 'c' which is a '3'.... When you let this run complete and examine the text file it creates you can see what I'm after. And each combination of the array will mean something to a force equation I'm working with. No matter the boring details, I don't seem to grasp how to get this to work with the entire elements of the array one at a time, that is the integers 301, 305 and 347. And based on the value of the elements in 'c' I want to tally up some force values in the variable 'vdForce'. Each line in the text file will show the elements in the combination for that line and the sum of the forces.
This appears to work very well for permutations but I guess like all recursive code this one could hog up large chunks of RAM if the array is large. Fortunately, I only want to figure the combinations on three integers with up to six items in each line. BTW, in the file that this produces the integers on the right side of the equal sign are not correct. I'm still working on that part.
using System;
using System.IO;
using System.Collections.Generic;
namespace Combinations01
{
class Program
{
static void Main(string[] args)
{
int k = 1;
double vdForce = 0, vdTempVar;
using (StreamWriter objWriter
= new StreamWriter
(@"C:\Combinations.txt")) {
for (int count = 1; count <= 6; count++)
{
foreach (var c
in CombinationsWithRepition
(new int[] { 301,
305,
347 }, count
)) {
foreach (var vf in c)
{
vdTempVar = 100;
if (vf == 305) vdTempVar = 200;
if (vf == 347) vdTempVar = 300;
vdForce = vdForce + vdTempVar;
}
objWriter.WriteLine("{0,4} -> {1} = {2}", k, c, vdForce);
k = k + 1;
vdForce = 0;
}
}
}
}
static IEnumerable<String> CombinationsWithRepition(IEnumerable<int> input, int length)
{
if (length <= 0)
yield return "";
else
{
foreach (var i in input)
foreach (var c in CombinationsWithRepition(input, length - 1))
yield return i.ToString() + " " + c;
}
}
}
}
edit:kdub -> code = csharp