0

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?

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by teo236
0

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
}

Edited by firstPerson: n/a

0

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.

Edited by teo236: n/a

0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.