Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Elevator in C++ Objectives: Use single arrays to solve a problem. Use searching

ID: 3803947 • Letter: E

Question

Elevator in C++

Objectives:

Use single arrays to solve a problem.

Use searching and sorting techniques on a single array.

Format output properly, according to the directions.

Use functions in a program to be able to reuse code.

Instructions:

Part I:

Create and print out the two arrays: (Be sure to do this first) You are allowed to hard code these arrays into your program. The data is as follows:

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.

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 DIFFERENT SORT ALGORITHM THAN THE ONE YOU USED IN PART III) 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) a print method which prints both arrays (include a size parameter and it can be called 6 times). Make sure all methods are writen to handle n elements, not just 12. You can pass 12 from main to n in the method/function.

Run:

You only have to run this program once since the data is embedded in the program. Please include your source code, followed by all of your output.

Explanation / Answer

Here is the code with comments for the question. Output is also shown. Please dont forget to rate the anwer if it helped. Thank you very much.

elevator.cpp

#include <iostream>
using namespace std;


int SIZE=12; //defining the size of the arrays, need to change if array size changes

//function to display first n elements from names and weights array
void display(string names[],int weights[],int n)
{
   //loop for n times and display their names and weights
   for(int i=0;i<n;i++)
       cout<<(i+1)<<". "<<names[i]<<" "<<weights[i]<<endl;
   cout<<"_________________________"<<endl;
}
//returns the number of people who can ride on based on the order of elements
int calculate_max(string names[],int weights[],int size,int max_weight)
{
   int total=0,lastperson;
   for(int i=0;i<size;i++)
   {
       //if next weight together with previous total is still less than max_weight, we can consider this person also
       if(weights[i]+total<=max_weight)
       {
           total+=weights[i];
           lastperson=i;
       }
       else
           break; // stop
      
   }
  
   cout<<(lastperson+1)<<" people with a total weight of "<<total<<" can ride on a elevator with max weight of "<<max_weight<<endl;
   return lastperson+1;
}

//sorts the arrays names and weights in increasing order of weights
void bubblesort_asc(string names[],int weight[],int n)
{
   int tempw;
   string tempn;
   for(int i=0;i<n;i++)
       for(int j=i;j<n;j++)
       {
           if(weight[j]<weight[i])
           {
               //swap both the weights and names to keep them related
               tempw=weight[i];
               weight[i]=weight[j];
               weight[j]=tempw;              
              
               tempn=names[i];
               names[i]=names[j];
               names[j]=tempn;
           }
       }
}
  

//merge the 2 sub arrays from start to mid and mid+1 to end
void merge(string names[],int weights[],int start, int mid, int end) {
int idx1,idx2, i;
   int arrw[SIZE];     //array to hold merged names      
   string arrn[SIZE]; //array to hold merged weights
for(idx1 = start,idx2 = mid + 1, i = start; idx1 <= mid && idx2 <= end; i++)
{
   if(weights[idx1] >= weights[idx2])
       {
           arrw[i] = weights[idx1];
           arrn[i]=names[idx1];          
           idx1++;
       }
   else
       {
    arrw[i] = weights[idx2];
           arrn[i]=names[idx2];
           idx2++;
          
       }
}

    while(idx1 <= mid)
   {
   arrw[i]=weights[idx1];
       arrn[i]=names[idx1];
       i++;
       idx1++;
   }
   while(idx2 <= end)
   {
   arrw[i]=weights[idx2];
       arrn[i]=names[idx2];
       i++;
       idx2++;
   }
  
for(i = start; i <= end; i++)
   {
   names[i]=arrn[i];
       weights[i]=arrw[i];
   }
}

void mergesort_desc(string names[],int weights[],int start, int end) {
int mid;

if(start < end)
{
mid = (start + end) / 2;
mergesort_desc(names,weights,start, mid);
mergesort_desc(names,weights,mid+1, end);
merge(names,weights,start, mid, end);
}   
}
int main()
{
   string names[]={"Anne","Bob","Ralph","Tim","Barbara","Jane","Steve","Tom","Mike","Shirley","Pam","Frank"};
   int weights[]={ 30 , 150 , 305, 225, 135, 160 , 80 , 200, 165, 90, 100, 120};
   int c1,c2,c3,max;
  
   //print out the original order and the number of people who ride on in that order
   cout<<"The orginal order of people is"<<endl;
   display(names,weights,12);
   c1=calculate_max(names,weights,12,1100);
   display(names,weights,c1);
  
   bubblesort_asc(names,weights,12);//sort in ascending order using bubblesort
   //print out the sorted order and the number of people who ride on in that order
   cout<<"The sorted ascending order of the people is "<<endl;
   display(names,weights,12);
   c2=calculate_max(names,weights,12,1100);
   display(names,weights,c2);
  
   mergesort_desc(names,weights,0,11); //sort in descending order using mergesort
   //print out the sorted order and the number of people who ride on in that order
   cout<<"The sorted descending order of the people is "<<endl;
   display(names,weights,12);
   c3=calculate_max(names,weights,12,1100);
   display(names,weights,c3);
  
  
   //get the maximum of c1 , c2 and c3
   if(c1>=c2 && c1>=c3)
       max=c1;
   else if(c2>=c1 && c2>=c3)
       max=c2;
   else if(c3>=c1 && c3>=c2)
       max=c3;
      
   cout<<"The maximum number of people that can ride on is "<<max<<endl;
}

output

The orginal order of people is
1. Anne 30
2. Bob 150
3. Ralph 305
4. Tim 225
5. Barbara 135
6. Jane 160
7. Steve 80
8. Tom 200
9. Mike 165
10. Shirley 90
11. Pam 100
12. Frank 120
_________________________
7 people with a total weight of 1085 can ride on a elevator with max weight of 1100
1. Anne 30
2. Bob 150
3. Ralph 305
4. Tim 225
5. Barbara 135
6. Jane 160
7. Steve 80
_________________________
The sorted ascending order of the people is
1. Anne 30
2. Steve 80
3. Shirley 90
4. Pam 100
5. Frank 120
6. Barbara 135
7. Bob 150
8. Jane 160
9. Mike 165
10. Tom 200
11. Tim 225
12. Ralph 305
_________________________
9 people with a total weight of 1030 can ride on a elevator with max weight of 1100
1. Anne 30
2. Steve 80
3. Shirley 90
4. Pam 100
5. Frank 120
6. Barbara 135
7. Bob 150
8. Jane 160
9. Mike 165
_________________________
The sorted descending order of the people is
1. Ralph 305
2. Tim 225
3. Tom 200
4. Mike 165
5. Jane 160
6. Bob 150
7. Barbara 135
8. Frank 120
9. Pam 100
10. Shirley 90
11. Steve 80
12. Anne 30
_________________________
5 people with a total weight of 1055 can ride on a elevator with max weight of 1100
1. Ralph 305
2. Tim 225
3. Tom 200
4. Mike 165
5. Jane 160
_________________________
The maximum number of people that can ride on is 9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote