You are using a version without Ads of this website. Please, consider donating:

### 2D Vectors

Hello!

I just recently learned about 2D vectors and my instructor is having us do a percolation assignment. I'm not exactly sure how to "printGrid" because my grid isn't coming up as aligned.

I have posted the template provided by my instructor and we are only to do the printGrid function, the perc function, and the percComplete function. But when I tried running it via percolate.out < grid.txt, my terminal doesn't show a rectangular shape.. I'm not sure what I'm doing wrong.

Note:
(1) I have not started the perc or percComplete yet
(2) The do while loop is ignored because I want to figure out the printGrid function

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113`` ``````#include #include using namespace std; // Provided functions void animationPause(); void clearScreen(); void printEmpty(); void printColor(); void printWall(); void readGrid(vector > &, vector > &); // States for grid spots for use throughout the program const int EMPTY = 1; const int WALL = 2; const int COLOR = 3; const int TO_BE_COLORED = 4; /// @brief percolates the fluid from grid blocks with fluid in them /// @param grid the current grid filled with current states void perc(vector > &grid) { // TODO: Implement this function /// Traverse 2D grid setting intermediate state where applicable /// Traverse 2D grid converting intermediate states to color state } /// @brief determines whether the percolation has reached goal cell /// @param grid the grid in its current state /// @param goals the 2D vector filled with true/false based on if cell is goal /// @return true if grid block is a goal and it is colored, otherwise false bool percComplete(const vector > &grid, const vector > &goals) { // TODO: Implement this function /// Traverse the 2D goals vector and return true if the cell is a goal /// and the same cell within the grid is in the COLOR state. Making sure /// to check bounds when appropriate. return false; } /// @brief prints the NxM grid /// /// Calls the respective print helper based on the state of the grid cell. /// /// @param grid the grid to print, filled with integer states for each cell void printGrid(const vector > &grid) { // TODO: implement this function /// Clear the screen clearScreen(); /// Traverse 2D grid, print each cell using each state's helper functions. for (int row = 0; row < grid.size(); row++) { for (int column = 0; column < grid.at(row).size(); column++) { if (grid.at(row).at(column) == WALL) { printWall(); } else if (grid.at(row).at(column) == COLOR) { printColor(); } else { printEmpty(); } } } } // DO NOT alter code in main int main() { // 2D vectors to store states of grid and whether it is a goal cell vector > grid; vector > goals; // Read in the grid from input readGrid(grid, goals); // Print initial grid printGrid(grid); // Run the simulation /*do { // Animation animationPause(); // Percolate perc(grid); // Print updated grid printGrid(grid); }while(!percComplete(grid, goals));*/ return 0; }``````
Last edited on
Where are the printWall, printColor, and printEmpty functions? I mean, what are they doing?
If you print just one digit, I think you need to put a new line character (endl) at the end of each row
The ones under // Provided functions are provided by my instructor in a separate file. He said don't worry about them.

Thanks :)
Last edited on
Topic archived. No new replies allowed.