09S-Section-Solution

09S-Section-Solution - CS106X Winter 2008 Handout 09S...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 09S Winter 2008 January 16 – 18, 2008 Section Solution 1 Problem 1: Compiler, compiler, what do you want? The line that will not compile in this example is the line that performs the assignment dir = num ; . Why not? Enumerated types are represented as integers at runtime, but at compile time, the C++ compiler provides addition type checking information to make your code safer. In this case, since num is in integer type, the compiler will not let you assign an integer to an enumeration. Could this be fixed? Of course! All we need to do is to add in a typecast so that the compiler’s convinced that you know what you are doing. The following line does the same thing, but will actually compile: dir = directionT(num); Problem 2: Removing all occurrences of a character If we want to remove the occurrences of the letter one at a time, we can write the following function: /** * Function: RemoveAllOccurrences * Usage: s = RemoveAllOccurrences(input, ch); * ------------------------------------ * This function takes a string and returns a corresponding string with all * the occurrences of a given letter removed. It uses a for loop to iterate * through the string, testing each character to see if it matches * the letter to remove, and building the string character by character.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern