in the following code how many assignment operations occur? if the if statement is true 50% of the time? I need some help dose not seem hard but my number is wrong. any help would be appreciated
1 2 3 4 5 6 7 8 9 10
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (arr[j] < arr[i){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
one of my many answers was = 25102
formula i used was as follows
( 100*100 )+( 100(for j being initialized)+( 1 for i being initialized)+(100*100)/2)*3 for the if statement being true 50% of the time then times 3 for the variables when the if condition is met.)
Taking into account that the if statement is true 50% then the total number oof assignments is equal tp 15000.
In the code snip there are only three assignment operators
1 2 3
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
So as the total number of iterations is 100 * 100 = 10000 and only in 50% the if statement is true then there are 15000 ( ( 100 * 100 ) / 2 * 3 ) assignments.
int i = 0; is not an assignment. It is a definition of an object with its initialization. i++ is not also an assignment. It is an increment.
The assignment operator is either single assignment = or a compound assignment as +=, -=, /= and so on.