So I am asked to write a function called parens that will allow me to do this. Say that I have a string (a + (x) * (6 + 14) - q7), then I will get:
(x)
(6 + 14)
(a + (x) * (6 + 14) - q7)

the function parens takes a parameter which is the address of the starting string which is always a '('. The base case here is when it finds ')' then it prints another function called printFormula that takes two parameter, one is the address of the '(' (\$a0) and the number of chars to be printed (\$a1). Whenever it sees a '(' then it will call the function parens again. My confusion is that what should I do when it's not a '(' or a ')' ?? Can someone help me to develop this function??

2
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by Duoas

Don't do anything. Just go to the next character. Remember, all you have to do is find matching ().

It would help you an awful lot if you get out a piece of graph paper and draw yourself one of these equations, then figure out how to find and handle each piece yourself.

Hope this helps.

Don't do anything. Just go to the next character. Remember, all you have to do is find matching ().

It would help you an awful lot if you get out a piece of graph paper and draw yourself one of these equations, then figure out how to find and handle each piece yourself.

Hope this helps.

and what do I do when I get the address of the next character? do I make a recursive call or do I have to jump to the base case? do I actually need to know the length of the string to do this function? do I need somekind of register to track the character in the string one by one?? or is the \$a0 (which is one of the function parameter which takes in the address of the starting string, always a '(' in this case). used to do that?? I suppose that the \$a0 is changed whenever I found another '(', therefore I can just pass the \$a0 whenever I want to print the formula

It would help you an awful lot if you get out a piece of graph paper and draw yourself one of these equations, then figure out how to find and handle each piece yourself.

Do that and you'll find an answer to a lot of your questions. The way you keep data in memory and the way you access that data is all up to you.

Hope this helps.

I tried to make a sketch in a paper,and tried to do this in java language first.. however I have one problem.. if the currentChar is not a '(' or a ')' then I will have to go to the next index of the string right. Then what do I have to do when I go to the next index of the string?? How do I make that so it goes and check again?? How do I know that the next index of the string is not beyond the strings limit, without having to know the strings length??

There are two pieces of information you need to keep track of:
- where am I in the string?
- how many '('s have I found?

You start out at the first character in the string, with zero '('s found so far.

I suggest you make your function recurse when it finds a '('.

Hope this helps.

[EDIT] Yes, how do you know you are at the end of the string?
In C, the string ends at the first character with a value of zero (not '0', which is a value of 48).

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.