Thursday, April 18, 2013

the solution to my previous question!

So last post I asked how to perform the passing of an array to a function / method in C++ similar to the Java code sample I published here.

 I was informed by a couple of folks to look into C++ Vectors. That turned out to be grand advice so I'd like to share the code that I came up with:

       

void zeroOnlyBubbleSort(vector< vector >&passedArray ){

for(int y = 0; y < passedArray.size(); y++){
bool needNextPass = true;
    while (needNextPass == true){
    needNextPass = false;
       for(int x = 1 ; x < passedArray.size()   ; x++) {  
           if (passedArray[x][y] == 0 && passedArray[x-1][y] != 0){
           int temp = passedArray[x][y];
           passedArray[x][y] = passedArray[x-1][y];
           passedArray[x-1][y]= temp;
           needNextPass = true;
       }  
    }
   }
  }
}

       
 

 This code is a tiny bit different in that it deals with a two dimensional array of vector type but that was a little bit tricky to figure out so I thought it would be nice to share here.

 I also found a couple of nice articles that helped a lot for learning resources so wanted to share those as well: 

This stackoverflow question was very useful: http://stackoverflow.com/questions/5333113/how-to-pass-a-vector-to-a-function 

 And this cplusplus.com article proved nice reference as well: http://www.cplusplus.com/reference/vector/vector/ Thanks for the tips guys!

No comments:

Post a Comment