Question

In: Computer Science

Function Name: medievalFootball​ Inputs: 1. (struct​ ​) A 1xN structure representing the "field" and the players...

Function Name: medievalFootball​

Inputs:

1. (struct​ ​) A 1xN structure representing the "field" and the players on it

Outputs:

1. (char​ ​) A short sentence describing what happened.

Topics: (​ structures​ ​), (conditionals)​ ​, (iteration​ ​), (cell arrays​ ​)

Background:

As you are backpacking through Europe during your "lifechanging study abroad experience​", you stop in a small town for a bite to eat and hear about an interesting local tradition similar to a game of football. Only it's not American football, it's Medieval Football. They're exactly the same, except Medieval Football has no limit on team size, no set playing field, and no rules on aggression. Instead of joining the locals, you decide to sit this one out and model it in MATLAB instead.

Function Description:

Given a 1xN structure array representing the "game" and only containing the field "player", determine which goal area (the first and last structures) has a player in it. Then count how many yards (absolute value of the amount of structures moved) that player can move across the field without running into another player who politely forces them to stop. Output what happened in a string with the format:

' madeth it <# of yards they ran> rod(s) ere stove in 's kneecap!'

Example: strc →

  

sout = medievalFootball(strc) sout ​ → 'Jim madeth it 3 rod(s) ere Bob stove in Jim's kneecap!'​

Notes:

  • ● The player who is in the goal area (either first or last structure) will be the runner
  • ● There will always be only​ 1 valid runner and at ​ least​ 1 blocker​
  • ● Each structure will have only 1 field with name "player"
  • ● The yards ran will be the absolute value of the index difference between the running and blocking player

Please use matlab to answer question and avoid using the BREAK Function!! This is all the information I am given for the problem!!!!

Solutions

Expert Solution

% medievalFootball.m

function sout = medievalFootball(strc)
% Matlab function that takes as input 1xN structure representing the
% "field" and the players on it, returns a short sentence describing
% what happened.
  
% initialize blockerIndex to 0
blockerIndex = 0;
  
% determine the runnerIndex based on first and last structure player
if(~isempty(strc(1).player))
runnerIndex = 1;
else
runnerIndex = length(strc);
end
  
% based on runnerIndex determine the blockerIndex
if(runnerIndex == 1)
% loop from index 2 to second last index, set blockerIndex to first
% non-empty player
i = 2;
while(i < length(strc) && blockerIndex == 0)
if(~isempty(strc(i).player)) % blocker found, set the blockerIndex and exit the loop
blockerIndex = i;
end
  
i = i + 1;
end
else
% loop from second last index to index 2, set blockerIndex to first
% non-empty player
i = length(strc)-1;
while(i > 1 && blockerIndex == 0)
if(~isempty(strc(i).player)) % blocker found, set the blockerIndex and exit the loop
blockerIndex = i;
end
i = i - 1;
end
end
  
% create the resultant sentence
sout = sprintf('%s madeth it %d rod(s) ere %s in %s%ss kneecap!',strc(runnerIndex).player, abs(runnerIndex-blockerIndex), strc(blockerIndex).player, strc(runnerIndex).player,"'") ;
end

%end of medievalFootball.m

Output:


Related Solutions

Read inputs from console and store in Structure Given a structure of type “struct book” (shown...
Read inputs from console and store in Structure Given a structure of type “struct book” (shown below), read input into struct book from a console. struct book { char name[50]; char author[50] ; float price; }; Write a function: struct book solution() that reads name, author and price into “struct book”. A function return structure variable. Input C Programming: A Modern Approach K.N.King 50.45 where, First line of input represents book name. Second line of input represents book author. Third...
QUESTION 60 Given the following Product structure: struct Product {     string name;     double price;...
QUESTION 60 Given the following Product structure: struct Product {     string name;     double price;     int quantity;     bool equals(const Product&); }; how would you define the equals function so two products are equal if their names and prices are equal? a. bool equals(const Product& to_compare) {     return (name == to_compare.name && price == to_compare.price); } b. bool Product::equals(const Product& to_compare) {     return (name == to_compare.name || price == to_compare.price); } c. bool equals(const Product& to_compare)...
Create a structure in C called BarcelonaPlayer with the following members. struct BarcelonaPlayer { char name[20];...
Create a structure in C called BarcelonaPlayer with the following members. struct BarcelonaPlayer { char name[20]; int age; char country[20]; char Position[20]; double Salary; double Rating; }; First, create an array of BarcelonaPlayer structures. Now, write a function that takes an array of BarcelonaPlayer structures as input and find out the highest paid player among all the players. void highestPaidPlayer(struct BarcelonaPlayer *pl, int size); Create another function that finds all the players from Argentina. void findPlayers(struct BarcelonaPlayer *pl, int size);
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix...
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix is a two-dimensional, rectangular data structure. The matrix struct should have three fields (in this order): an unsigned variable named "rows", an unsigned variable named "columns", and a pointer to a pointer to double (i.e. double**) named "data". (Code #including matrix.h should be able to declare Matrix variables.) In matrix.c, implement the create_matrix function. The Matrix should be filled with 0.0 values. data[i] should...
1. Define a struct for a soccer player that stores their name, jersey number, and total...
1. Define a struct for a soccer player that stores their name, jersey number, and total points scored. 2. Using the struct in #1, write a function that takes an array of soccer players and its size as arguments and returns the average number of points scored by the players. 3. Using the struct in #1, write a function that takes an array of soccer players and its size as arguments and returns the index of the player who scored...
A constructor, with a String parameter representing the name of the item.
USE JAVA Item classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.Inventory classA constructor which takes a String parameter indicating the item type, an int parameter indicating the initial...
13. Describe the sodium-potassium pump, including all players involved, and what the function is 16. Name...
13. Describe the sodium-potassium pump, including all players involved, and what the function is 16. Name 3 components that contribute to the structure of the plasma membrane 17. Draw and label the reaction of the formation of a peptide bond with two generic amino acids
Name four organelles of the cell and give their basic structure and function. give reference
Name four organelles of the cell and give their basic structure and function. give reference
Suppose there are two inputs in the production function, labor and capital, and these two inputs...
Suppose there are two inputs in the production function, labor and capital, and these two inputs are perfect substitutes. The existing technology permits 5 machines to do the work of 2 workers. So the production function is f(E, K) = 2K + 5E. The firm wants to produce q units of output, where q > 0 is some number. Suppose the price of capital is $10 per machine per hour. What combination of inputs will the firm use if the...
Suppose there are two inputs in the production function, labor and capital, and these two inputs...
Suppose there are two inputs in the production function, labor and capital, and these two inputs are perfect substitutes. The existing technology permits 3 machines to do the work of 2 worker. So F(E,K)=2K+3E. The firm wants to produce 60 units of output. Suppose the price of capital is $10 per machine per hour. What combination of inputs will the firm use if the wage rate is $10 or $15 or $20 per hour? What if the firm wants to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT