Part I: Create and print out the two arrays: (Be sure to do this first) Anne 30
ID: 3546892 • Letter: P
Question
Part I: Create and print out the two arrays: (Be sure to do this first) Anne 30 Bob 150 Ralph 305 Tim 225 Barbara 135 Jane 160 Steve 80 Tom 200 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. (Anne through Steve is equal to 1085 lbs, so no one can get on after Steve.) Part III: Rearrange these people in ascending 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 DIFFERENT SORT 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. Part V: Have the program determine which method allowed the most people to get on the elevator. The program should compare the three different counts of how many people got on the elevator. This program should include 2 sort methods, a method to determine how many people get on the elevator (will be called 3 times), and a print method which prints both arrays (include a size parameter and it can be called 6 times). All of these methods should be separate from the main method. All code must be written in the C++ language. NOTE: You may hard code the data into the arrays and print the answers on the screen.Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
/*This program should include
2 sort methods,
a method to determine how many people get on the elevator (will be called 3 times),
and a print method which prints both arrays (include a size parameter and it can be called 6 times).
All of these methods should be separate from the main method.
All code must be written in the C++ language.
NOTE: You may hard code the data into the arrays and print the answers on the screen.
*/
void SortByName(string names[],int weights[],int size);//function bubble sorts by name, accending order
void SortByWeight(string names[],int weights[],int size);//function selection sorts by weight, accending order
void printInfo(string names[],int weights[],int size);//function prints out both arrays with a tab inbetween
int HowManyGotOn(string names[],int weights[],int size);//function prints and returns how many people got on
int main(){
//Part I:
//Create and print out the two arrays: (Be sure to do this first)
//Anne 30 Bob 150 Ralph 305 Tim 225 Barbara 135 Jane 160 Steve 80 Tom 200 Mike 165 Shirley 90 Pam 100 Frank 120
//create array
string names[12]={"Anne","Bob","Ralph","Tim","Barbara","Jane","Steve","Tom","Mike","Shirley","Pam","Frank"};
int weights[12]={30,150,305,225,135,160,80,200,165,90,100,120};
int peopleCount[3];//holds the people count per method
string order[3]={"default", "weight", "name"};
//print out arrays
printInfo(names,weights,12);
//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.
//(Anne through Steve is equal to 1085 lbs, so no one can get on after Steve.)
peopleCount[0]=HowManyGotOn(names,weights,12);
//Part III: Rearrange these people in ascending 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.
SortByWeight(names,weights,12);
printInfo(names,weights,12);
peopleCount[1]=HowManyGotOn(names,weights,12);
//Part IV: Rearrange these people in ascending sequence by name
//(USE A DIFFERENT SORT 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.
SortByName(names,weights,12);
printInfo(names,weights,12);
peopleCount[2]=HowManyGotOn(names,weights,12);
//Part V: Have the program determine which method allowed the most people to get on the elevator.
//The program should compare the three different counts of how many people got on the elevator.
SortByWeight(order,peopleCount,3); //use SortByWeight to sort peopleCount in ascending order
cout<<" Order People loaded";
for(int k=0;k<3;k++){
cout<<endl<<order[k]<<" "<<peopleCount[k];
}
cout<<" Loading by "<<order[2]<<" loads the most people into the elevator and by"<<order[0]<<", the least.";
cin.get();//wait for an key press, stall console from autoclosing out
return 0;
}
//functions
void SortByName(string names[],int weights[],int size){//function bubble sorts by name, accending order
cout<<" Sorting by name... ";
//variables for holding values for swapping
int iTemp;
string sTemp;
int swapped = 1;//was there a swap?
for(int i=1; (i <= size) && (swapped!=0); i++){ //sort through whole array until no swap was made
swapped = 0;//set swapped to 0
for (int j=0; j < (size-1); j++){
if (names[j+1].compare(names[j])<0){ //compare name
// swap elements
sTemp = names[j];
names[j] = names[j+1];
names[j+1]=sTemp;
iTemp=weights[j];
weights[j] = weights[j+1];
weights[j+1]=iTemp;
swapped = 1;// signal that there was a swap
}
}
}
return;
}
void SortByWeight(string names[],int weights[],int size){//function selection sorts by weight, accending order
cout<<" Sorting by weight... ";
//variables for holding values for swapping
int iTemp;
string sTemp;
int index;//will hold index of the min weight
for(int i=0; i<size; i++){
index = i; //assumes is index of min weight
for(int j=i; j<size; j++){
if(weights[index]>weights[j])//found smaller value?
index= j;//change min weight index to new index
}
if(index!=i){//swap if new min was found
sTemp = names[i];
names[i] = names[index];
names[index]=sTemp;
iTemp=weights[i];
weights[i] = weights[index];
weights[index]=iTemp;
}
}
return;
}
void printInfo(string names[],int weights[],int size){//function prints out both arrays with a tab inbetween
cout<<endl<<"Name Weight(lb)";
for(int i=0;i<size;i++){
cout<<endl<<names[i]<<" "<<weights[i];
}
cout<<endl;
}
int HowManyGotOn(string names[],int weights[],int size){//function prints and returns how many people got on
int totalWeight=0;
int people=0;
int i=0;
cout<<endl<<"Loading empty elevator...";
while((weights[i]+totalWeight<=1100)&&(i<size)){
cout<<endl<<names[i]<<" gets in. "<<weights[i]<<" lb added.";//print message
//increment people count and totalweight
people++;
totalWeight+=weights[i];
i++;
}
/*
// uncomment this section for lining up and trying to enter until everyone has a turn
for(i=0;(i<size) && (totalWeight<1100);i++){
if(weights[i]+totalWeight<=1100){
cout<<endl<<names[i]<<" gets in. "<<weights[i]<<" lb added.";//print message
//increment people count and totalweight
people++;
totalWeight+=weights[i];
}
}
*/
cout<<endl<<people<<" got into the elevator with a total weight of "<<totalWeight<<" lb. ";
return people;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.