Your manager at the local supermarket has asked you to create a produce display;
ID: 3799153 • Letter: Y
Question
Your manager at the local supermarket has asked you to create a produce display; he’s asked you to build a pyramid of apples from the upcoming shipment. The pyramid should be erected such that each square of fourapples in one layer supports one apple on the next layer.
Example shown to right:
Top layer (1 apple)
Next layer (2 rows, 2 columns, i.e., 4 apples)
Base layer (3 rows, 3 columns, i.e., 9 apples)
Task 1
Given a specified number of layers, how many apples will be used? Show test cases:
5, 7, and 10 layers.
USE MATLAB CODE
Explanation / Answer
Matlab code find_apples.m for the problem
n = input('Enter number of layers'); % asking the user to enter the number of layers
apples = 0; % initial value in case of no layers
for k=1:n
apples = apples + k*k; %adding appkes in each layer
end
fprintf('The total number of apples required is %d ',apples); % Printing the result
Results
>> find_apples
Enter number of layers: 5
The total number of apples required is 55
>> find_apples
Enter number of layers: 7
The total number of apples required is 140
>> find_apples
Enter number of layers: 10
The total number of apples required is 385
>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.