Splitting code into multiple files - Time & Space Complexity
When we split code into multiple files, we want to know if it changes how long the program takes to run.
Does breaking code into parts affect the speed of the program?
Analyze the time complexity of this simple program split into two files.
// file1.c
#include <stdio.h>
void printNumbers(int n);
int main() {
printNumbers(5);
return 0;
}
// file2.c
#include <stdio.h>
void printNumbers(int n) {
for (int i = 1; i <= n; i++) {
printf("%d\n", i);
}
}
This code prints numbers from 1 to n, but the printing function is in a separate file.
Look for loops or repeated steps that take time.
- Primary operation: The for-loop inside
printNumbersthat prints numbers. - How many times: It runs n times, once for each number.
As n grows, the number of print steps grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print steps |
| 100 | 100 print steps |
| 1000 | 1000 print steps |
Pattern observation: The work grows directly with n, no matter if code is split or not.
Time Complexity: O(n)
This means the time to run grows in a straight line with the input size n.
[X] Wrong: "Splitting code into files makes the program slower because it adds extra steps."
[OK] Correct: Splitting code into files is about organizing code, not about how many times loops run. The main work still depends on the loops, not the file structure.
Understanding that splitting code helps keep things neat without slowing down your program is a useful skill to show.
"What if the function called inside the other file had a nested loop? How would that change the time complexity?"
Practice
Solution
Step 1: Understand code organization
Splitting code into multiple files helps keep related code parts separate and easier to manage.Step 2: Recognize code reuse benefit
It also allows reusing code in different programs without rewriting it.Final Answer:
To organize code and make it reusable -> Option CQuick Check:
Splitting code = Organize and reuse [OK]
- Assuming splitting files makes code run faster
- Thinking header files are not needed
- Believing it reduces variables automatically
utils.h in a C source file?Solution
Step 1: Understand header file inclusion syntax
To include your own header files, use double quotes around the filename.Step 2: Differentiate system and user headers
Angle brackets are for system headers, double quotes for user headers like utils.h.Final Answer:
#include "utils.h" -> Option DQuick Check:
User header files use "filename" [OK]
- Using angle brackets for user headers
- Omitting # in include directive
- Not using quotes or brackets around filename
// file1.c
#include
void greet();
int main() {
greet();
return 0;
}// file2.c
#include
void greet() {
printf("Hello from file2!\n");
}What is the output when compiled and run together?
Solution
Step 1: Check function declaration and definition
file1.c declares greet() and calls it; file2.c defines greet() printing the message.Step 2: Understand linking multiple files
Compiling both files together links greet() correctly, so the program prints the message.Final Answer:
Hello from file2! -> Option AQuick Check:
Function declared and defined across files prints message [OK]
- Forgetting to compile both files together
- Missing function declaration in file1.c
- Expecting compilation error due to separate files
// main.c
#include "helper.h"
int main() {
print_message();
return 0;
}// helper.c
#include
void print_message() {
printf("Hi!\n");
}// helper.h void print_message();
Compiling with
gcc main.c gives an error: undefined reference to 'print_message'. What is the likely cause?Solution
Step 1: Understand the error message
"undefined reference" means the linker can't find the function definition.Step 2: Check compilation command
If helper.c is not compiled or linked, the function print_message() is missing at link time.Final Answer:
helper.c was not compiled or linked properly -> Option BQuick Check:
Linker error = missing compiled file [OK]
- Not compiling helper.c with main.c
- Ignoring linker errors thinking it's a syntax error
- Assuming declaration alone is enough
main.c and math_ops.c. math_ops.c defines int add(int a, int b). How should you organize files and compile to use add in main.c correctly?Solution
Step 1: Create a header file math_ops.h
Declare the function prototype int add(int a, int b); in math_ops.h.Step 2: Include math_ops.h in main.c and compile both files
Include math_ops.h in main.c to use add(), then compile with gcc main.c math_ops.c to link both.Final Answer:
Declare add in math_ops.h, include it in main.c, compile with gcc main.c math_ops.c -> Option AQuick Check:
Header declares, source defines, compile both [OK]
- Including .c files instead of headers
- Not declaring functions before use
- Compiling only one source file
Ready to prove it?
Pass the test and this pattern moves unknown
