Project 4 Elevator Objectives: I. Use single arrays to solve a problem. 2. Use s
ID: 3831586 • Letter: P
Question
Project 4 Elevator Objectives: I. Use single arrays to solve a problem. 2. Use searching and sorting techniques on a single array Format output properly, according to the directions. 4. Use functions in a program to be able to reuse code. Instructions: Create and print out the two arrays: (Be sure to do this first) You are allowed to hard code these anrays into your program. The data is as follows: Anne 30 Bob 150 Ralph 305 Tim 225 Barbara 135 160 Jane Steve 80 200 Tom Mike 165 Shirley 90 Pam 100 Frank 120 Part II: The elevators in our building have an 1100 lb. load limit. Determine which people in the list above get on the elevator. Print their names, weights, total weight, and how many got on. Part III Rearrange these people in descending sequence by weight and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on Part IV Rearrange these people in ascending sequence by name (USE A DIFFERENTSORT ALGORITHM THAN THE ONE YOU USED IN PART II) and print the two arrays. Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on.
Explanation / Answer
Below are the algorithms for the various functions and the main program.
Note:- All arrays are assumed to be passed by reference.
BEGIN FillElevator(Names[],Weights[],n)
Declare an int variable TotalWeight=0
for i=0 to n /*n is the number of elements in each array*/
TotalWeight=TotalWeight+Weights[i] /*add current weight to TotalWeight*/
if TotalWeight<=1100 /*if TotalWeight<=elevator load capacity*/
print Names[i] + " " + Weights[i] +" got on the elevator" + newline character
else /*if total weight exceeds elevator load capacity*/
TotalWeight=TotalWeight - Weights[i] /*subtract the weight of the person who cannot get on the elevator and caused
TotalWeight to exceed load capacity*/
Exit For /*break out of For loop*/
End for
print i + " people could board the elevator. Total Weight = " + TotalWeight + newline character
return i /*return the number of people allowed to board*/
END FillElevator
BEGIN SortDescending(Names[],Weights[],n)
/*using bubble sort*/
for i=0 to n-1
if Weights[i]<Weights[i+1]
/*first swap Weights elements*/
temp=Weights[i];
Weights[i]=Weights[i+1];
Weights[i+1]=temp;
/*then swap corresponding Names elements*/
temp=Names[i];
Weights[i]=Names[i+1];
Weights[i+1]=temp;
End if
End for
End SortDescending
BEGIN SortAscending(Names[],Weights[],n)
/*using insertion sort*/
for i=0 to n
dataW=Weights[i]
dataN=Names[i]
hole=i
while hole>0 and Weights[hole-1] > dataW
Weights[hole]=Weights[hole-1]
Names[hole]=Names[hole-1]
End while
Weights[hole]=dataW
Names[hole]=dataN
end for
END SortAscending
BEGIN printArrays(Names[],Weights[],n)
for i=0 to n /*n is the number of elements in each array*/
print Names[i] +" "+Weights[i] + newline character /*print name with corresponding weight in each line
End for
END printArrays
BEGIN max(CountAscending,CountDescending,CountUnsorted) /*return greatest of three numbers*/
if CountAscending>CountDescending
if CountAscending>CountUnsorted
return CountAscending
else
return CountUnsorted
else
if CountDescendingcending>CountUnsorted
return CountDescending
else
return CountUnsorted
end if
end if
END max
BEGIN main program
/*first declare the arrays*/
Names={Anne, Bob, Ralph, Tim, Barbara,Jane, Steve, Tom,Mike,Shirley, Pam, Frank}
Weights={30,150,305,225,135,160,80,200,165,90,100,120}
Declare 3 int variables CountUnsorted,CountAscending, CountDescending to store number of people allowed on the elevator
when the arrays are unsorted, sorted by weight in ascending order and sorted by weight in descending order respectively.
/*print the two arrays*/
printArrays(Names,Weights,n)
/*Call FillElevator() to determine and print how many people can get on the elevator with the current arrangement*/
CountUnsorted = FillElevator(Names,Weights,n)
/*Sort the arrays in descending order of weights*/
SortDescending(Names,Weights,n)
/*print the two arrays*/
printArrays(Names,Weights,n)
/*Call FillElevator() to determine and print how many people can get on the elevator with the current arrangement*/
CountDescending = FillElevator(Names,Weights,n)
/*Sort the arrays in ascending order of weights*/
SortAscending(Names,Weights,n)
/*print the two arrays*/
printArrays(Names,Weights,n)
/*Call FillElevator() to determine and print how many people can get on the elevator with the current arrangement*/
CountAscending = FillElevator(Names,Weights,n)
print "Most number of people can board the elevator when the arrays are "+ max(CountAscending,CountDescending,CountUnsorted)
END main program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.