I would like to know if there's a way for me to get the current value of "j" outside of the foor loop whenever the conditions is true. The variable "totalvalid" will tell me how many times the condition was met but I would also like to know the exact value of j when the condition is true so that I can use it at a later point. So I would want to extract the value of "j" whenever the "totalvalid = totalvalid +1" happens. Sorry if it looks messy. I'm new to coding and still have no idea how to make it cleaner. Thank you.
for(int j = 0; j < stringnumber; j++){
int valid = 0;
if(str[j][10] == '\0'){
for(int k = 0; k < 10; k++){
if(str[j][k] >= 'A' && str[j][k] <= 'Z'){
valid++;
}
}
if (valid == 10){
totalvalid = totalvalid + 1;
}
}
}
It seems that you want an array of numbers from that pass the condition.
My suggestion would be to make an array of ints, where you will keep these numbers.
Before loop:
int *array_of_valid_ints = (int *) calloc(stringnumber, sizeof(int)); // allocate the array
int number_of_valid_ints = 0;
Inside the if statement:
array_of_valid_ints[number_of_valid_ints] = j;
number_of_valid_ints++;
After the loop ends, you can check the good values with:
printf("This are the good ints: ")
for (int i = 0; i < number_of_valid_ints; i++) {
printf("%d ", array_of_valid_ints[i]);
}
printf("\n");
maybe you can define a variable before the loop as int j=0; then use a while loop instead of for.also remember to write j++ in the while loop.this way you can use the value of j outside of the loop too!
So,I have two loops that I would like to attempt to do a 10 x 10 unrolling on. I really have never done this. I have seen some simple examples that did not involve if/else statements or nested loops. So I am kind of at a loss how to do this for these loops.
All the variables are int.
The first loop is:
for (j=0; j < WIDTH; ++j) {
for (i = 0; i < HEIGHT; ++i) {
n = Calculate(prv, i, j);
if (prv[i][j] && (n == 3 || n == 2))
nxt[i][j] = true;
else if (!prv[i][j] && (n == 3))
nxt[i][j] = true;
else
nxt[i][j] = false;
}
}
I believe the secret is doing some sort of multiple accumulators, I am just not quite sure how that would look.
The second loop:
for (ii = i_left; ii < i_right; ++ii) {
for (jj = j_left; jj < j_right; ++jj) {
n += b[ii][jj];
}
}
Again, I believe this too would involve some sort of multiple accumulator approach as well.
Any help getting started on this would be greatly appreciated. Also, if there are any other ways to optimize the loops, I would appreciate those suggestions as well.
Thank you
I need to check whether the elements on the main diagonal of a matrix are even and divisible into sum of its indexes. I remembered such elements in an 1-D array:
for (i=0; i<n; ++i)
for (j=0; j<m; ++j)
{
if ((i == j) && ((arr[i][j] % 2) == 0))
{
arr2[count] = arr[i][j];
++count;
break;
}
}
Then I replace the elements which satisfy the condition with the sum of its indexes and place a special condition for [0][0] because of dividing on 0:
count = 0;
for (i=0; i<n; ++i)
for (j=0; j<m; ++j)
{
if ((i+j != 0) && (arr[i][j] == arr2[count]) && ((arr[i][j] % (i+j)) == 0))
{
arr[i][j] = i+j;
++count;
}
else if (((i+j) == 0) && (arr[i][j] == arr2[count])) arr[i][j] = 0;
}
The trouble is that when the first element is even, it is the only replaced number, and the condition doesn't work for the other elements:
Problem:
arr2 is not filled appropriately. As soon as you fill one element into it, you break out of that loop. Notice the usage of break in that loop. Moreover, you did not update the value of count in that else-if condition which causes your loop to run in vain searching for arr2[0] throughout.
Solution:
Remove that break statement.
Add ++count into that else-if condition.
Bonus:
You have written ugly code. You used an extra array which adds to the space complexity of your code and you have too many loops which increases the time complexity as well. You will understand these things later as you progress but for now, I'll give you a better solution:
// Deal with the (0, 0) case outside the loop
// And avoid an extra else-if inside the loop
if (arr[0][0] % 2 == 0)
arr[0][0] = 0;
// There are n diagonal elements in a matrix of order n
// Row and column indexes of a diagonal element are equal
// So you can eliminate the use of j and just rely on i
for (i = 1; i < n; ++i)
// Check if the diagonal element is even and divisible by the sum of the indices
if (arr[i][i] % 2 == 0 && arr[i][i] % (i + i) == 0)
// Replace the element if the condition is satisfied
arr[i][i] = i + i;
As you can see, this approach does not require any extra space and runs in a very good linear time. You may further optimize it by checking if a number is not odd using bitwise AND !(i & 1), and changing i + i into 2 * i which can be done quickly using the bitwise left shift operator (i << 1).
By the way, why do you want arr[0][0] to be replaced? Division by 0 is undefined.
What does for (; --i >= 0; ) mean in C?
How is the counter getting decremented and how is it different from for ( ; i >= 0; --i)?
They are very similar, but not the same! First you have to understand how a for loop in C gets executed:
As an example:
1 2, 5 4
| | |
v v v
for(i = 0; i < 5; i++) {
// Do something <-- 3
}
As you can see 2, 3, 4, 5 is a loop until the condition is false.
Now you should clearly see how the for loop gets executed. The difference now is that in your first example:
int i = 5;
for ( ; --i >= 0; )
printf("%d\n", i);
The output would be:
4
3
2
1
0
Because after the first check of the condition (Point 2), it executes the code block of the for statement and i already gets decremented.
In your second example:
int i = 5;
for( ; i>=0; --i)
printf("%d\n", i);
The output would be:
5 // See here the difference
4
3
2
1
0
Here you get the difference, because it gets decremented in Point 4, so the first time it runs with the value 5.
These constructs are formally equivalent to
while (--i >= 0)
{
Body;
}
and
while (i >= 0)
{
Body;
--i;
}
Do you better see the difference?
In general, we can convert any for loop or while loop into a set of mostly linear statements with a goto. Doing this may be helpful to compare the code.
Case 1
### for (; --i >= 0; ) { statements; }
; // Initializer statement
start:
bool condition = (--i >= 0); // Conditional
if (condition) { // If true, we execute the body as so:
statements; // The statements inside the loop
; // Empty increment statement
goto start // Go to the top of the loop.
}
Case 2
### for( ; i>=0; --i) { statements; }
; // Initializer statement
start:
bool condition = (i >= 0); // Conditional
if (condition) { // If true, we execute the body as so:
statements; // The statements inside the loop
--i; // Increment statement
goto start; // Go to the top of the loop.
}
Let's compare the "simpler" code from those two cases.
In the first case, we decrement i for the first time before each loop body. In the second case, we decrement i after each loop body.
The easiest way to see that is to consider what happens when you enter this loop when i == 0. In the first case, after this block of code, you would have a resulting value of i == -1. In the second case, i wouldn't have changed (that is, i == 0), because we never reached the increment statement.
for (; --i >= 0; ) works like this:
Say if you have i as 10, it will decrement the i value and will compare 9 >= 0 and so on.
So the output would be like 9, 8... till 0.
While the loop for( ; i>=0;--i) would first go to 10, then decrement the value and then it would check i>=0. So the output would be 10, 9, 8... till 0.
The first one decrements i and then checks the condition.
The second one checks the condition first and if true, decrements i after the loop body has been executed.
Well, it is the same as saying for( i=0 or some value; --i <= 0 ; do nothing){}
and --i is an predecrement operator.
That means when this piece of code, i.e., --i, is being read, the value of i will decrease by 1 at the same moment.
There are three main components in a for loop.
Initialization, condition, and afterthought.
Initialization happens once at the start of the entire statement. Condition happens before every cycle. Afterthought comes after every cycle. Consider i starts at 2:
for (; --i >= 0; )
Initialization: i = 2
Condition: i = 1
Afterthought: i = 1
Condition: i = 0
Afterthought: i = 0
Condition: i = -1
Exit
In the other case
for( ; i>=0; --i)
Initialization: i = 2
Condition: i = 2
Afterthought: i = 1
Condition: i = 1
Afterthought: i = 0
Condition: i = 0
Afterthought: i = -1
Condition: i = -1
Exit
You can see that the second version actually lasts one cycle longer!
The three parameters (initialization, condition and increment) in for are optional (but they still requires the semicolon ;). So, you can safely write for(;;) and it's the same as while(1). So, expressions like
for(; x > y; x++) { ... }
for(;1;x++) { if(x > y) break;
for(;;) { if(x++ > y) break;
for(;x++ > y;) { ... }
Are valid and equivalent. In this, the increment occurs in the conditional parameters, just like in your code example.
What is the difference between the following two constructs? I am getting a different output for each:
for (int counter = 0; (counter < numberOfFolds) && counter != currentFold; counter++)
{
if (instances[counter] < minimum)
{
return (currentFoldHasAtleastMinimum && true);
}
}
AND
for (int counter = 0; (counter < numberOfFolds); counter++)
{
if (counter != currentFold)
{
if (instances[counter] < minimum)
{
return (currentFoldHasAtleastMinimum && true);
}
}
}
Essentially, the second block of code, simply breaks the compound condition in the for loop and takes it inside using an additional if statement (I may be missing something very fundamental here, and it may be really stupid, but I thought they were the same).
Please help. It appears that they are in fact not the same, and I cannot figure out why.
The first condition will end the loop as soon as either sub-condition becomes false (so counter >= numberIfFolds or counter == currentFold). The second loop will only terminate when counter >= numberOfFolds. It will, however, check if counter == currentFold and skip executing those statements if it is. The loop will continue, though.
In the first example, when counter is equal to currentFold the loop terminates.
In the second example, the loop will continue when that condition is met, and instead will only terminate when counter < numberOfFolds is false.