By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,470 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,470 IT Pros & Developers. It's quick & easy.

My program keeps crashing. The problem seems to be in the for loop or the recursive f

P: 1
This is the program - its supposed to flood fill a vector, but it keeps crashing upon me running it. The problem seems to be in both the for loops and the fill void
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. void fill ( vector <vector  <int> > k, int a, int b  ) {
  8.  
  9.  
  10. if (k[a][b+1] != 1) {
  11.     k[a][b+1] = 1;
  12.     fill(k, a, b + 1);
  13. }
  14.  
  15. if (k[a][b-1] != 1) {
  16.     k[a][b-1] = 1;
  17.     fill(k, a, b);
  18. } if (k[a+1][b] != 1) {
  19.     k[a + 1][b] = 1;
  20.     fill(k, a + 1, b);
  21. } if (k[a - 1][b] != 1) {
  22.     k[a - 1][b+1] = 1;
  23.     fill(k, a - 1, b);
  24. }
  25. }
  26.  
  27. int main () {
  28.     int a,b;
  29. vector <vector <int> >  op;
  30. for ( int i = 0; i < 5; i++ ) {
  31.     for ( int j = 0; j < 5; j++) {
  32.  
  33.         cin  >> op[i][j] ;
  34.  
  35.  
  36.     }
  37.  
  38.  
  39. }
  40.  
  41. cin >> a >> b;
  42.  
  43. for ( int i = 0; i < 5; i++ ) {
  44.     for ( int j = 0; i < 5; j++) {
  45.  
  46.         cout << op[i][j];
  47.  
  48.  
  49.     }
  50.  
  51.  
  52. }
  53. fill(op,a,b);
  54. }
4 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


100+
P: 110
Line 30,Type miss in for statement
Expand|Select|Wrap|Line Numbers
  1. for ( int j = 0; j < 5; j++) {

Use push_back function or emplace_back function to add elements to the vector.

In this case, the size of the vector is fixed (5x5),
Declare as follows:
Expand|Select|Wrap|Line Numbers
  1. vector <vector <int>> op (5, vector <int> (5));
  2.  
4 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.