Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

c++ exam question im stuck on...

Options
  • 20-07-2009 11:48am
    #1
    Registered Users Posts: 329 ✭✭


    QUESTION 4
    Consider the function:
    int apply(string& line,const string& str) {
    int c = 0;
    int pos = line.find(str);
    while (pos > 0) {
    c++;
    pos = line.find(str,pos);
    }
    return c;
    }
    What will the following code output?
    string s = "* ** *** ****";
    cout << apply(s,"**") << endl;


    Anyone have any idea how to answer this, I failed exam miserably and need help...

    Cheers

    Nappy


Comments

  • Registered Users Posts: 3,287 ✭✭✭padraig_f


    the reference for the string.find() function might help: http://www.cplusplus.com/reference/string/string/find/

    the first thing you need to answer is, what's the value of 'pos' after the first call to line.find() ?
    int pos = line.find(str);
    

    (incidentally it looks to me like there's a bug in that code, but it shouldn't prevent you from interpreting the meaning of it)


  • Registered Users Posts: 981 ✭✭✭fasty


    What do you think it does? You'll just fail again if we tell you the output. This is simple stuff as long as you have half a brain and just THINK about it!


  • Registered Users Posts: 2,379 ✭✭✭toiletduck


    Nappy wrote: »
    Anyone have any idea how to answer this, I failed exam miserably and need help...

    We could just give you the answer but it'd be much better for you to try and work your way through it and take a stab at it yourself.

    I recommend putting together a simple program which makes use of that function. Step through it line by line in a debugger and see what's happening.


  • Registered Users Posts: 329 ✭✭Nappy


    Right ok,

    I appreciate help and criticism. Am I right in assuming that the function is finding the first occurence of "**" in
    "* ** *** ****";
    so its position would be 2 from a choice of 0-12. Is that what would be outputted? Am I on the right track?



  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    It's doing more than that but you are on the right tracks however I do think there is a slight bug in that code but nothing to prevent you from solving it really and understanding what it is doing.


  • Advertisement
  • Registered Users Posts: 329 ✭✭Nappy


    Could you elaborate? What is the bug you speeak of and would I have to list the positions of other occurences of "**" like at 5, 6, 9, 10 and 11? And would the output just be the digits??


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Nappy wrote: »
    Could you elaborate? What is the bug you speeak of and would I have to list the positions of other occurences of "**" like at 5, 6, 9, 10 and 11? And would the output just be the digits??
    Well you should see from looking at it that the main thing in that that's coming back is the C. What is that doing based on the returned value of the position? That loop also has significance. You have to trace this program out on paper and learn what it is doing. There is no point us telling you the answer. To be come a good programmer you will have to come up with this code, not mind read and understand it so you are better off trying this yourself.

    From what I can see
    pos = line.find(str,pos);
    
    should be
    pos = line.find(str,pos+1) ;
    


  • Registered Users Posts: 329 ✭✭Nappy



    while (pos > 0)

    On this line hoe is pos ever going to be less than 0, cant it only be between 0-12, wouldnt the loop go on infinitely if the condition is never broken.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Nappy wrote: »
    On this line hoe is pos ever going to be less than 0, cant it only be between 0-12, wouldnt the loop go on infinitely if the condition is never broken.
    Well if you cared to take a look at the link padraig_f posted above you'd realise that the find command returns a npos ( static const size_t npos = -1; ) when no occurence of the string is found so yes indeed it can be less than 0. Why else would we be checking this condition.

    If it never went below 0, then we would end up in an infinite loop unless we broke out of it some how. Funnily enough that code does go into an infinite loop I think but thats because your lecturer forgot the + 1 to move onto next occurence. There you have it, a little hint dropped in.


  • Registered Users Posts: 558 ✭✭✭wobbles-grogan


    Im guessin your programming exam is over at this stage?

    Anyway, the above comments put you in the right direction, and you were already on it when you started.

    In the spoiler is the explanation of this.
    I put it in the spoiler cos you really should figure this out is you want to move forward.

    Spoiler:
        while(pos>0){
            c++;
            pos = line.find(str,pos);
        }
    
    this loop simply 'counts' the number of occurences of the 'str' in the 'line'
    the count is stored in the variable 'c'.

    every time the string "**" is found in line, c is incremented and
    the 'pos' variable is set to the position of the occurence of "**" in the line. So when its on the last occurence of "**", the next time it loops through 'pos' will be set to "0", terminating the loop.

    The funtion then returns 'c'.

    Hope this clears things up for ya


  • Advertisement
Advertisement