Question

Example: Function Name: battleFormation formation([3 5 11, formation1.txt) Inputs: (double) 1xN vector of the number of soldimatlab

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code to copy:

%Define the function battleFormation().

function[V] = battleFormation(arr, inputFileName)

%Get the maximum value of the array arr.

maxVal = max(arr);

%Get the size of the vector arr.

[rowSize, vectorSize] = size(arr);

%Fill zeroes at odd positions.

oddArr = zeros(1, vectorSize);

%Fill zeroes in the array of vectorSize by maxVal.

reqArr = zeros(vectorSize, maxVal);

%Start a for loop till the size of the vector.

for index = 1 : vectorSize

%Get th eodd number position of the vector arr.

oddPos = mod(arr(index), 2);

%Assign this odd position value to the odd array at

%current index.

oddArr(index) = oddPos;

%End the for loop.

end

%Add the od position array values.

oddSum = sum(oddArr);

%If the sum of odd position values is not equal to the

%size of the vector.

if oddSum ~= vectorSize

%Display an error message and return from the

%function.

error('Error! Please enter odd values.');

return;

end

%Start a for loop again till the vector size.

for index = 1 : vectorSize

%Get the difference of current value from maximum

%value.

elem = maxVal - arr(index);

%Divide the required element by 2.

b = elem/2;

%Start another for loop from b + 1 to the maxVal –

%b.

for k = b + 1 : maxVal - b

%Assign 1 to the element of require array.

reqArr(index, k) = 1;

%End inner for loop.

end

%End outer for loop.

end

%Open the file name in write mode.

infile = fopen(inputFileName, 'w');

%Start another for loop till the vector size.

for index = 1 : vectorSize

%Form anther vector using reqArr.

reqVector = reqArr(index, :);

%Display the current line number with required

%formations on the file.

fprintf(infile, "\n%d| Line %d has the following line up: ", index, index);

%Start a for loop till the maxVal.

for reqIndex = 1 : maxVal

%Display the required vector.

fprintf(infile, "%d ", reqVector(reqIndex));

%End the inner for loop.

end

%End the outer for loop.

end

%Close the input file.

fclose(infile);

%Use type() command over input file.

type(inputFileName);

%Use save() command over input file name.

save(inputFileName);

%End the function.

end

Add a comment
Know the answer?
Add Answer to:
matlab Example: Function Name: battleFormation formation([3 5 11, formation1.txt) Inputs: (double) 1xN vector of the number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • matlab Example: Function Name: battleFormation formation([3 5 11, formation1.txt) Inputs: (double) 1xN vector of the number...

    matlab Example: Function Name: battleFormation formation([3 5 11, formation1.txt) Inputs: (double) 1xN vector of the number of soldiers each line 2. (char)A file name, indluding extension, for the output file group number Formation1.txt: 1 Line 1 has the following line up: e 1 1 1 0 File Outputs: 21 Line 2 has the folloOwing line up: 1 1 1 1 1 1. A text file showing the soldiers formations 3 Line 3 has the following line up: e e 1...

  • Thank you for your help! Homework 02 - Vectors, Strings, and Masking Function Name: badWeatherman Inputs: 1. (logical)...

    Thank you for your help! Homework 02 - Vectors, Strings, and Masking Function Name: badWeatherman Inputs: 1. (logical) Vector of suspect #1's answers to a lie detector (logical) Vector of suspect #2's answers to a lie detector (logical) Vector of suspect #3's answers to a lie detector (logical) Vector of suspect #4's answers to a lie detector 2" 3. 4. Outputs 1. (char) Sentence stating which suspect stole the fruit Banned Functions: isequal (, isequaln( Background Oh no, there has...

  • Function Name: stPaddysFeast Example search- Inputs 1 (struct) A MxN structure array 2. double) A...

    Using MATLAB, please read question well Function Name: stPaddysFeast Example search- Inputs 1 (struct) A MxN structure array 2. double) A 1x2 vector of your starting position: [row, column] next: [1, 3] next: [1, 1] next: [2, 1) food: 'potato food potato next: 2, 3] step 2 food: start next: [2, 2] food: 'potato' step 3 Outputs next [1, 21 food: food: 1. (ohar)A descriplion of how mary potatoes you found ad where you stopped looking step 4 (stop) Background...

  • There is a file called mat2.txt in our files area under the Assignments folder. Download it...

    There is a file called mat2.txt in our files area under the Assignments folder. Download it and save it in the same folder where this Matlab file (HW08_02.m) is saved. It may be worth opening that text file to see how it is set out. Note well, however, that the file might have a different number of lines and different number of numbers on each line. Write a Matlab program that does the following: Prompt the user for an input...

  • Please provide C language code no c++ ,txt file also needed with comment Finish task 5...

    Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...

  • MATLAB code help! Function Name: jackSparrow Inputs: 1. (double) A 1x2 vector describing your hand 2....

    MATLAB code help! Function Name: jackSparrow Inputs: 1. (double) A 1x2 vector describing your hand 2. (double) A 1x2 vector describing Jack Sparrow's hand 3. (double) A vector with the next cards to be drawn from the deck Outputs: 1. (char) A description of the game outcome Background: You're out at sea with the infamous Jack Sparrow, and it's a pretty calm day. Without anything better to do, Jack Sparrow challenges you to his favorite card game: BlackJack (Sparrow edition)...

  • MATLAB code help! Function Name: chemTimer Inputs: 1. (double) The current position of the hour hand 2. (double) The current position of the minute hand 3. (double) A positive or negative number of m...

    MATLAB code help! Function Name: chemTimer Inputs: 1. (double) The current position of the hour hand 2. (double) The current position of the minute hand 3. (double) A positive or negative number of minutes Outputs: 1. (double) The position of the hour hand after the specified time 2. (double) The position of the minute hand after the specified time Background: Oh no, you were running late to your Chem lab and completely forgot your reaction timer! It's a good thing...

  • Function Name: zoomZoom Inputs: 1. (double) An Nx5 array of scooters' speeds at various times 2. (double) The time...

    Function Name: zoomZoom Inputs: 1. (double) An Nx5 array of scooters' speeds at various times 2. (double) The time at which you want to find the scooters' speeds Outputs: 1. (double) A 1x5 vector of scooters' speeds at the given time Background: Scooter go zoom zoom! Function Descripti on : You will be given an array with times in the first column, and speeds of different electric scooters in columns 2-5 that correspond to those times. Use spline interpolation or...

  • Assume that you will be given a *.txt file, which in the same format of RandSource.txt...

    Assume that you will be given a *.txt file, which in the same format of RandSource.txt (attached). The file has three numbers at each line. Numbers are separated with a space. All number are in the range of [0, 50]. The first two numbers indicate a range. The third one shows number of random numbers to be generated in the range of first two numbers. The range always given in first < second order. If not, raise an error. For...

  • IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files...

    IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files for the following Knapsack Problem that you will solve. KnapsackData1.txt contains a list of four prospective projects for the upcoming year for a particular company: Project0 6 30 Project1 3 14 Project2 4 16 Project3 2 9 Each line in the file provides three pieces of information: 1) String: The name of the project; 2) Integer: The amount of employee labor that will be demanded by the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT