Hi,

I have a problem with L-systems recursion, I tried a lot of ways to figure out how to make my fuction recure but I have no luck at all.

What the program must do is the Koch system or rather the replacement rule, where by everytime the fuction recurs, every instance of "F" is replaced with "F+F-F-F+F" (F being the axiom)

This is coding which I came with so far but still working on other ways to solve the problem. My mind is blocked right now.

Option A:

//This code gives me an error over and over again
void RecursionProblem(char* c)
{
   char axiom = 'F';
   char rule[] = {"F+F-F-F+F"};

   int length = sizeof(rule);
   c = new char[length];

   for(int i = 0; i < length; i++)
   {
        if(c[i] == axiom)
        {
            cout << c[i];
        }
   }

   delete[] c;
}

Opyion 2:

//This was my test on how to map F --> F+F-F-F+F
int main()
{
   char* str[1];

   char rule[] = {"F+F-F-F+F"};

   int length = sizeof(rule);
   str[1] = new char[length];

   str[1][1] = 'F';


    if (str[1][1] == 'F')
    {
        str[1] = rule;
    }
    cout << str[1];


   delete[] *str;

   return 0;
}

Are there any suggestions as to how I can maybe tackle this problem?

Can you use std::string?

If not you can do something like this:

void substituteRule(const char *expr, const char *rule, char *result){
   if(!expr) return; 
   else if(expr[0] == 'F') _expand(result,rule); // rule = rule + result; Substitute rule
   else _expand(rule,expr[0]); //else no substitute just add
   substituteRule(expr + 1, rule, result); //else move to next character
}

In option A you have to initiate your var 'c' like this:

char * c=new char[length];

That's why it gives you an error. It's a pointer.

Thank you,

Yes, but I tried using string types instead of char types and it tels me that I cannot convert std::char into std::string

even when I declared every variable to be a string

like:

string axiom = "F"
string rule = "F+F-F-F+F"
string c; //where c is user input

can you show us your complete code? where's the semicolon at the end of the line?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.