Sign in to follow this  

For loop help

This topic is 3458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.
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 ^^

Hope I could explain my problem. have a nice day.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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:

foo(0): write bar and a "newline" to the console

foo(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]

Share this post


Link to post
Share on other sites
Recursion works.

all(list,0) : print list, newline

all(list, n) :
all([A | list], n-1)
all([B | list], n-1)
all([C | list], n-1)

print_all(n) : all([], n)


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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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[i] = START)
if (++s[i] != 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));
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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_b1
PROG: namenum
LANG: 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;
}

Share this post


Link to post
Share on other sites

This topic is 3458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this