output to a file

I have code the will output to a csv file all combinations of a give string.

say the string is abc
i will get:

aaa, aab, aac, aba, abb, abc, aca, acb, acc, .. bbb, ...

i do not want to have any more than 2 of the same character next to each other.

out of the ex. the: aaa, bbb, ccc, would not be writen to the file.

any ideas of how to do that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void combinations_r_recursive(const vector<char> &elems, unsigned long req_len,
	vector<unsigned long> &pos, unsigned long depth,
	unsigned long margin)
{	
	// Have we selected the number of required elements?
	if (depth >= req_len)
	{	
		
		for (unsigned long ii = 0; ii < pos.size(); ++ii)

			myfile << elems[pos[ii]];
			myfile << "," << endl;
			cout << ". ";
				
		return;
		
	}	
	// Try to select new elements to the right of the last selected one.
	for (unsigned long ii = 0; ii < elems.size(); ++ii)
	{		
		pos[depth] = ii;
		combinations_r_recursive(elems, req_len, pos, depth + 1, ii);

	}
	
	return;
}

void combinations_r(const vector<char> &elems, unsigned long req_len)
{	
	assert(req_len > 0 && req_len <= elems.size());
	vector<unsigned long> positions(req_len, 0);
	combinations_r_recursive(elems, req_len, positions, 0, 0);
}
const unsigned long num_elements = 25;
const unsigned long comb_len = 4;


It's called permutation. There are several examples online. I didn't look closely at your functions, do they work?
I'd say before you output to the file you check each element against the others:
1
2
3
4
5
6
7
8
9
10
bool IsThreeInARow(char ARRAY[], int SIZEOFARRAY)
{
 for(int i = 0; i < SIZEOFARRAY; i++)
 {if((i + 2) > SIZEOFARRAY)
    return false;
  else
   if(ARRAY[i] == ARRAY[i+1] && ARRAY[i] == ARRAY[i+2])
     return true;
  }
}

There's probably something wrong with the code I posted but it's meant to be an illustration not a plug-n-chug solution.

EDIT: Forgot to include the arguments :p

REEDIT: WOW! There was a lot wrong with that first post. There's bound to be more.

P.S. @ ultifinitus: I don't think he wants to eliminate duplicates in his set. He only seems to want to make sure there are no repeating letters more then two at a time.
Last edited on
I will look at permutation. and yes right now all the fuctions work with combinations.

computergeek01 i am not sure where in my code i would put your code?


thanks

edit: computergeek01 you are right:

"P.S. @ ultifinitus: I don't think he wants to eliminate duplicates in his set. He only seems to want to make sure there are no repeating letters more then two at a time."
Last edited on
@toadmar: Cool cool, my bad.
My Psuedo-Code up there would plug in right before you output your sets to a file. Again, it's not meant to be plug in and go code, it's psuedo-code so you'll need to change a few things about it.
Is there away to output to an ms access file? I would like to open the file from ms access at the same time that the c++ program is writting to it.

Topic archived. No new replies allowed.