Thread: Question i dont understand

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    33

    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

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    Quote Originally Posted by anon View Post
    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 ?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    33
    Quote Originally Posted by anon View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. I could not understand this question
    By enggabhinandan in forum C Programming
    Replies: 3
    Last Post: 10-22-2006, 05:17 AM
  3. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  4. fstream question...
    By werdy666 in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2002, 03:35 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM