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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <vector>

using namespace std;

// Provided functions
void animationPause();
void clearScreen();
void printEmpty();
void printColor();
void printWall();
void readGrid(vector <vector <int> > &, vector <vector <bool> > &);


// 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 <vector <int> > &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 <vector <int> > &grid,
    const vector <vector <bool> > &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 <vector <int> > &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 <vector <int> > grid;
    vector <vector <bool> > 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.