Hi everyone, below are problems for Lab#3 :)

Enjoy,
Saty


Questions

Q1. For HW2, you are asked to create an array2D class. Create a similar class called charMatrix, with the following private members and public methods:

With the above, you can create a grid, and fill it with keyboard characters to create several forms of ASCII art :) See also: this and this.

Here is how you'd use the charMatrix class:

int main()
{
    int xR=25, yR=25, myChar='x';
    charMatrix c(xR,yR,' '); // fill a 25x25 block with blanks
    for(int i=0;i < yR;i++) {
        for(int j=0;j < xR;j++) {
            // do c.setChar(j,i,myChar) based on a calculation involving i,j
        }
    }
    c.prt();
    c.saveToFile("fun.txt"); 

    return 0;
}

How would you create this pattern (hint: think "odd or even"):

And this one:

And this (think "sum"):

For more practice (after this course), you can use your lovely little charMatrix class to try creating more complex patterns (more x,y resolution, more ASCII characters, more complex math) like ones shown here [from chris.com] - you can put every single char on your keyboard to good use :) Not all of these were generated by code, but most of them can be! This is how you can get better and better at coding - pick a pattern you like, and work towards creating it using code - think of pattern generation as puzzle-solving.

Here is a solution (you need to add the saveToFile() method to it).

Q2. Count the number of occurences of 'Alice', in 'Alice in Wonderland'. For a starting point, look at the 'Miscellany' class slides, where we have an example of counting 'the' occurences in this amazing document: http://constitutionus.com/

Note that you are only counting "Alice" instances, not "Alice,", "Alice." etc. That's because we are doing if (token=="Alice") {}. What we need is, if (token.startsWith("Alice"){} - that will catch things like "Alice,", "Alice.", "Alice!", "Alice:" etc. But C++'s string class does not have a .startsWith() method, so you can create and use a standalone startsWith() function (returns a bool), like so:

// eg. if token and substr were "Alice," and "Alice", that will result in 'true' being returned
bool startsWith(string token, string substr)
{
    if (token.find(substr)==0) {
        return true;
    }
    else return false;
}// startsWith()

For this lab exercise, just counting 'Alice' is enough; later, for practice, be sure to add in and call the startsWith() function [shown above] instead, and observe that you get a higher count :)

Here is one way to code the above.