Identify Groups on a Board - Step 3
◀ Identify Groups on a Board - Step 2▶ Identify Groups on a Board - Step 4 Amazon
Let’s run the 3rd step of the Four-Step Programming Model!
Four Step Programming Model: Step 3
Interrelation: tempvx, tempvy, groupX, and groupY are vector and if you use [] to access their elements, be sure the index doesn’t go out of their bounds.
State: occupy and handled, once changed, are changed for the lifetime of the program. width and height should never be changed once given by the file. tempvx and tempvy should be emptied after a group is determined so that they can be reused to get the next group. groupX and groupY keep on storing the groups until all groups are found.
Scope: Let’s scrutinize process() and see what variables it needs access to. It needs handled, occupied, tempvx, tempvy, width, and height. It needs width and height to make sure the surrounding squares do not go out of the grid. We can make those 6 variables global. If doing so gives us too much trouble later, we can always change their scopes. The other variables can stay local.
Given this information, let’s update our skeleton:
bool **occupy, **handled;
int width, height;
vector<int> tempvx, tempvy;
void process(int, int);
int main(int argc, char **argv){
- if arguments are not correct, give an error message and quit
char *fileIn, *fileOut;
ifstream fin;
ofstream fout;
vector<vector<int> > groupX, groupY;
- receive locations of people in the given file and assign proper values to all squares on the grid
- go through each square on the grid, find the groups, and store them into a vector
- output results to the output file, specified by the command line
}
/*
precondition: x and y can never go out of the grid’s bounds
postcondition: tempvx and tempvy will contain people belonging to a group
*/
void process(int x, int y) {
- same
}
Keep in mind that we may need more variables as we code.
Let's look at the next step!
◀ Identify Groups on a Board - Step 2▶ Identify Groups on a Board - Step 4