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...
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
Create a struct MenuItem containing fields for name (a string) and price (a float) of a...
Create a struct MenuItem containing fields for name (a string) and price (a float) of a menu item for a diner. Create a ReadItem() function that takes an istream and a MenuItem (both by reference) and prompts the user for the fields of the MenuItem, loading the values into the struct's fields. Create a PrintItem() function that takes an output stream (by reference) and a MenuItem (by value) and prints the MenuItem fields to the stream in a reasonable one-line...
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...
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT