# Question i dont understand

• 06-03-2007
muran_pling
Question i dont understand
Hi guys, i recieved a question for homework. I dont really understand what its asking me to do.

Question:
Code:

```bool balanced(vector <char> p) // Precondition: p[0]...p[n-1] contains n characters, each of which // is ’(’, ’)’, ’{’ or ’}’. // Postcondition: The function returns true if the characters form a // sequence of correctly balanced parentheses with each ’(’ matching // a ’)’ and each ’{’ matching a ’}’. Note that a sequence such as // ( { ) } is NOT balanced because when we draw lines to match the // parentheses to their partners, the lines cross each other. On the // other hand, ( { } ) amd { ( ) } are both balanced.```
if someone can explain what its trying to ask for, that would be much appreciated.
PS. im not asking someone to do it for me, just explain what its trying to say :) cheers
• 06-03-2007
anon
What part have you problems understanding with?

The function takes a vector of characters and outputs whether the sequence of characters has a certain property (matched pairs of braces - as in C++ where all opening braces must have a closing brace).

It's not enough, though, just to count each character as you are also interested in their order. A stack-like data structure can be useful.
• 06-03-2007
muran_pling
Quote:

Originally Posted by anon
What part have you problems understanding with?

The function takes a vector of characters and outputs whether the sequence of characters has a certain property (matched pairs of braces - as in C++ where all opening braces must have a closing brace).

It's not enough, though, just to count each character as you are also interested in their order. A stack-like data structure can be useful.

did u mean something like this:
Code:

```  p.push_back('(');   p.push_back(')');   //THESE ARE ARE IN SEQUENCE     p.push_back('{');   p.push_back('}');   //THESE ARE ARE IN SEQUENCE   p.push_back('{');   p.push_back('{'); //ARE NOT IN SEQUENCE, THEREFORE DO NOT BALANCE ?```
• 06-03-2007
anon
No, the vector may contain one or more opening braces in a row. The question is - are they closed in a proper order.

Here are some examples:
Code:

```balanced: ({}{}) ({()}()) unbalanced ({} - unclosed brace ({)} - expected } instead of ) )( - can't have closing brace before opening brace```
• 06-03-2007
muran_pling
Quote:

Originally Posted by anon
No, the vector may contain one or more opening braces in a row. The question is - are they closed in a proper order.

Here are some examples:
Code:

```balanced: ({}{}) ({()}()) unbalanced ({} - unclosed brace ({)} - expected } instead of ) )( - can't have closing brace before opening brace```

ahh yes.. i understand now... thanks alot anon :)