MATLAB: A) How many sausages in total are eaten when the first dog is full? b) In this case we do not really need a function to check if the dog eating the last sausage is full. Suggest a modification of the game without a function.

homework

This question is about a simple computer game where hungry dogs are fighting for sausages. The dogs get one sausage at the time and have to fight to eat the sausage. The players decide how many dogs will fight for the sausages and which dog to bet on. The player betting on the dog that first is full is the winner. The game consists of the program below and the function isHungry just below the program. Three players play the game with three dogs and the variable eatSausage get the following series of random number: eatSausage = [3, 2, 3, 1, 1, 2, 1, 3, 3, 2, 1, 3, 3, 3, 1, 2, 3, 3, 2, 3, 2, 2, 1, 1] Note that the series is longer than actually required. % This game is about dogs eating sausages. The program asks how many number of dogs to include and the % players decide which dog to bet on. When the game starts the dogs are fed with one sausage at the time
% and a winner is randomly found. After each new sausage the function isHungry is called to check if the
% last dog having a sausage is still hungry.
nbrDogs = input('Give the number of dogs: ');
hungry = true; % Will be true as long as all dogs are hungry
dogs = zeros(1, nbrDogs); % A vector for the number of sausages per dog
while hungry
eatSausage = randi(nbrDogs); % Finds which dog that “wins” a sausage
dogs(eatSausage) = dogs(eatSausage) + 1;
hungry = isHungry(dogs(eatSausage));
end
fprintf('Dog number %d have had enough of sausages\n', eatSausage)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ hungryBoolean ] = isHungry( nbrSausages )
% isHungry tests if a dog is hungry depending. It the dog has eaten 8
% sausages the dog is no longer hungry
if nbrSausages >= 8
hungryBoolean = false;
else
hungryBoolean = true;
end
end

Best Answer

Just run the program and sum the sausages. Isn't it as simple as that, or am I missing something?
numSausagesEaten = sum(dogs)
Remember, there is one element per dog and the value of the element is the number of sausages that dog (element) has eaten. So just sum the vector to get the total number of sausages eaten by all dogs.