### assignment operator count

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

 ``12345678910`` `````` 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.)
the operator ++ is also an assignment. It can be written like so: `i = i + 1`
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

 ``123`` `````` 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.

Last edited on
Topic archived. No new replies allowed.