Tracking component energy through nested loops

An engineering test applies energy pulses to three components. They receive two, three, and four pulses, respectively. The first pulse delivered to each component supplies 6 J; every later pulse supplies 2 J.


The MATLAB script uses nested loops, meaning that one loop lies inside another. Every energy variable below stores a numerical value in joules.


MATLAB
totalEnergy = 0;
pulseCount = 0;
for nPulses = 2:4
    componentEnergy = 0;
    for pulse = 1:nPulses
        if pulse == 1
            componentEnergy = componentEnergy + 6;
        else
            componentEnergy = componentEnergy + 2;
        end
        pulseCount = pulseCount + 1;
    end
    totalEnergy = totalEnergy + componentEnergy;
end


After the entire script finishes, determine componentEnergy in J, totalEnergy in J, and the dimensionless pulseCount. Enter all three values as exact integers.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Conditional Statements and Loops Questions: