Out of Range errors?

I cannot figure out the perc function because it seems to always give me an out of range error

I am trying to check if the up, left, down, right cells have a COLOR grid, but the grid has to be within range

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//  Paste over this comment with your assessment header.


//        Requires grid file as input (input redirection).
//        Grid file looks similar to:
//
//            xxxSxxxxxxxxxxxxxxx
//            x                 x
//            xxxxxxx xxxxxxxxxxx
//            x                 x
//            xxxxxxxxxxxxxxGxxxx
//
//        Spaces represent empty grid blocks.  Init in EMPTY state.
//        S represents the starting grid block(s). Init in COLOR state.
//        G represents the goal grid block. More than one can exist.
//        Any non-space (x here) is considered a wall. Init in WALL state.

#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
    for (int row = 0; row < grid.size(); row++)
    {
        cout << endl;
        for (int column = 0; column < grid.at(row).size(); column++)
        {
            // "Down"
            if ((grid.at(row+1).at(column) <= grid.size()) && 
            (grid.at(row+1).at(column) == COLOR))
            {
                grid.at(row).at(column) = TO_BE_COLORED;
            }
            
            // "Up"
            if ((grid.at(row-1).size() <= 0) && 
            (grid.at(row-1).at(column) == COLOR))
            {
                grid.at(row).at(column) = TO_BE_COLORED;
            }

            // "Left"
            if ((grid.at(row).at(column-1) <= 0) && 
            (grid.at(row).at(column-1) == COLOR))
            {
                grid.at(row).at(column) = TO_BE_COLORED;
            }
            
            // "Right"
            if ((grid.at(row).at(column+1) >= grid.size()) && 
            (grid.at(row).at(column+1) == COLOR))
            {
                grid.at(row).at(column) = TO_BE_COLORED;
            }
        }
    }
    

    /// Traverse 2D grid converting intermediate states to color state
    
    for (int row = 0; row < grid.size(); row++)
    {
        cout << endl;
        for (int column = 0; column < grid.at(row).size(); column++)
        {
            if (grid.at(row).at(column) == TO_BE_COLORED)
            {
                grid.at(row).at(column) = COLOR;
            }
        }
    }
}


/// @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++)
    {
        cout << endl;
        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();
            }
        }
    }
    cout << endl;
}


// 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;
}
The vector has no sub-vector with index row + 1 when row is equal to grid.size() - 1. So you will always get out of range in the loops of the function

1
2
3
4
5
6
7
8
9
10
11
12
13
void perc(vector <vector <int> > &grid)
{
    // TODO: Implement this function
    
    /// Traverse 2D grid setting intermediate state where applicable
    for (int row = 0; row < grid.size(); row++)
    {
        cout << endl;
        for (int column = 0; column < grid.at(row).size(); column++)
        {
            // "Down"
            if ((grid.at(row+1).at(column) <= grid.size()) && 
            (grid.at(row+1).at(column) == COLOR))
How would I make it look at the row above/below the grid?

Here is my new code

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
void perc(vector <vector <int> > &grid)
{
    // TODO: Implement this function
    
    /// Traverse 2D grid setting intermediate state where applicable
    for (int row = 0; row < grid.size(); row++)
    {
        for (int column = 0; column < grid.at(row).size(); column++)
        {
            if (grid.at(row).at(column) == EMPTY)
            {
                // "Down"
                if (((row + 1) <= grid.size()) && 
                grid.at(row+1).at(column) == COLOR)
                {
                    grid.at(row).at(column) = TO_BE_COLORED;
                }
                
                // "Up"
                if (((row - 1) >= 0) && grid.at(row-1).at(column) == COLOR)
                {
                    grid.at(row).at(column) = TO_BE_COLORED;
                }
                
                // "Left"
                if (((column-1) >= 0) && (grid.at(row).at(column-1) == COLOR))
                {
                    grid.at(row).at(column) = TO_BE_COLORED;
                }
                
                // "Right"
                if (((column+1) <= grid.at(row).size()) && 
                (grid.at(row).at(column+1) == COLOR))
                {
                    grid.at(row).at(column) = TO_BE_COLORED;
                }
            }
        }
    }
Last edited on
It seems you changed nothing. There is no such element in a vector with index

equal to size().

1
2
                if (((row + 1) <= grid.size()) && 
                grid.at(row+1).at(column) == COLOR)
Could you post the finished code for this because I have a similar assignment for my high school C++ class?
Topic archived. No new replies allowed.