Arithmetic operators in C++ - Time & Space Complexity
We want to see how the time to run arithmetic operations changes as we do more of them.
How does the number of calculations affect the total time?
Analyze the time complexity of the following code snippet.
int sum = 0;
for (int i = 0; i < n; i++) {
sum += i * 2 + 3;
}
return sum;
This code adds up a series of numbers calculated with simple arithmetic inside a loop.
Look for loops or repeated calculations.
- Primary operation: The arithmetic calculation and addition inside the loop.
- How many times: Exactly once for each number from 0 up to n-1, so n times.
Each time n grows, the number of calculations grows the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calculations |
| 100 | 100 calculations |
| 1000 | 1000 calculations |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input size grows.
[X] Wrong: "Arithmetic operations inside a loop take constant time no matter how many times the loop runs."
[OK] Correct: Each arithmetic operation is fast, but doing many of them adds up, so total time grows with the number of loop runs.
Understanding how simple calculations add up helps you explain how programs handle bigger data smoothly.
"What if we replaced the loop with two nested loops each running n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of each operator
The operator%returns the remainder after division, while/returns the quotient.Step 2: Identify the operator for remainder
Since remainder is asked, the correct operator is%.Final Answer:
% -> Option CQuick Check:
Remainder operator = % [OK]
- Confusing / with %
- Using * for remainder
- Using - for remainder
a and b in C++?Solution
Step 1: Recall multiplication operator syntax
In C++, multiplication uses the asterisk symbol*between operands.Step 2: Check each option
Onlya * b;uses the correct operator and syntax.Final Answer:
a * b; -> Option AQuick Check:
Multiplication operator = * [OK]
- Using x instead of *
- Writing multiply as word
- Using dot operator for math
int a = 10, b = 3; std::cout << a / b << std::endl;
Solution
Step 1: Understand integer division in C++
Bothaandbare integers, so division truncates decimals.Step 2: Calculate integer division result
10 divided by 3 is 3.333..., but integer division returns 3.Final Answer:
3 -> Option BQuick Check:
Integer division truncates decimals = 3 [OK]
- Expecting decimal result
- Using floating-point division without casting
- Thinking output is error
int x = 5; int y = 2; int z = x / y.0; std::cout << z << std::endl;
Solution
Step 1: Analyze the divisor
The expressiony.0y.0is invalid syntax in C++ because variables cannot have decimal points.Step 2: Identify the syntax error
The decimal point afterycauses a syntax error; correct would bey * 1.0orstatic_cast<double>(y).Final Answer:
Syntax error due to decimal point in divisor -> Option AQuick Check:
Variable names cannot have decimal points [OK]
- Thinking y.0 is valid float
- Ignoring syntax error
- Confusing variable and literal
int a = 7, b = 4;, which expression correctly calculates the average as a floating-point number in C++?Solution
Step 1: Understand integer vs floating-point division
Using2divides integers, truncating decimals. Using2.0forces floating-point division.Step 2: Evaluate each option
(a + b) / 2 does integer division, result 5. (a + b) / 2.0 divides by 2.0, giving 5.5 as float. Options C and D do not calculate average correctly.Final Answer:
(a + b) / 2.0 -> Option DQuick Check:
Use floating-point divisor for decimal average [OK]
- Using integer 2 causing truncation
- Wrong operator precedence
- Misinterpreting average formula
Ready to prove it?
Pass the test and this pattern moves unknown
