Discussion 3C: F 10:00-12:00 in BH 4283 CS 31 – Fall 2008 – UCLA
Office Hours: W 10:30-12:30 in BH 4428 chuong [at] cs [dot] ucla [dot] edu

News | Projects & HW | Discussions | Related Materials

PROJECTS and HOMEWORK

Project 1 Project 2 Project 3 Project 4 Project 5 Project 6 Project 7

(if there is any in-accurate information in here, please let me know right away)

Project 7 – Due 9:00 PM Thursday, December 4

Project 7 warmup, specs, FAQ, basic tests.

Arena class

  • Robot* m_robots[MAXROBOTS]: an array of elements, each element is a pointer, to a dynamically allocated Robot object
  • Arena::~Arena(): delete the dynamically allocated player. Delete the array of dynamically allocated Robots (walk through the array, for each element which is a pointer, release the memory that pointer is pointing to, NOTE: delete [] m_robots; won’t work, why?)
  • When a robot is dead, release the memory allocate by that robot, swap the last robot in the array to the position of the one just died (by setting the pointer point to the last robot), decrease the size of m_nRobots

Project 6 – Due 9:00 PM Tuesday, November 25

Project 6 specs. It’s more like a homework than a project.


Project 5 – Due 9:00 PM Tuesday, November 18

Project 5 specs, FAQ, File I/O.

So, you wonder why I haven’t updated the notes for this project. The answer is that I didn’t start early and didn’t try to solve the project (in my head) incrementally. LESSON LEARNED. Even TAs get screwed if they don’t follow the advice of staring early and developing incrementally. Honestly, I have been so busy lately (read lazy) to write up additional help for you guys.

We have learned the truth. Now, I will try to make up reasons to cover up:

  • This project is meant for you to do the dirty jobs yourself. Think of how you would break a big problem down into small pieces. That’s why not much information is given in the specs. You have to learn “how to figure it out yourself”. I can’t spoon-feed you guys all the time.
  • This is a situation where I can borrow the characteristics of NP problems (Non-deterministic Polynomial time, or Non Poly for short) to explain something that you guys will have no idea what I’m talking about. An NP problem is a problem where verifying a solution is quick/easy, but finding a solution is slow/hard. Same thing here for this particular project 5 (with respect to me). If you come to me to discuss your solution, I can verify it very quickly. But if I have to think of a solution to give you guys, it is hard for me (because I’ve been busy lately).
  • From personal experience: all the money you pay in school is not for any fancy material or high-class professors, it’s all to teach you guys “the ability to figure out how to solve a problem yourself”. This project is your first attempt.

And here are some baby-step project notes that I will pretend to help:

  • Do NOT screw up your report’s score. Do what they ask for in the report. Have at least 15-20 test cases.
  • There is NO output to file in this project. Only input from file.
  • The crib must fully match some part of a line from the cipherfile
  • Do pre-processing on both the crib and the content of the cipherfile: remove non-alpha characters, 1 space between words.
  • We really don’t care how long the given crib is. If its strlen(crib) is 81+ characters, we know for sure it’s not going to match any of the lines from the cipherfile (because those lines are limit to 80 characters max). If it is 80- characters, we can easily store it in a fixed size array of 80+1.
    • // if you want to pass in 2-dimensional array to a function
      // and don't want the function to accidentally change the content of the array
      void whateverFunction(const char cipherContent[][80+1], int sizeOf1stDimention);
      
      // if you want to pass in 2-dimensional array to a function
      // and want to put in some value into the array and pass it back to the caller
      void whateverFunction(char key[][26], int sizeOf1stDimention);
      

Project 4 – Due 9:00 PM Tuesday, November 4

Project 4 specs, FAQ

What to do when n is 0?

  • From the specs:
    • “Notwithstanding each function’s behavior described below, all functions that return an int must return −1 if they are passed any bad arguments (e.g. a negative array size, or a position that would require looking at the contents of an element past the last element we’re interested in).”
  • FAQ #3 – What should my functions do if n equals 0?
    • The spec says: “Unless otherwise noted, passing to the function an array size of 0 is not an error; it merely indicates the function should examine no elements of the array.” So do what makes sense. For example, setting all elements of an empty array to a particular value does nothing; that’s no error. If asked to rotateRight an element from an empty array, that’s as much an error as being asked to rotateRight the element at position 8 in a 5-element array. The empty sequence is a subsequence of every sequence (even an empty one), at position 0.
  • int lookup(const string a[], int n, string target);
    • “… Return −1 if there is no such string …”
    • If n is 0: DO something (which turns out to be nothing). RETURN -1 (because we found no such string, aka nothing, duh!!!).
  • int positionOfMax(const string a[], int n);
    • “… Return −1 if the array has no elements …”
    • If n is 0: RETURN -1 (because n is 0 -> no elements, duh!!!).
  • int setAll(string a[], int n, string value);
    • “… Set all n elements of the array to value and return n …”
    • If n is 0: DO something (which turns out to be nothing). RETURN 0 (because n is 0, duh!!!).
  • int rotateRight(string a[], int n, int pos);
    • If n is 0: RETURN -1 (from the FAQ #3: this is a bad argument, aka error. From the specs: return -1 if there is any bad argument, duh!!!).
  • int flip(string a[], int n);
    • “… Reverse the order of the elements of the array and return n …”
    • If n is 0: DO something (which turns out to be nothing). RETURN 0 (because n is 0, duh!!!).
  • int differ(const string a1[], int n1, const string a2[], int n2);
    • “… If the arrays are equal up to the point where one or both runs out, return the smaller of n1 and n2 …”
    • If n1, n2 are 0: DO something (which turns out to be nothing). RETURN 0 (because the smaller of 0 and 0 is 0, duh!!!).
    • If only n1 or n2 is 0: figure it out yourself. THINK.
  • int subsequence(const string a1[], int n1, const string a2[], int n2);
    • “… then return the position in a1 where that subsequence begins …”
    • If n is 0: RETURN 0 (from the FAQ #3: empty sequence is a subsequence of every sequence (even an empty one), at position 0. So a2 is a subsequence of a1, that subsequence begins at 0, duh!!!).
  • int lookupAny(const string a1[], int n1, const string a2[], int n2);
    • “… Return −1 if no element of a1 is equal to any element of a2 …”
    • If n is 0: DO something (which turns out to be nothing). RETURN -1 (because we found no element, duh!!!).
  • int divide(string a[], int n, string divider);
    • “… Return the position of the first element that, after the rearrangement, is not < divider, or n if there are none …”
    • If n is 0: DO something (which turns out to be nothing). RETURN 0 (because n is 0, duh!!!).

How to test your functions:

  • #include <iostream>
    #include <string>
    #include <cassert>  //for the assert function that we use
    using namespace std;
    
    void test_lookup(); //test function lookup
    void test_setAll(); //test function setAll
    ...
    
    int main()
    {
        test_lookup();
        test_setAll();
        ...
    }
    
    void test_lookup()
    {
        string h[6] = { "peter", "", "claire", "hiro", "", "hiro" };
        assert(lookup(h,6,"hiro") == 3);
        assert(lookup(h,6,"mohinder") == -1);
        ...
        cout << "All tests for lookup succeeded" << endl;
    }
    
    void test_setAll()
    {
        string h[6] = { "peter", "", "claire", "hiro", "", "hiro" };
        assert(setAll(h,3,"mohinder") ==  3  &&
    		h[0] == "mohinder"  &&  h[1] == "mohinder"  &&
    		h[2] == "mohinder"  &&  h[3] == "hiro");
        ...
        cout << "All tests for setAll succeeded" << endl;
    }

Project 3 – Due 9:00 PM Tuesday, October 28

Related documents: Project 3 specs, Project 3 warmup, Project 3 FAQ, 2nd note on strings, and 1st note on strings (you should already know this stuff by now).

If you can’t get your project compiled successfully during the warmup, check:

  • picture.cpp must have #include "graphic.h"
  • Only 1 int main() in picture.cpp
  • Any function that you call inside main() should have its prototype/declaration before main() and its implementation/definition after main()
  • #include "graphic.h"
    ...
    void plotHorizontalLine(int r, int c, int length, char ch); //declaration
    
    int main()
    {
        ...
        void plotHorizontalLine(1, 1, 5, 'x'); //function's call
    }
    
    void plotHorizontalLine(int r, int c, int length, char ch) //definition
    {
    }
    

Things to pay attention to on the specs:

  • 3 files: copy & paste graphic.h and graphic.cpp, new code that you write will go into picture.cpp.
  • Use simple functions that you have implemented in other complex functions. This is the whole point of this project: learn how to WRITE and USE functions. So feel free to use the stuff you have written for the stuff you’re about to write.
  • Use cout for required error output message. Use cerr for your own whatever message.
  • NO functions will output anything (meaning NO cout in those functions at all).
  • Error output message comes from the main only.

If I were you, I would implement the project in this order:

Easy stuff:

  • int main()
    • Minimum coding. Set up the basic structure so that you can test your functions.
  • bool inGrid(int r, int c)
  • void clearPicture()

Medium stuff:

  • void plotHorizontalLine(int r, int c, int length, char ch)
  • void plotVerticalLine(int r, int c, int length, char ch)
  • void plotText(int r, int c, string text)
  • void plotRectangle(int r, int c, int height, int width, char ch)
    • Try to utilize functions you have written in the implementation of plotRectangle.
    • If those functions have already promised to do something the right way, do you have to redo the tedious checking of parameters before calling those functions in your plotRectangle?

Hard stuff:

  • void invertRectangle(int r, int c, int height, int width, char ch)
    • Sequentially walk through parts of the rectangle. A rectangle is composed of multiple line, each line is a sequence of characters. What is the natural order to walk through a rectangle?
  • void flipHorizontal(int r, int c, int height, int width)
    • Same stuff. Walk through the rectangle. However, you might not need to walk through a line end-to-end. When you’re flipping characters of a line, think of where you should stop.
  • void plotCircle(int r, int c, int radius, char ch)
    • Take a pencil and a piece of paper. Mark down a grid. Now draw a circle with different radius, starting from 1, 2, 3, 4, 5, 6, etc. by plotting appropriate cells on the grid. Pay attention on the pattern of where you plot the cells. Note if there is any difference between even and odd radius. They could be the same, however. Figure it out yourself. I’m giving away too much already.

Hard stuff:

  • int main()
    • Implement the main function in full now. Set up a loop so that you can keep prompting for user’s input.
    • Do error checking on that input to make sure it’s a valid command.Error checking is done by executeCommands, the main function will craft an error message based on the return value of executeCommands.
    • The main function uses start-at-1 convention when reporting error messages. Make sure to handle this issue in main when printing out message because the executeCommands uses start-at-0 convention.
  • int executeCommands(string commandString)
    • Develop incrementally here. Make sure executeCommands works with 1 command first, before attempting to handle multiple commands.
    • This function returns an integer indicating the position of the first error (else return -1 if everything looks fine), “considering 0 to be the position of the first character of the command string”.

Project 2 – Due 9:00 PM Thursday, October 16

Things to pay attention to on the specs:

  • There is an official FAQ.
  • Duplicate the required messages exactly.
  • No looping required. Meaning if and switch statements should be sufficient.
  • “write only the error message for the earliest erroneous item.”
  • “The one kind of input error your program does not have to deal with is the case of not finding a number in the input where a number is expected.”
  • “A string with no characters in it is the empty string. A string with at least one character in it is not the empty string, even if the characters in it are things like spaces or tabs.” Meaning if a string has at least a character (doesn’t matter what it is), consider it a valid string input.

Project 1 – Due 9:00 PM Tuesday, October 7

Things to pay attention to on the specs:

  • #5 – use integers. By integers, they do mean integers. hello is not an integer, 1.23 is not an integer.
  • #6 – introduce an error that doesn’t prevent a successfully compilation of the program, but produces incorrect/undesired results when the program runs. What type of error is this? THINK!!!
  • #7 – you need to introduce 2 errors, they must be 2 distinct kinds of mistakes, that prevent a successful build. What type of error is this? And remember, you need 2 different kinds of mistakes.
  • Make sure you have 4 files with the correct names when submitting the projects. Otherwise, it’s a 0 for you.

How to structure your files in project 1

  • TEST RUN: Make sure you have followed this guide to create a project, a file within that project, and to compile and run the project succesfully.
  • Repeat those same steps to create your Project 1 and a file named reallyDoesNotMatter.cpp, with the given content.
  • Navigate to where you saved the file reallyDoesNotMatter.cpp. Copy it to your desktop. Rename the one you just copied to your desktop to original.cpp.
    • You now have reallyDoesNotMatter.cpp in your Project 1 folder, and original.cpp on your desktop (both have the same content).
  • Now, in your Project 1, modify reallyDoesNotMatter.cpp to introduce a logic error as required in #6 of the specs. Compile and run your Project 1. After you verify that your program has satisfied whatever is asked in #6 of the specs, copy reallyDoesNotMatter.cpp to your desktop. Rename the one you just copied to your desktop to logic_error.cpp.
    • You now have reallyDoesNotMatter.cpp in your Project 1 folder, original.cpp on your desktop, and logic_error.cpp on your desktop.
  • Now, in your Project 1, delete everything in reallyDoesNotMatter.cpp and copy a fresh given content into it. Introduce 2 different kinds of mistakes as compile erros, as required in #7 of the specs. Compile and run your Project 1. After you verify that your program has satisfied whatever is asked in #7 of the specs, copy reallyDoesNotMatter.cpp to your desktop. Rename the one you just copied to your desktop to compile_error.cpp.
    • You now have reallyDoesNotMatter.cpp in your Project 1 folder, original.cpp on your desktop, logic_error.cpp on your desktop, and compile_error.cpp on your desktop.
  • Zip the 3 files on your desktop + your report file. Do whatever you want with it.
  • What should the name of the zip file be? Good question. READ THE SPECS.