Assignment operators in C - Time & Space Complexity
Let's see how assignment operators affect the time it takes for a program to run.
We want to know how the number of assignments changes as the program runs with bigger inputs.
Analyze the time complexity of the following code snippet.
int sum = 0;
for (int i = 0; i < n; i++) {
sum += i; // assignment with addition
}
return sum;
This code adds numbers from 0 up to n-1 and stores the result in sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The assignment
sum += iinside the loop. - How many times: It runs once for each number from 0 to n-1, so n times.
As n gets bigger, the number of assignments grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The number of assignments grows in a straight line as n increases.
Time Complexity: O(n)
This means the time to complete the assignments grows directly with the size of the input.
[X] Wrong: "Assignment operations inside a loop don't affect time much because they are simple."
[OK] Correct: Even simple assignments add up when repeated many times, so they do affect total time.
Understanding how assignments inside loops affect time helps you explain code efficiency clearly and confidently.
"What if we replaced the assignment sum += i with a function call inside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of += operator
The operator += adds the value on the right to the variable on the left and stores the result back in the left variable.Step 2: Compare with other operators
Other operators like -= subtract, *= multiply, and /= divide, so they do not add.Final Answer:
+= -> Option BQuick Check:
+= means add and assign [OK]
- Confusing '+=' with '=' which just assigns
- Mixing '+=' with '-=' or '*='
- Thinking '+=' only adds without assignment
x by 5 and assign the result back to x?Solution
Step 1: Identify correct compound assignment syntax
The correct syntax for multiplying and assigning is 'x *= 5;'. It combines multiplication and assignment.Step 2: Check other options for syntax errors
x =* 5; 'x =* 5;' is invalid syntax. x = *5; 'x = *5;' is invalid because '*5' is not a valid expression. x = x *5; 'x = x *5;' is correct but not a compound assignment operator.Final Answer:
x *= 5; -> Option DQuick Check:
'*=' is multiply and assign operator [OK]
- Writing '=*' instead of '*='
- Forgetting space or semicolon
- Using 'x = *5;' which is invalid
int a = 10;
a += 5;
a -= 3;
printf("%d", a);
Solution
Step 1: Calculate after first operation 'a += 5;'
Starting with a = 10, adding 5 gives a = 15.Step 2: Calculate after second operation 'a -= 3;'
Subtracting 3 from 15 gives a = 12.Final Answer:
12 -> Option CQuick Check:
10 + 5 - 3 = 12 [OK]
- Adding and subtracting in wrong order
- Confusing '+=' and '-=' effects
- Ignoring initial value of variable
int x = 7;
x +== 3;
printf("%d", x);
Solution
Step 1: Check the operator '+==' in the code
The operator '+==' does not exist in C. The correct operator for addition assignment is '+='.Step 2: Understand the effect of invalid operator
Using '+==' causes a syntax error and the code will not compile.Final Answer:
Syntax error due to '+==' operator -> Option AQuick Check:
Invalid operator '+==' causes syntax error [OK]
- Typing '+==' instead of '+='
- Assuming '+==' works like '=='
- Ignoring compiler error messages
result? int result = 20;
result /= 4;
result *= 3;
result %= 5;
result += 2;
printf("%d", result);
Solution
Step 1: Perform division assignment 'result /= 4;'
Starting with result = 20, dividing by 4 gives result = 5.Step 2: Perform multiplication assignment 'result *= 3;'
Multiplying 5 by 3 gives result = 15.Step 3: Perform modulus assignment 'result %= 5;'
15 modulo 5 is 0, so result = 0.Step 4: Perform addition assignment 'result += 2;'
Adding 2 to 0 gives result = 2.Final Answer:
2 -> Option AQuick Check:
20/4=5, 5*3=15, 15%5=0, 0+2=2 [OK]
- Skipping modulus step or calculating it wrong
- Mixing up order of operations
- Forgetting that %= gives remainder
Ready to prove it?
Pass the test and this pattern moves unknown
