for(x = 0;x<3;x++)
{
mystring &= charlist[x];
for(y = 0;y<3;y++)
{
mystring &= charlist[y];
giveresult(mystring);
mystring = "";
}
}
// for example thats the solution for the input "2"(as length).But I need to // "automate" it ^^
For loop help
Hey,I couldn't think of a better title for this thread but here is my problem.
I should write nested for loops,but I dont know the count of the loops.
for example you need to list all strings can be created using the letters A B and C. But user will give you the length of the string at runtime.So you can't just write nested for next loops.
Hope I could explain my problem. have a nice day.
Going to give you a little psudo code.
string input_str;
input_str = getString();
strLength = GetLength(input_str);
for(x = 0; x < strLength; x++)
You should be able to figure it out from here.
theTroll
string input_str;
input_str = getString();
strLength = GetLength(input_str);
for(x = 0; x < strLength; x++)
You should be able to figure it out from here.
theTroll
Recursion seems to be an elegant approach. You need a global string, let's call it bar. It is initially the empty string. Then you can define a method foo(int length) like this:
And that's basically it.
You can also do an iterative version. Just interpret a string as a number and do the increasing yourself.
Remember when you add 1 to a number, you just have to change the rightmost digit from 0 to 1, from 1 to 2, from 2 to 3... from 8 to 9. If it was already 9, you set it back to 0 and continue at the left neighbour.
Just do the same with the digits 'A', 'B' and 'C'. Here is some scaffold to get you started:
[Edited by - DevFred on June 23, 2008 6:56:01 PM]
foo(0): write bar and a "newline" to the consolefoo(n > 0): append 'A' to bar foo(n - 1) remove last character from bar append 'B' to bar foo(n - 1) remove last character from bar append 'C' to bar foo(n - 1) remove last character from bar
And that's basically it.
You can also do an iterative version. Just interpret a string as a number and do the increasing yourself.
Remember when you add 1 to a number, you just have to change the rightmost digit from 0 to 1, from 1 to 2, from 2 to 3... from 8 to 9. If it was already 9, you set it back to 0 and continue at the left neighbour.
Just do the same with the digits 'A', 'B' and 'C'. Here is some scaffold to get you started:
#include <string>#include <iostream>bool increase(std::string &s){ // increase the rightmost character // if this yields a 'D', wrap around to 'A' // and continue with next character // (if there aren't any more, return false), // otherwise return true}int main(){ int n = 1; std::cin >> n; std::string a(n, 'A'); do { std::cout << a << std::endl; } while (increase(a)); return 0;}
[Edited by - DevFred on June 23, 2008 6:56:01 PM]
Recursion works.
all(list,0) : print list, newlineall(list, n) : all([A | list], n-1) all(, n-1)<br> all([C | list], n-1)<br><br>print_all(n) : all([], n)</pre><br><br>DevFred: as a general tip, never combine mutable structures and recursion. It makes things overly complicated. Think in terms of immutable structures (such as lists) instead.
Quote:Original post by ToohrVyk
DevFred: as a general tip, never combine mutable structures and recursion.
You're totally right, but I'm still fighting with C++ to have it append something to my string with nice, clean syntax :)
Quote:Original post by DevFred
You can also do an iterative version. Just interpret a string as a number and do the increasing yourself.
Can I win an obfuscated C++ contest for my perverted loop? :)
#include <iostream>#include <string>const char START = 'A';const char BOUND = 'D';bool increase(std::string &s){ for (size_t i = s.size(); i--; s = START) if (++s != BOUND) return true; return false;}int main(){ int n = 1; std::cin >> n; std::string s(n, START); do std::cout << s << std::endl; while (increase(s));}
Quote:Original post by Eralp
for example you need to list all strings can be created using the letters A B and C. But user will give you the length of the string at runtime.
Can you post a compilable example of what you have so far?
Quote:Original post by K-Quote:Original post by Eralp
for example you need to list all strings can be created using the letters A B and C. But user will give you the length of the string at runtime.
Can you post a compilable example of what you have so far?
I was thinking of doing what troll suggested.
for(int x = 0;x<inputlength;x++){for(int d = 0;d<3;d++){mystring &= mycharlist[d];}}
but that won't do it because I want them nested. I think I'm gonna try the "iterative" version DevFred suggested.
Oh I realized you wanted full source code, here it is but it wont help because this is just a part of the whole problem that I should solve.I am not even sure that it will compile lol.
/*ID: eralp_b1PROG: namenumLANG: C++*/#include <iostream>#include <fstream>#include <string>#include <vector>#include <sstream>using namespace std;char arr[8][3] = { { 'A', 'B', 'C' }, { 'D', 'E', 'F' }, { 'G', 'H', 'I' }, { 'J', 'K', 'L' }, { 'M', 'N', 'O' }, { 'P', 'R', 'S' }, { 'T', 'U', 'V' }, { 'W', 'X', 'Y' } };int main() { ofstream fout ("namenum.out"); ifstream fin ("namenum.in"); ifstream fin2 ("dict.txt"); string bufferz; vector<string> dict; vector<string> results; int* iName; while(getline(fin2,bufferz)) { dict.push_back(bufferz); } getline(fin,bufferz); iName = new int[bufferz.length()]; for(int x = 0;x<bufferz.length();x++) iName[x] = bufferz[x]-48; ///////////////////////////////////////// for(int k = 0;k<bufferz.length();k++) { for(int m = 0;m<3;m++) { } } ///////////////////////////////////////// fout << "test" << endl; return 0;}
Quote:Original post by Eralp
I am not even sure that it will compile lol.
There's an easy way to find out: let the compiler try.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement