Created
November 2, 2018 03:15
-
-
Save danielpox/bb9bfa3da64a59aa8a653f5268fbbe8b to your computer and use it in GitHub Desktop.
SimAquarium Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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