Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Your functions returning a unsigned long long are missing a return which leads to undefined behavior (more details on this question more details on this question). You could throw an exception to handle this or you could just return 0.


Not a real issue and mostly a matter of personal preference but the way you check that you are not going out of the bounds of the array could be written in a more natural way.

To check that index b + 3 is in the array, I'd rather read b + 3 < a.size() than b < a.size() - 3.


Even more unusual is the pre-increment in the case of BackDiag() : you add 3 to b and then you consider b - 3.


Once your code re-written to take into account these comments, it looks like :

unsigned long long Horizontal(int a, int b, std::vector<std::vector<int>> grid){
 return (b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a][b+1] * grid[a][b+2] * grid[a][b+3]) :
 0;
}
//comparing all int's that are vertically next to each other
unsigned long long Vertical(int a, int b, std::vector<std::vector<int>> grid){
 return (a + 3 < grid.size()) ?
 (grid[a][b] * grid[a+1][b] * grid[a+2][b] * grid[a+3][b]) :
 0;
}
unsigned long long ForDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a+1][b+1] * grid[a+2][b+2] * grid[a+3][b+3]) :
 0;
}
unsigned long long BackDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b+3] * grid[a+1][b+2] * grid[a+2][b+1] * grid[a+3][b]) :
 0;
}

Now, one thing to notice is that this good is basically always the same. You should try to write a generic function that you can reuse.

The signature would be something like :

unsigned long long computeProduct(int a, int b, int incrA, int incrB, std::vector<std::vector<int>>grid)

Your functions returning a unsigned long long are missing a return which leads to undefined behavior (more details on this question). You could throw an exception to handle this or you could just return 0.


Not a real issue and mostly a matter of personal preference but the way you check that you are not going out of the bounds of the array could be written in a more natural way.

To check that index b + 3 is in the array, I'd rather read b + 3 < a.size() than b < a.size() - 3.


Even more unusual is the pre-increment in the case of BackDiag() : you add 3 to b and then you consider b - 3.


Once your code re-written to take into account these comments, it looks like :

unsigned long long Horizontal(int a, int b, std::vector<std::vector<int>> grid){
 return (b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a][b+1] * grid[a][b+2] * grid[a][b+3]) :
 0;
}
//comparing all int's that are vertically next to each other
unsigned long long Vertical(int a, int b, std::vector<std::vector<int>> grid){
 return (a + 3 < grid.size()) ?
 (grid[a][b] * grid[a+1][b] * grid[a+2][b] * grid[a+3][b]) :
 0;
}
unsigned long long ForDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a+1][b+1] * grid[a+2][b+2] * grid[a+3][b+3]) :
 0;
}
unsigned long long BackDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b+3] * grid[a+1][b+2] * grid[a+2][b+1] * grid[a+3][b]) :
 0;
}

Now, one thing to notice is that this good is basically always the same. You should try to write a generic function that you can reuse.

The signature would be something like :

unsigned long long computeProduct(int a, int b, int incrA, int incrB, std::vector<std::vector<int>>grid)

Your functions returning a unsigned long long are missing a return which leads to undefined behavior (more details on this question). You could throw an exception to handle this or you could just return 0.


Not a real issue and mostly a matter of personal preference but the way you check that you are not going out of the bounds of the array could be written in a more natural way.

To check that index b + 3 is in the array, I'd rather read b + 3 < a.size() than b < a.size() - 3.


Even more unusual is the pre-increment in the case of BackDiag() : you add 3 to b and then you consider b - 3.


Once your code re-written to take into account these comments, it looks like :

unsigned long long Horizontal(int a, int b, std::vector<std::vector<int>> grid){
 return (b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a][b+1] * grid[a][b+2] * grid[a][b+3]) :
 0;
}
//comparing all int's that are vertically next to each other
unsigned long long Vertical(int a, int b, std::vector<std::vector<int>> grid){
 return (a + 3 < grid.size()) ?
 (grid[a][b] * grid[a+1][b] * grid[a+2][b] * grid[a+3][b]) :
 0;
}
unsigned long long ForDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a+1][b+1] * grid[a+2][b+2] * grid[a+3][b+3]) :
 0;
}
unsigned long long BackDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b+3] * grid[a+1][b+2] * grid[a+2][b+1] * grid[a+3][b]) :
 0;
}

Now, one thing to notice is that this good is basically always the same. You should try to write a generic function that you can reuse.

The signature would be something like :

unsigned long long computeProduct(int a, int b, int incrA, int incrB, std::vector<std::vector<int>>grid)
added 4 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Your functions returning a unsigned long long are missing a return which leads to undefined behavior (more details on this question). You could throw an exception to handle this or you could just return 0.


Not a real issue and mostly a matter of personal preference but the way you check that you are not going out of the bounds of the array could be written in a more natural way.

To check that index b + 3 is in the array, I'd rather read b + 3 < a.size() than b < a.size() - 3.


Even more unusual is the pre-increment in the case of BackDiag() : you add 3 to b and then you consider b - 3.


Once your code re-written to take into account these comments, it looks like :

unsigned long long Horizontal(int a, int b, std::vector<std::vector<int>> grid){
 return (b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a][b+1] * grid[a][b+2] * grid[a][b+3]) :
 0;
}
//comparing all int's that are vertically next to each other
unsigned long long Vertical(int a, int b, std::vector<std::vector<int>> grid){
 return (a + 3 < grid.size()) ?
 (grid[a][b] * grid[a+1][b] * grid[a+2][b] * grid[a+3][b]) :
 0;
}
unsigned long long ForDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a+1][b+1] * grid[a+2][b+2] * grid[a+3][b+3]) :
 0;
}
unsigned long long BackDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b+3] * grid[a+1][b+2] * grid[a+2][b+1] * grid[a+3][b]) :
 0;
}

Now, one thing to notice is that this good is basically always the same. You should try to write a generic function that you can reuse.

The signature would be something like :unsigned long long computeProduct(int a, int b, int incrA, int incrB, std::vector<std::vector<int>>grid).

unsigned long long computeProduct(int a, int b, int incrA, int incrB, std::vector<std::vector<int>>grid)

Your functions returning a unsigned long long are missing a return which leads to undefined behavior (more details on this question). You could throw an exception to handle this or you could just return 0.


Not a real issue and mostly a matter of personal preference but the way you check that you are not going out of the bounds of the array could be written in a more natural way.

To check that index b + 3 is in the array, I'd rather read b + 3 < a.size() than b < a.size() - 3.


Even more unusual is the pre-increment in the case of BackDiag() : you add 3 to b and then you consider b - 3.


Once your code re-written to take into account these comments, it looks like :

unsigned long long Horizontal(int a, int b, std::vector<std::vector<int>> grid){
 return (b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a][b+1] * grid[a][b+2] * grid[a][b+3]) :
 0;
}
//comparing all int's that are vertically next to each other
unsigned long long Vertical(int a, int b, std::vector<std::vector<int>> grid){
 return (a + 3 < grid.size()) ?
 (grid[a][b] * grid[a+1][b] * grid[a+2][b] * grid[a+3][b]) :
 0;
}
unsigned long long ForDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a+1][b+1] * grid[a+2][b+2] * grid[a+3][b+3]) :
 0;
}
unsigned long long BackDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b+3] * grid[a+1][b+2] * grid[a+2][b+1] * grid[a+3][b]) :
 0;
}

Now, one thing to notice is that this good is basically always the same. You should try to write a generic function that you can reuse.

The signature would be something like :unsigned long long computeProduct(int a, int b, int incrA, int incrB, std::vector<std::vector<int>>grid).

Your functions returning a unsigned long long are missing a return which leads to undefined behavior (more details on this question). You could throw an exception to handle this or you could just return 0.


Not a real issue and mostly a matter of personal preference but the way you check that you are not going out of the bounds of the array could be written in a more natural way.

To check that index b + 3 is in the array, I'd rather read b + 3 < a.size() than b < a.size() - 3.


Even more unusual is the pre-increment in the case of BackDiag() : you add 3 to b and then you consider b - 3.


Once your code re-written to take into account these comments, it looks like :

unsigned long long Horizontal(int a, int b, std::vector<std::vector<int>> grid){
 return (b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a][b+1] * grid[a][b+2] * grid[a][b+3]) :
 0;
}
//comparing all int's that are vertically next to each other
unsigned long long Vertical(int a, int b, std::vector<std::vector<int>> grid){
 return (a + 3 < grid.size()) ?
 (grid[a][b] * grid[a+1][b] * grid[a+2][b] * grid[a+3][b]) :
 0;
}
unsigned long long ForDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a+1][b+1] * grid[a+2][b+2] * grid[a+3][b+3]) :
 0;
}
unsigned long long BackDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b+3] * grid[a+1][b+2] * grid[a+2][b+1] * grid[a+3][b]) :
 0;
}

Now, one thing to notice is that this good is basically always the same. You should try to write a generic function that you can reuse.

The signature would be something like :

unsigned long long computeProduct(int a, int b, int incrA, int incrB, std::vector<std::vector<int>>grid)
Source Link
SylvainD
  • 29.7k
  • 1
  • 49
  • 93

Your functions returning a unsigned long long are missing a return which leads to undefined behavior (more details on this question). You could throw an exception to handle this or you could just return 0.


Not a real issue and mostly a matter of personal preference but the way you check that you are not going out of the bounds of the array could be written in a more natural way.

To check that index b + 3 is in the array, I'd rather read b + 3 < a.size() than b < a.size() - 3.


Even more unusual is the pre-increment in the case of BackDiag() : you add 3 to b and then you consider b - 3.


Once your code re-written to take into account these comments, it looks like :

unsigned long long Horizontal(int a, int b, std::vector<std::vector<int>> grid){
 return (b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a][b+1] * grid[a][b+2] * grid[a][b+3]) :
 0;
}
//comparing all int's that are vertically next to each other
unsigned long long Vertical(int a, int b, std::vector<std::vector<int>> grid){
 return (a + 3 < grid.size()) ?
 (grid[a][b] * grid[a+1][b] * grid[a+2][b] * grid[a+3][b]) :
 0;
}
unsigned long long ForDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b] * grid[a+1][b+1] * grid[a+2][b+2] * grid[a+3][b+3]) :
 0;
}
unsigned long long BackDiag(int a, int b, std::vector<std::vector<int>>grid){
 return (a + 3 < grid.size() && b + 3 < grid[a].size()) ?
 (grid[a][b+3] * grid[a+1][b+2] * grid[a+2][b+1] * grid[a+3][b]) :
 0;
}

Now, one thing to notice is that this good is basically always the same. You should try to write a generic function that you can reuse.

The signature would be something like : unsigned long long computeProduct(int a, int b, int incrA, int incrB, std::vector<std::vector<int>>grid).

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /