[go: up one dir, main page]

0% found this document useful (0 votes)
30 views3 pages

Application 6

Uploaded by

spencerqin101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

Application 6

Uploaded by

spencerqin101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ENGR 1181 | Application 6: For loops and While Loops Application|

Individual Assignment
Instructions
Define
• You are being assigned one warmup problem and one application problem. Use the provided
script file: Application6.m for these problems. Include comment statements to help organize
your script file
Represent
• Make a flow chart for your code for the application problem.
Plan
• Download Application6.m
• Outline the steps your program will take by adding comment statements.
Implement
• Complete the problems in the script file
Evaluate
• Comment on your answers to the application problem; are they reasonable? You may do a
calculation (by hand or in Excel) to support your evaluation. You may submit an additional file
containing your evaluate step, or include within comments in your script file.
Document
• Publish your work.
• Submit the following:
o Flow chart
o Script file & Command window output PDF generated
o Evaluate step comments including any calculation(s) done (as a separate file or within script
file comments)

Warmup Problem
A while loop can do whatever a for loop does, but the for loop is easier to program and understand
when the number of iterations is known before the loop is run. The converse is also true – any while
loop can be replaced by a for loop and an if statement with a break instruction. Consider the
following program, which determines the smallest integer N for which the sum of 1 through N is greater
than 1,000:

%don’t know in advance how far to go, but it must be less than 1000/2
for N = 1:500
if sum (1:N) > 1000
break
end
end
fprintf('The sum of integers from 1 through %i exceeds 1000\n', N)
Write the for loop as an equivalent while loop. Remember that there are 3 things that happen
automatically with a for loop that must be done explicitly with a while loop:

1. The loop counter must be initialized prior to the loop


2. The loop counter must be incremented within the loop
3. The stopping condition must be specified in the while statement

Application Problem

Background

In the Humanitarian Relief Lab, you explored the logistics issues involved in shipping and distributing
relief supplies. One aspect of shipping that must be considered is the weight limit of a cargo plane. In
this application, you will write code to determine how many cargo items can be loaded onto a plane,
given the weights of the items and the capacity of the plane.

Problem Statement

You have a list of cargo items to be loaded onto a plane. Your objective is to determine which objects to
load onto the plane, given the weights of the items and the maximum capacity of the plane. You will be
solving this problem first using a for loop and then using a while loop. Your code should return the list
of weights of the items that were loaded onto the plane.

Your code needs to meet the following requirements:

• The sum of the weights of the items loaded is less than or equal to the capacity of the plane
• The unused capacity of the plane is less than the smallest unloaded weight.

Note:

For example: If the weights of the cargo items are 500, 300, 150 and 1200 points and the capacity of the
plane is 1000, then the weights loaded could be 500, 300, 150. This is a correct answer since the sum of
these weights is less than the capacity of the plane, while the remaining item is too heavy to fit on the
plane.

Each weight combination may be multiple possible solutions. For example, for the following weights:
500, 300, 250, 1200, 400, 150

Any of these results is a valid solution:

[500, 300, 150]


[300, 250, 400]
[500, 400]
[500, 250, 150]

Your code is NOT required to find all valid solutions or the best possible solution.
For example, in the third solution above, 900 pounds of cargo were loaded onto a plane that has a
capacity of 1000 pounds; the unused capacity is 100 pounds. This is a valid solution, since the smallest
unloaded weight is 150 pounds, which exceeds the unused capacity.

Programming Specifications

You will solve this problem using both a for loop and a while loop:
• Load the weights from the provided file cargo_data.txt
• Assume that the max capacity of the plane is 5000 lbs
• In part 1, use a while loop to sequence through the list of weights until a stopping condition is
reached and in part 2, use a for loop to do the same.
• Displaying Results: For both the while loop (Part 1) and the for loop (Part 2) store the
outputs in variables of your choosing. Then display the results in the following format (this is
only an illustration of the format; these are not the correct values):

The following weights were loaded onto the plane:

[100, 200, 150, 325]

The total weight loaded is 775 pounds, and the unused capacity is
225 pounds.

Programming Hints:

Since you are not required to produce an optimal solution, the algorithm can be quite simple. One
simple approach is as follows:

1. Initialize list of weights loaded to [], and initialize unused capacity to the maximum capacity of
the plane

2. Begin with the first item in the list

3. Is the weight of this item less than the remaining unused capacity?
a. If yes, append it to the list of items loaded, and subtract its weight from the unused
capacity
b. If no, go to the next item in the list

4. Are there more items in the list, and is the lightest remaining item less than the unused
capacity?
a. If yes, advance to the next item in the list and go back to step 3.
b. If no, return from the function

This simple algorithm will produce a valid solution, but not necessarily a good one. If you want to
challenge yourself, try a more advanced algorithm that will produce a better result. For example, instead
of always loading the next weight in the list (if it is within the unused capacity), you could try a “greedy
algorithm” – always load the heaviest remaining item in the list that is within the unused capacity.

You might also like