Challenge - 5 Problems
Generated C Code Inspection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple generated C function
What is the output of the following generated C code snippet when
input = 5?Simulink
int generated_function(int input) { int output = 0; for (int i = 0; i < input; i++) { output += i * 2; } return output; } int main() { int result = generated_function(5); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Sum the values of i*2 for i from 0 to 4.
✗ Incorrect
The loop runs from i=0 to i=4. The sum is 0*2 + 1*2 + 2*2 + 3*2 + 4*2 = 0 + 2 + 4 + 6 + 8 = 20.
❓ data_output
intermediate2:00remaining
Inspecting array output from generated code
What is the content of the array
output after running this generated C code?Simulink
void generated_array(int input, int output[]) { for (int i = 0; i < input; i++) { output[i] = i * i; } } int main() { int output[4]; generated_array(4, output); for (int i = 0; i < 4; i++) { printf("%d ", output[i]); } return 0; }
Attempts:
2 left
💡 Hint
Each element is the square of its index.
✗ Incorrect
The loop assigns output[i] = i*i for i from 0 to 3, so output is [0,1,4,9].
🔧 Debug
advanced2:00remaining
Identify the error in generated C code snippet
What error will this generated C code produce when compiled?
Simulink
int generated_error(int input) { int output; if (input > 0) output = input * 2; return output; } int main() { int result = generated_error(0); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Consider what happens when input is 0.
✗ Incorrect
If input is 0, output is never assigned a value but is returned, causing use of uninitialized variable.
🧠 Conceptual
advanced1:30remaining
Understanding memory layout in generated C code
In generated C code, which memory segment typically stores global variables?
Attempts:
2 left
💡 Hint
Globals are stored separately from local variables and dynamic memory.
✗ Incorrect
Global variables are stored in the data segment, which is separate from stack and heap.
🚀 Application
expert2:30remaining
Analyzing performance impact of generated C code loop unrolling
Given this generated C code with loop unrolling, what is the main benefit compared to a simple loop?
Simulink
void generated_unrolled(int *arr, int n) { for (int i = 0; i < n; i += 4) { arr[i] = arr[i] * 2; arr[i+1] = arr[i+1] * 2; arr[i+2] = arr[i+2] * 2; arr[i+3] = arr[i+3] * 2; } }
Attempts:
2 left
💡 Hint
Think about how fewer loop iterations affect CPU instructions.
✗ Incorrect
Loop unrolling reduces the number of loop control instructions and allows the CPU to execute multiple instructions simultaneously.