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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SaleSacko
    New Member
    • Feb 2020
    • 1

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

    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
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    
    void fill ( vector <vector  <int> > k, int a, int b  ) {
    	
    
    if (k[a][b+1] != 1) {
    	k[a][b+1] = 1;
    	fill(k, a, b + 1);
    }
    
    if (k[a][b-1] != 1) {
    	k[a][b-1] = 1;
    	fill(k, a, b);
    } if (k[a+1][b] != 1) {
    	k[a + 1][b] = 1;
    	fill(k, a + 1, b);
    } if (k[a - 1][b] != 1) {
    	k[a - 1][b+1] = 1;
    	fill(k, a - 1, b);
    }
    }
    
    int main () {
    	int a,b;
    vector <vector <int> >  op;
    for ( int i = 0; i < 5; i++ ) {
    	for ( int j = 0; j < 5; j++) {
    	
    		cin  >> op[i][j] ;
    		
    		
    	}
    	
    	
    }
    
    cin >> a >> b;
    
    for ( int i = 0; i < 5; i++ ) {
    	for ( int j = 0; i < 5; j++) {
    	
    		cout << op[i][j];
    		
    		
    	}
    	
    	
    }
    fill(op,a,b);
    }
    Last edited by gits; Feb 25 '20, 08:21 AM. Reason: added code tags
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    Line 30,Type miss in for statement
    Code:
    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:
    Code:
    vector <vector <int>> op (5, vector <int> (5));

    Comment

    • guyfrench
      New Member
      • Apr 2020
      • 1

      #3
      Thank you very much for the very detailed help!

      Comment

      Working...