Skip to content

Instantly share code, notes, and snippets.

@danielpox
Created November 2, 2018 03:15
Show Gist options
  • Save danielpox/bb9bfa3da64a59aa8a653f5268fbbe8b to your computer and use it in GitHub Desktop.
Save danielpox/bb9bfa3da64a59aa8a653f5268fbbe8b to your computer and use it in GitHub Desktop.
SimAquarium Problem
/*
* Original problem with code
*/
/* Definition */
struct algae_position {
int x;
int y;
};
struct algae_position grid[16][16];
int total_x = 0, total_y = 0;
int i, j;
/* Usage */
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
total_x += grid[i][j].x;
}
}
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
total_y += grid[i][j].y;
}
}
/*
* Solution #1
* # Should be much more efficient and performant.
*/
// ...
int i, j;
/* Usage */
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
total_x += grid[i][j].x;
total_y += grid[i][j].y;
}
}
/*
* Solution #2
* # Not entirely sure if more efficient or not!
*/
// ...
int i, j;
algae_position grid_pos;
/* Usage */
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
grid_pos = grid[i][j];
total_x += grid_pos.x;
total_y += grid_pos.y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment