How do i execute an IF statment inside a double for loop, checking if an object in an array equals the selectedItem.label? Here is my try! (didn't work)
function klikkA(evt:Event):void{
for( var j:int = 0; j < 4; j++)
{
for (var k:int = 0; k < 8; k++)
{
if (listeA.selectedItem.label != myArray[j][k])
{
continue;
}
else if(listeA.selectedItem.label == myArray[j][k])
{
txtFlagg.text = myArray[j][k];
break;
}
}
}
}
The break statement alone will only break the inner loop.
As you can see in the documentation of break, you can specify a label
break [label]
It explains:
In nested loops, break only skips the rest of the immediate loop and does not break out of the entire series of nested loops. To break out of an entire series of nested loops, use label
There's additional documentation on the label keyword
It provides an example comparable to yours:
outerLoop: for (var i:int = 0; i < 10; i++) {
for (var j:int = 0; j < 10; j++) {
if ( (i == 8) && (j == 0)) {
break outerLoop;
}
trace(10 * i + j);
}
}
/*
1
2
...
79
*/
Since break can only jump out of single loop you need to either use flag or goto.
I'd use flag that outer loop(s) check as it generally more accepted practice:
var found = false;
for( var j:int = 0; !found && j < 4; j++)
{
for (var k:int = 0; k < 8; k++)
{
.... if(listeA.selectedItem.label == myArray[j][k])
{ ....
found = true;
break;
}
}
}
Related
What I am trying to do (not very successful) is if my code detects a signal (if(matrix[i][j] ==1)) coming (1 or 0) for the next few steps I want my code to write in a new matrix: newmatrix[i][j]=10 and if not to continue with 0. Here is my code so far:
for (i = 0; i < rows; i++) {
j = 0;
do {
if (matrix[i][j] == 1) {
int m = j;
while (j < m + 3) {
newmatrix[i][j] = 10;
printf("newmatrix[%i][%i] and %f\n", i, j, newmatrix[i][j]);
j++;
continue;
}
}
if (matrix[i][j] == 0) {
newmatrix[i][j] = 0;
printf("newmatrix[%i][%i] and 0 is %f\n", i, j, newmatrix[i][j]);
j++;
continue;
}
j++;
} while (j < MAXTIME);
}
}
The problem is that if there is a signal near the end instead of stopping when to column count reaches the max number the code inserts new columns even though they are only 10:
Where is my mistake can someone point me to the right direction? Is there maybe a way to do this cleaner with goto statement?
Here is a simpler approach with a temporary variable:
for (i = 0; i < rows; i++) {
int spike = 0;
for (j = 0; j < MAXTIME; j++) {
if (matrix[i][j] == 1) {
spike = 3;
}
if (spike) {
newmatrix[i][j] = 10;
spike--;
} else {
newmatrix[i][j] = 0;
}
printf("newmatrix[%i][%i] is %f\n", i, j, newmatrix[i][j]);
}
}
Notes:
I am assuming that matrix[i][j] is either 0 or 1. If other values are possible and newmatrix[i][j] should stay unmodified for these cells, the code should be modified.
It is advisable to only modify a loop index in the for update clause. do / while loops are notoriously error prone, especially with nested loops that also modify the loop index as is the case in your code.
What is the output of this C code?
//The output gives 5 hi's. I can't understand how it is 5. I think the output may 8 hi's. So I want an explanation for this output.
void main()
{
int i = 0, j = 0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 4; j++)
{
if (i > 1)
break;
}
printf("Hi\n");
}
}
Actually Your Hi is working on this loop
for (i = 0; i < 5; i++)
{
printf("Hi\n");
}
Your inner loop has no effect on output because there is no output statement there
just a break statement
see
for (j = 0;j < 4; j++)
{
if (i > 1)
break;
}
that's why You have only 5 Hi on your output according to values of i
Happy Coding
for (j = 0;j < 4; j++)
{
if (i > 1)
break;
}
This for loop does nothing essentially.
The inner for loop doesn't really do anything. The only thing that really happens is it checksif (i>1) and it gets out of the inner loop.
So the execution goes back into the outer loop and "hi" is printed once for every i value
I'm trying to track a player's location with x marking their spot. When the player enters a string I increment the coordinates accordingly. However when the player is located one space from the perimeter, then attempts to move to the edge of the map, the player disappears.
Example:
.....
...x.
.....
.....
.....
Player located at 'x'
If player enters string "right" and I move player_loc, array simply returns:
.....
.....
.....
.....
.....
I attempted to add a sort of buffer by increasing the size of the array. No luck. I've been stuck on this for almost a week now. Any help would be appreciated. I apologize for messy code. I'm a total newbie at this and I'm really just futzing around in the dark with all this stuff. I've researched this across the forums here and haven't found a solution. If you know of something that I possibly (probably) missed feel free to point me in that direction.
#include <stdio.h>
#include <string.h>
char map[6][6];
char player_loc = 'x';
int row;
int col;
void init_map()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
map[i][j] = '.';
}
}
}
void print_map()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
}
int get_player_loc()
{
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 5; k++) {
if(map[j][k] == player_loc)
{
row = k;
col = j;
}
}
}
return row;
return col;
}
void init_player_loc()
{
int check = 1;
for (int g = 0; g < 5; g++) {
for (int h = 0; h < 5; h++) {
if (map[g][h] == 'x') {
check = 0;
}
}
}
if(check == 1) {
map[0][0] = player_loc;
} else {
get_player_loc();
}
}
void move_left()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (map[i][j] == player_loc) {
map[i][j-1] = player_loc;
map[i][j] = '.';
}
}
}
}
void move_right()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (map[i][j] == player_loc) {
map[i][j+1] = player_loc;
map[i][j] = '.';
}
}
}
}
int main(int argc, char* argv[])
{
char input[15];
printf("You enter a room...you can go left, right, or straight. Which way do you go?\n");
int done = 0;
init_map();
map[3][3] = player_loc;
//init_player_loc();
print_map();
while (!done) {
scanf("%s", input);
if (strcmp("left", input) == 0) {
move_left();
printf("You go left...\n");
print_map();
get_player_loc();
printf("%d %d\n", row, col);
done = 1;
}
else if (strcmp("right", input) == 0) {
move_right();
printf("You go right...\n");
print_map();
get_player_loc();
printf("%d %d\n", row, col);
done = 1;
}
else if (strcmp("straight", input) == 0) {
printf("You go straight...");
done = 1;
}
else {
printf("Sorry, can't do that.\n");
}
}
}
You must break the loop if you find the player location, e.g
void move_right()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (map[i][j] == player_loc) {
map[i][j+1] = player_loc;
map[i][j] = '.';
return;
}
}
}
}
In your code you move right the player, and the next loop will find the player in the new location and do the right move again, forever.
Moreover in your code you are not taking care of boundaries of your 2d matrix: j+1 is valid only if j<5.
Then a better code should be
void move_right()
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (map[i][j] == player_loc) {
map[i][j+1] = player_loc;
map[i][j] = '.';
return;
}
}
}
}
The problem is that your move_right function picks up the player and moves them completely off of the map. Let's say your player is at [0, 2] and step through the code.
for (int j = 0; j < 5; j++) {
if (map[i][j] == player_loc) {
map[i][j+1] = player_loc;
map[i][j] = '.';
}
}
[0, 0] No player here, move along
[0, 1] No player here, move along
[0, 2] Found a player! Move them right to [0, 3]
[0, 3] Found a player! Move them right to [0, 4]
[0, 4] Found a player! Move them right to [0, 5]
At 5, the loop ends. Because of the buffer you added, your array is 6x6, so the player is stashed in the wings without crashing the program. There are a few things you should do:
Once you've found and moved the player, break or return so they'll only move once.
Make your array 5x5 (or print all 6x6) so you can see everything.
Do some bounds checking so the player isn't allowed to move right from j = 5.
Watch out for this same bug in move_up, where it would happen as you increment i.
Your loops allow for checking the position twice, once at i,j, and again at i,(j+1) (or some other variant). This probably isn't what you intend. After you find the player you should make the updates and then break out of the loops.
Also, the code as is allows for indexing passed the bounds of the array, in theory. Also not what is desired. You may consider bounds checking. I don't know what is supposed to happen when the player moves right and there is a wall to the right. Does he not move? Wrap around? LR corner could cause seg fault as it is now.
You appear to have row and column indeces transposed in the get_player_loc function, as well as having two return statements (the compiler should warn you about unreachable code), neither of which is required or used by the calling code.
At the start, initialise the row and col variables. (Values taken from your main.)
int row = 3;
int col = 3;
Change the get_player_loc function so that it just updates the globals row and col. It sets row and col to 0 if the player is not found, as per the original.
void get_player_loc(void)
{
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 5; k++) {
if(map[j][k] == player_loc)
{
// The meaning of row and col is set by how they are used
// to index the array in the move and print functions. Keep
// the same order and meaning here.
row = j;
col = k;
return;
}
}
}
// Set row and col to 0 if the location is not found.
row = 0;
col = 0;
map[0][0] = player_loc;
}
You'll still have problems when they reach an edge, due to the index into the array going out of bounds in the move functions, but that's a different problem.
This question contains a JavaScript example but it could possibly be relevant for other languages as well.
I got a 2d binary array (values are set to 1 and 0 only). I want to make an action which toggles all values, meaning turn all 0 to 1 and all 1 to 0.
Which is a better way to do it:
1)
for(var i = 0; i < rowsNum; i++)
{
for(var j = 0; j < colNum; j++)
{
if(arr[i][j] == 0)
{
arr[i][j] = 1;
}
else
{
arr[i][j] = 0;
}
}
}
or
2)
for(var i = 0; i < rowsNum; i++)
{
for(var j = 0; j < colNum; j++)
{
arr[i][j] = 1 - arr[i][j];
}
}
I would like to know if there's a generic method which is best for most cases. Also, specifically regarding JS, is there a better way to do it than these 2 methods?
I would go for the second way of doing it, or I would use the xor operation, like this:
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
arr[i][j] ^= 1;
}
}
The thing is, if statements translate into branching instructions which can be slow due to branch mispredictions. However, the performance gain in an example like this will barely show, and if it makes the code less readable, then it's not worth it. Always optimize last and if it's absolutely necessary.
So I have an array board[][] whose values keep shuffling around. It is a square array with dimensions d*d. I want to check the array to see if all of its values are in ascending order.
for (int i = 0; i < d; i++)
{
for (int j = 0; i < d; i++)
{
if (board[i][j] == (d * i) + j + 1)
{
return true;
}
else
{
return false;
}
}
}
the problem is that as soon as the first element in the array board[0][0] = 1, it returns true, and ends my code. I don't know how to implement it so that it doesn't return true until all the elements in the array are in ascending order, from 1 to (d*d - 1).
Thank you!
If I understood your question correctly I think you want to do this:
int Previous = board[0][0];
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] < Previous)
{
return false;
}
Previous = board[i][j];
}
}
return true; // Only at the end do we know that all elements are in ascending order
Problems with what you have are:
You are returning on either condition instead of returning after all values have been checked
Your second for loop is wrong and references i, not j
Your comparison is comparing board values against the indexes, not other board values
This should work
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] != (d * i) + j + 1)
{
return false;
}
}
}
return true;
Try replacing return true with continue:
for (int i = 0; i < d; i++)
{
for (int j = 0; i < d; i++)
{
if (board[i][j] == (d * i) + j + 1)
{
continue;
}
else
{
return false;
}
}
}
You can also remove the continue clause completely if you reverse the condition:
if (board[i][j] != (d * i) + j + 1)
{
return false;
}
1) First what you mean by ascending order (vertically, horizontally or in diagonal ?)
2) Replace your if statement
if ((board[i][j] > board[i][j+1]) || (board[i][j] > board[i+1][j]))