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
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
ADVERTISEMENT