Available: Friday October 7
Due: Monday October 17, noon
This assignment is to be done in TEAMS OF TWO PEOPLE. The assignment should be submitted only once by a member of your team, but please ensure that the identification material contains the information for both team members. Follow the instructions in the lab manual for submitting assignments.
Your algorithms should be developed using the format used in class. Your algorithm traces should use the format shown in class; a separate table is to be used for each call to each algorithm. Programs should be based on the template file, and should follow good coding practices as described in the course lab manual.
This shows the corrected formula. There will be no deductions for using the
originally posted formula.
A 60 kilogram object is to be hung from the end of a rigid 3 metre horizontal pole of negligible mass, as shown in the figure below. The pole is attached to a wall by a pivot and is supported by a 3 metre cable which is attached to the wall at a higher point.
The tension on the cable is given by the following equation:
where:
Part a) (20 marks)
Design an algorithm to determine the distance d at which to attach the cable to the pole in order to minimize the tension on the cable. To do this, the algorithm should calculate the tension at 0.05 metre intervals from d = 0.5 metres to d = 2.5 metres, and keep track at which of those points that the tension had the minimum value.
The GIVENS for your algorithm should be the mass of the object, and the lengths of the cable and the pole.
Your algorithm should have as RESULTS the distance d at which the minimum tension occurs, as well as the tension T at that distance.
GIVENS:
Mass (mass
of the object, in kilograms)
CableLength (length of the cable, in metres)
PoleLength (length of the pole, in metres)
RESULTS:
MinDistance (distance, in metres, from wall to
attach cable for minimum tension)
MinTension (minimum tension on cable, in
HEADER:
(MinDistance,
MinTension
) ← FindMinimumTension( Mass, CableLength, PoleLength )
ASSUMPTIONS:
All givens are greater than zero.
CONSTRAINTS:
The pole length should be greater
than two times the distance from the end.
INTERMEDIATES:
Gravity (Constant:
acceleration due to gravity, in metres per second-squared)
DistanceFromEnd (Constant:
distance from wall and end of pole for starting and stopping loop)
Increment (Constant:
difference between distance points to test)
Distance (current distance, in metres, being evaluated
for cable attachment point)
Tension (tension, in
BODY:
Part b) (20 marks)
Translate your algorithm from Question 1 part a) to Java.
The use of the decimal formatter is shown for illustration only;
use of the formatter is not necessary to receive full marks.
// ITI 1220 Fall 2005, Assignment 5,
Question 1b
// Name: Alan Williams, Student# 81069665
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* This program finds the distance
at which to attach a suspension cable
* to
a pole perpendicular to a wall that supports an object with specified
* mass,
in order to minimize the tension on the cable.
*
* To do this, the tension
on the cable is calculated for various distances
* at
which the cable could be attached. The
calculation starts at a distance
* 'distanceFromEnd'
and is repeated with specified change in distance up to
* the
distance 'poleLength - distanceFromEnd'.
*/
public class A5Q1b
{
public
static void main( String[] args )
{
// SET UP
KEYBOARD INPUT
Scanner
keyboard = new Scanner( System.in
);
DecimalFormat df = new DecimalFormat(
"####.##" );
// DECLARE
VARIABLES/DATA DICTIONARY
double mass; //
GIVEN The mass of the object, in
kg.
double poleLength; // GIVEN Length of the pole, in m.
double cableLength; // GIVEN Length of the cable, in m.
double minDistance; // RESULT
Distance with minimum tension, in m.
double minTension;
// RESULT The minimum cable tension, in
double
gravity; // INTERMEDIATE
(Constant) Acceleration due to gravity, in m/s^2
double distanceFromEnd; //
INTERMEDIATE (Constant) Start and end distances from end of pole, in m.
double increment; //
INTERMEDIATE (Constant) Difference between distance points, in m.
double
distance; // INTERMEDIATE Current cable attachment distance, in m.
double tension; // INTERMEDIATE Tension at current distance, in
// PRINT
OUT IDENTIFICATION INFORMATION
System.out.println( );
System.out.println( "ITI 1220 Fall 2005, Assignment 5, Question 1b"
);
System.out.println( "Name: Alan Williams, Student# 81069665" );
System.out.println( );
// READ
IN GIVENS
System.out.println( "Enter
the mass, in kg:" );
mass = keyboard.nextDouble( );
System.out.println( "Enter the pole length, in m:" );
poleLength = keyboard.nextDouble(
);
System.out.println( "Enter the cable length, in m:" );
cableLength = keyboard.nextDouble(
);
// BODY
OF ALGORITHM
// Set up
constants
gravity = 9.8;
distanceFromEnd = 0.5;
increment = 0.05;
//
Initialize other variables. Note that
the maximum possible
// double value
is used for the initial minimum tension value,
// so that any
calculated value should be less than this number.
distance = distanceFromEnd;
minDistance = distance;
minTension = Double.MAX_VALUE;
// Repeat
tension calcluation for each distance in range
// [disanceFromEnd, poleLength - distanceFromEnd],
// with
specified increment in distance.
while (
distance <= poleLength - distanceFromEnd
)
{
// Calculate tension from formula
// This is the corrected version of the formula. No deduction
// if poleLength is used in the denominator as in the original
version.
tension = mass
*
gravity
*
cableLength
*
poleLength
/
( distance * Math.sqrt( cableLength * cableLength
-
distance * distance ) );
//
See if we have the minimum tension seen so far
if ( tension
< minTension )
{
//
Save minimum tension, and distance at which it occurred.
minTension = tension;
minDistance = distance;
}
else
{
; // do nothing
}
//
Go to next distance
distance =
distance + increment;
}
//
PRINT OUT RESULTS AND MODIFIEDS
System.out.println( "The
minimum tension is " + df.format( minTension )
+ " N at distance " + df.format(
minDistance ) + " m." );
}
}
Part a) (15 marks)
Design an algorithm that will determine whether or not an array A of length N is sorted in increasing order. This means that within the array, if you compare the values in two consecutive array positions, the value at the lower array index is less than or equal to the value at the higher array index.
Your algorithm should immediately terminate the loop if it is discovered that the array is not sorted.
GIVENS:
A (an
array)
N (the
number of items in the array)
RESULTS:
IsSorted (true if array A is sorted in
increasing order, and false otherwise)
HEADER:
IsSorted ←
CheckIfArraySorted( A, N )
ASSUMPTIONS:
N is at least 2.
Values in A can be compared with
< operator.
INTERMEDIATES:
Index (current array position)
BODY:
Part b) (10 marks)
Trace your algorithm using this array: { 5, 8, 8, 7, 12 }
# |
Statement |
A |
N |
IsSorted |
Index |
0 |
initial values |
{ 5, 8,
8, 7, 12 } |
5 |
? |
? |
1 |
IsSorted ← True |
|
|
TRUE |
|
2 |
Index ← 1 |
|
|
|
1 |
3 |
Index < N AND IsSorted : true |
|
|
|
|
4 |
A[Index-1] ≤ A[Index] : true |
|
|
|
|
5 |
|
|
|
|
2 |
3 |
|
|
|
|
|
4 |
|
|
|
|
|
5 |
Index ← Index + 1
|
|
|
|
3 |
3 |
Index < N AND IsSorted : true |
|
|
|
|
4 |
A[Index-1] ≤ A[Index] : false |
|
|
|
|
6 |
IsSorted ← False |
|
|
FALSE |
|
3 |
|
|
|
|
|
Part a) (15 marks)
Design an algorithm that will copy the values from a section of AnArray to a new array SubArray, where the section of AnArray to copy is from index Lower to index Upper inclusive. That is, SubArray [0] receives a copy of the value in AnArray[Lower], SubArray [1] receives a copy of AnArray[Lower+1] and so on until the final value that is copied is AnArray[Upper].
GIVENS:
AnArray (an array)
Lower (lower index bound of copy range)
Upper (upper index bound of copy range)
RESULTS:
SubArray (a new array containing the
values from AnArray[Lower] to AnArray[Upper])
HEADER:
SubArray ←
CreateSubArray( AnArray, Lower, Upper )
ASSUMPTIONS:
Lower ≤ Upper
The length of A is at least Upper +
1.
INTERMEDIATES:
NewArraySize (size
of new array)
Index (current array position in new array)
BODY:
Part b) (10 marks)
Trace your algorithm using the following GIVENS:
AnArray = { 20, 19, 18, 17, 16,
15, 14, 13, 12, 11, 10 }
Lower = 3
Upper = 5
# |
Statement |
AnArray |
Lower |
Upper |
NewArraySize |
SubArray |
Index |
0 |
initial values |
{ 20, 19,
18, 17, 16, 15, 14, 13, 12, 11, 10 } |
3 |
5 |
? |
? |
? |
1 |
NewArraySize ← Upper –
Lower + 1 |
|
|
|
3 |
|
|
2 |
SubArray ← MakeNewArray(NewArraySize) |
|
|
|
|
{ ?, ?, ? } |
|
3 |
Index ← 0 |
|
|
|
|
|
0 |
4 |
Index < NewArraySize : true |
|
|
|
|
|
|
5 |
SubArray[Index] ← AnArray[Index + Lower]
|
|
|
|
|
{ 17, ?, ? } |
|
6 |
Index ← Index + 1
|
|
|
|
|
|
1 |
4 |
|
|
|
|
|
|
|
5 |
|
|
|
|
|
{ 17, 16, ? } |
|
6 |
|
|
|
|
|
|
2 |
4 |
|
|
|
|
|
|
|
5 |
|
|
|
|
|
{ 17, 16, 15 } |
|
6 |
|
|
|
|
|
|
3 |
4 |
|
|
|
|
|
|
|