Bird
Raised Fist0
Simulinkdata~20 mins

Generated C code inspection in Simulink - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Generated C Code Inspection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
A20
B30
C40
D50
Attempts:
2 left
💡 Hint
Sum the values of i*2 for i from 0 to 4.
❓ data_output
intermediate
2: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;
}
A1 4 9 16
B0 2 4 6
C0 1 4 9
D0 1 2 3
Attempts:
2 left
💡 Hint
Each element is the square of its index.
🔧 Debug
advanced
2: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;
}
ANo error, prints 0
BSyntax error: missing semicolon
CType error: incompatible types
DUse of uninitialized variable 'output'
Attempts:
2 left
💡 Hint
Consider what happens when input is 0.
🧠 Conceptual
advanced
1:30remaining
Understanding memory layout in generated C code
In generated C code, which memory segment typically stores global variables?
AData segment
BStack segment
CHeap segment
DCode segment
Attempts:
2 left
💡 Hint
Globals are stored separately from local variables and dynamic memory.
🚀 Application
expert
2: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;
    }
}
AAutomatically parallelizes code across multiple CPU cores
BReduces loop overhead and increases instruction-level parallelism
CDecreases code size significantly
DEliminates the need for array bounds checking
Attempts:
2 left
💡 Hint
Think about how fewer loop iterations affect CPU instructions.

Practice

(1/5)
1. What is the main purpose of inspecting generated C code from a Simulink model?
easy
A. To run the Simulink model faster in simulation
B. To create new Simulink blocks automatically
C. To verify the correctness and understand the behavior of the generated code
D. To convert C code back into Simulink diagrams

Solution

  1. Step 1: Understand the role of generated code inspection

    Generated C code inspection is used to check if the code matches the Simulink model's logic and to understand how the model is translated into code.
  2. Step 2: Eliminate unrelated options

    Creating new blocks, speeding simulation, or converting code back to diagrams are not purposes of code inspection.
  3. Final Answer:

    To verify the correctness and understand the behavior of the generated code -> Option C
  4. Quick Check:

    Code inspection = verify and understand code [OK]
Hint: Code inspection checks correctness and logic match [OK]
Common Mistakes:
  • Thinking code inspection creates new blocks
  • Confusing code inspection with simulation speedup
  • Assuming code inspection converts code back to diagrams
2. Which Simulink tool is primarily used to generate C code from a model?
easy
A. Simulink Profiler
B. Simulink Data Inspector
C. Simulink Real-Time
D. Simulink Coder

Solution

  1. Step 1: Identify the tool for code generation

    Simulink Coder is the tool designed to generate C and C++ code from Simulink models.
  2. Step 2: Confirm other tools' purposes

    Profiler measures performance, Data Inspector visualizes signals, Real-Time runs models on hardware, none generate code.
  3. Final Answer:

    Simulink Coder -> Option D
  4. Quick Check:

    Code generation tool = Simulink Coder [OK]
Hint: Simulink Coder generates C code from models [OK]
Common Mistakes:
  • Confusing Profiler with code generator
  • Mixing Data Inspector with code generation
  • Assuming Real-Time generates code
3. Given the following snippet from generated C code, what does the variable rtU.inputSignal represent?
/* Model step function */
void model_step(void) {
  real_T output = rtU.inputSignal * 2.0;
  rtY.outputSignal = output;
}
medium
A. An output signal from the model
B. An input signal to the model
C. A local temporary variable
D. A constant value defined in the code

Solution

  1. Step 1: Analyze variable naming conventions

    In generated code, rtU prefix usually denotes inputs to the model, so rtU.inputSignal is an input.
  2. Step 2: Check usage in code

    The code multiplies rtU.inputSignal by 2 and assigns to output, confirming it is an input value.
  3. Final Answer:

    An input signal to the model -> Option B
  4. Quick Check:

    rtU = inputs, rtY = outputs [OK]
Hint: rtU prefix means input signals in generated code [OK]
Common Mistakes:
  • Thinking rtU is output or local variable
  • Assuming inputSignal is a constant
  • Confusing variable scope
4. You find this line in generated C code:
int32_T counter = 0
counter += 1;

What is the error here?
medium
A. Missing semicolon after the first statement
B. Incorrect variable type for counter
C. counter cannot be incremented like this
D. Variable counter should be declared as float

Solution

  1. Step 1: Check syntax of variable declaration

    The first line declares counter but misses a semicolon at the end, which is required in C.
  2. Step 2: Validate increment statement

    The increment counter += 1; is correct syntax and valid for int32_T type.
  3. Final Answer:

    Missing semicolon after the first statement -> Option A
  4. Quick Check:

    Every C statement ends with ; [OK]
Hint: Check for missing semicolons in C code lines [OK]
Common Mistakes:
  • Ignoring missing semicolon errors
  • Thinking increment syntax is wrong
  • Confusing variable types unnecessarily
5. You want to optimize generated C code by removing unused signals. Which step should you take in Simulink before generating code?
hard
A. Use the Code Mappings editor to mark unused signals as 'Exclude from code'
B. Enable signal logging for all signals
C. Increase the sample time of all blocks
D. Disable the Simulink Coder entirely

Solution

  1. Step 1: Understand code optimization options

    Simulink allows marking signals as unused in the Code Mappings editor to exclude them from generated code.
  2. Step 2: Evaluate other options

    Enabling logging increases code size, increasing sample time changes model behavior, disabling coder stops code generation.
  3. Final Answer:

    Use the Code Mappings editor to mark unused signals as 'Exclude from code' -> Option A
  4. Quick Check:

    Exclude unused signals via Code Mappings [OK]
Hint: Mark unused signals to exclude in Code Mappings editor [OK]
Common Mistakes:
  • Enabling logging increases code size
  • Changing sample time affects model timing
  • Disabling coder stops code generation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard