Multiple input and output in C++ - Time & Space Complexity
When a program takes several inputs and produces multiple outputs, it is important to see how the time it takes grows as these inputs get bigger.
We want to know how the total work changes when we have more inputs to handle.
Analyze the time complexity of the following code snippet.
#include <iostream>
using namespace std;
void process(int n, int m) {
for (int i = 0; i < n; i++) {
cout << i << " ";
}
cout << endl;
for (int j = 0; j < m; j++) {
cout << j*j << " ";
}
cout << endl;
}
int main() {
process(5, 3);
return 0;
}
This code prints numbers from 0 to n-1, then prints squares of numbers from 0 to m-1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops printing values.
- How many times: First loop runs n times, second loop runs m times.
Each loop runs independently, so total work is the sum of both.
| Input Size (n, m) | Approx. Operations |
|---|---|
| 10, 5 | 15 |
| 100, 50 | 150 |
| 1000, 500 | 1500 |
Pattern observation: The total steps grow roughly as the sum of n and m.
Time Complexity: O(n + m)
This means the time grows in a straight line with the size of both inputs added together.
[X] Wrong: "The time complexity is O(n * m) because there are two loops."
[OK] Correct: The loops run one after another, not inside each other, so their times add up, not multiply.
Understanding how multiple inputs affect time helps you explain your code clearly and shows you can think about efficiency in real situations.
"What if the second loop was placed inside the first loop? How would the time complexity change?"
Practice
Which of the following statements correctly reads two integers a and b from the user in C++?
Solution
Step 1: Understand input operator usage
In C++,std::cinis used for input and the extraction operator>>reads values.Step 2: Identify correct syntax for multiple inputs
Usingstd::cin >> a >> b;reads two integers from the user correctly.Final Answer:
std::cin >> a >> b; -> Option DQuick Check:
Input uses std::cin with >> operator [OK]
- Using << instead of >> for input
- Using std::cout for input
- Mixing input and output operators
Which of the following lines correctly prints three variables x, y, and z separated by spaces in C++?
Solution
Step 1: Understand output operator usage
In C++,std::coutis used for output and the insertion operator<<prints values.Step 2: Print multiple variables with spaces
Usingstd::cout << x << ' ' << y << ' ' << z;prints variables separated by spaces correctly.Final Answer:
std::cout << x << ' ' << y << ' ' << z; -> Option AQuick Check:
Output uses std::cout with << and spaces [OK]
- Using commas instead of << for output
- Using >> operator for output
- Using std::cin for output
What is the output of the following C++ code if the user inputs 5 10?
int a, b; std::cin >> a >> b; std::cout << a + b << ' ' << a * b << std::endl;
Solution
Step 1: Read input values
The code reads two integers: a = 5 and b = 10 from user input.Step 2: Calculate and print sum and product
The output printsa + b = 15anda * b = 50separated by a space.Final Answer:
15 50 -> Option AQuick Check:
Sum and product printed with space [OK]
- Concatenating numbers as strings
- Forgetting space between outputs
- Confusing input and output operators
Identify the error in the following C++ code snippet:
int x, y; std::cin << x << y; std::cout << x << y;
Solution
Step 1: Check input operator usage
Input usesstd::cinwith extraction operator>>, but code uses<<which is wrong.Step 2: Check output operator usage
The code correctly usesstd::coutwith insertion operator<<.Final Answer:
<< used instead of >> for input -> Option CQuick Check:
Input needs >> operator, not << [OK]
- Swapping << and >> operators
- Confusing std::cin and std::cout
- Ignoring operator direction
Given the following code, what will be the output if the user inputs 3 4 5?
int a, b, c; std::cin >> a >> b >> c; std::cout << a + b << ' ' << b + c << ' ' << a + c << std::endl;
Solution
Step 1: Read three integers from input
The variables are assigned as a=3, b=4, c=5 from user input.Step 2: Calculate sums and print
Printsa + b = 7,b + c = 9, anda + c = 8separated by spaces.Final Answer:
7 9 8 -> Option BQuick Check:
Sum pairs printed correctly [OK]
- Adding all three instead of pairs
- Mixing input order
- Forgetting spaces between outputs
Ready to prove it?
Pass the test and this pattern moves unknown
