Spring 2021

PIC 10C: Advanced Programming

Discussion Section 1A

Wonjun Lee



Week3

Tuesday

Filesystem

Opening files from the directories

#include < iostream >
#include < filesystem >
#include < vector >
#include < fstream >

namespace fs = std::filesystem;

int main(){

    // Getting all the files in the directory "submissions"
    std::vector< std::string > filenames;
    fs::directory_iterator iter("./submissions");

    for(const auto& p : iter){
        if(p.is_directory()){
            fs::directory_iterator iter2(p.path().string());
            for(const auto& p2 : iter2){
                if(p2.is_regular_file()) {
                    std::cout << "\tfile: " << p2.path() << " under the directory " << p.path().string() << '\n';
                    filenames.push_back(p2.path());
                }
            }
        }
    }

    // Read one file from filenames vector.
    std::string out = "";
    std::string line = "";

    std::ifstream myfile(filenames[0]);

    if(myfile.is_open()){
        while(getline(myfile, line)){
            out += line;
            out += '\n';
        }
        myfile.close();
    }

    std::cout << filenames[0] << '\n';
    std::cout << out << std::endl;
}

      

Finding the longest subsequence

#include < iostream >

using namespace std;

int main(){

    string a = " ABCYZB";
    string b = " ABZBC";

    int n1 = a.length(); // 6
    int n2 = b.length(); // 5
    int** count = new int*[n2];
    for(int i=0;i < n2;++i){
        count[i] = new int[n1];
    }

    // set the first row and first col to be one

    // i <- row index
    // j <- col index
    for(int i=0;i < n2;++i){
        count[i][0] = 1;
    }

    for(int j=0; j < n1;++j){
        count[0][j] = 1;
    }

    for(int i=1;i < n2;++i){
        for(int j=1; j < n1;++j){
            if( a[j] != b[i] ){
                count[i][j] = fmax(count[i][j-1], count[i-1][j]);
            }else{
                count[i][j] = 1 + count[i-1][j-1];
            }
        }
    }

    // backtracking
    int i = n2-1;
    int j = n1-1;
    string longest_sub = "";

    while(i>=0 && j>=0){
        if(a[j] == b[i]){
            longest_sub = a[j] + longest_sub;
            i = i - 1;
            j = j - 1;
        }else{
            if(count[i][j-1] > count[i-1][j]){
                j = j - 1;
            }else{
                i = i - 1;
            }
        }
    }

    cout << "longest_sub : " << longest_sub << '\n';


    for(int i=0;i < n2;++i){
        for(int j=0; j < n1;++j){
            cout << count[i][j] << " ";
        }
        cout << '\n';
    }

    return 0;
}



      

Thursday

Installing SFML on Mac

Step 1: Install Homebrew

To check if your Mac has a homebrew installed already, type brew on your terminal. If the error shows up saying it cannot recognize the command, then you have to install homebrew

Type the following line on your terminal to install the Homebrew. [Homebrew website]

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Now you have installed the Homebrew

Step 2: Install SFML

Type the following line on your terminal to install the SFML.

brew install sfml

If you get an error from installation then you may need to type the following lines on your terminal.

export HOMEBREW_NO_BOTTLE_SOURCE_FALLBACK=1
brew install sfml

Bouncing the ball using SFML

The following code will show the animation of the ball bouncing on the walls.

#include < SFML/Graphics.hpp >
int main() {
    // Set the window size.
    int windows_height = 800;
    int windows_width  = 800;
    sf::RenderWindow window(sf::VideoMode(windows_width, windows_height), "SFML works!");
    // Set the radius of the ball.
    float radius = 100;
    sf::CircleShape circle(radius);
    // Set the color of the ball.
    circle.setFillColor(sf::Color::Green);
    // Set the initial position of the ball
    circle.setPosition(100, 300);
    
    double velx = 1; // horizontal velocity
    double vely = 1; // vertical velocity
    
    while (window.isOpen()) {
        // close the window when "close" button of the window is clicked
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }
        
        // try to comment this line and see what happens!
        window.clear();
        
        // getting the position of the circle. (top left corner of the ball)
        sf::Vector2f position = circle.getPosition();
        
        float x = position.x; // x position
        float y = position.y; // y position
        
        // when the ball hits the boundary
        // invert the velocity x
        if(x<0){
            velx = 1;
        }else if(x>windows_width - 2*radius){
            velx = -1;
        }
        
        // when the ball hits the boundary
        // invert the velocity y
        if(y<0){
            vely = 1;
        }else if(y>windows_height - 2*radius){
            vely = -1;
        }
        
        // move the ball
        circle.move(velx,vely);
        
        // draw the ball on the canvas
        window.draw(circle);
        
        // showing
        window.display();
    }
    return 0;
}