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

CSE 1311 - Project 4 Part I: Create and print out the two arrays: (Be sure to do

ID: 3690497 • Letter: C

Question

CSE 1311 - Project 4 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. You can also put the data into a file and read the information into the program. The data is as follows: 130 250 305 225 Anne Bob Ralph Tim Barbara 85 Jane Steve Tom Mike Shirley 155 Pam Frank 160 180 200 165 125 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 p Determine again how many may ride the elevator, printing out their names, weights, total weight and the number of how many people got on. the two ys

Explanation / Answer

Program:

#include<iostream>
#include<string>
using namespace std;
int printArray(string name[],int weight[],int size);
int list_of_people_in_elevator(string name[],int weight[],int size,int elv_cap);
void BubbleSort(string name[],int weight[],int size);
void SelectionSort(string name[],int weight[],int size);
main()
   {
   string name[] = {"Anne","Bob","Ralph","Tim","Barbara","Jene","Steve","Tom","Mike","Shirley","Pam","Frank"};
   int size=12,elv_cap = 1100,method1,method2,method3,weight[] = {130,250,305,225,85,160,180,200,165,155,125,120};  
   cout<<" -------------------------Method 1---------------------";  
   printArray(name,weight,size);
   method1=list_of_people_in_elevator(name,weight,size,elv_cap);

   cout<<" -------------------------Method 2---------------------";  
   BubbleSort(name,weight,size);
   printArray(name,weight,size);
   method2=list_of_people_in_elevator(name,weight,size,elv_cap);
  
   cout<<" -------------------------Method 3---------------------";  
   SelectionSort(name,weight,size);
   printArray(name,weight,size);
   method3=list_of_people_in_elevator(name,weight,size,elv_cap);
  
   if(method1 > method2 && method1 > method3)
       cout<<" Method 1 allowed most of the people in the elevator. Number of people: "<<method1;
   else if(method2 > method1 && method2 > method3)
       cout<<" Method 2 allowed most of the people in the elevator. Number of people: "<<method2;
   else if(method3 > method1 && method3 > method2)
       cout<<" Method 3 allowed most of the people in the elevator. Number of people: "<<method3;
   else if (method1==method2 && method1==method3)
       cout<<" All 3 methods has allowed same of the people in the elevator. Number of people: "<<method1;
   else
       cout<<" Unable to determine which method allowed most of the people in the elevator.";
   }

int printArray(string name[],int weight[],int size)
   {
   int i=0;
   cout<<" List of the people in Array:";
   while(i<size)
       {
       cout<<" "<<name[i++]<<" "<<weight[i];
       }
   return 0;
   }

int list_of_people_in_elevator(string name[],int weight[],int size,int elv_cap)
   {
   int i=0,total_weight=0;
   cout<<" List of people in elevator: ";
   while(i<size)
       {
       if((total_weight+weight[i])<elv_cap)
           {
           total_weight=total_weight+weight[i];
           cout<<" "<<name[i++]<<" "<<weight[i];
           }
       else
           break;
       }
   cout<<" Number of people in the elevator: "<<i;
   cout<<" Total weight: "<<total_weight;
   return i;
   }
  
void BubbleSort(string name[],int weight[],int size)
{
int i, j, flag = 1; // set flag to 1 to start first pass
int temp;
string temp1;
for(i = 1; (i <= size) && flag; i++)
{
flag = 0;
for (j=0; j < (size -1); j++)
{
if (weight[j+1] > weight[j]) // ascending order simply changes to <
{
temp = weight[j];
                   temp1 = name[j]; // swap elements
weight[j] = weight[j+1];
name[j] = name[j+1];
weight[j+1] = temp;
name[j+1]=temp1;
flag = 1; // indicates that a swap occurred.
}
}
}
return; //arrays are passed to functions by address; nothing is returned
}

void SelectionSort(string name[],int weight[],int size)
{
int i, j, first, temp;
string temp1;
for (i= size - 1; i > 0; i--)
{
first = 0; // initialize to subscript of first element
for (j=1; j<=i; j++) // locate smallest between positions 1 and i.
{
if (weight[j] > weight[first])
first = j;
}
temp = weight[first]; // Swap highest found with element in position i.
temp1 = name[first];
weight[first] = weight[i];
name[first] = name [i];
weight[i] = temp;
name[i] = temp1;
       }
return;
}

Sample output:

-------------------------Method 1---------------------

List of the people in Array:
Anne   130
Bob   250
Ralph   305
Tim   225
Barbara   85
Jene   160
Steve   180
Tom   200
Mike   165
Shirley   155
Pam   125
Frank   120

List of people in elevator:

Anne   130
Bob   250
Ralph   305
Tim   225
Barbara   85

Number of people in the elevator: 5
Total weight: 995

-------------------------Method 2---------------------

List of the people in Array:
Ralph   305
Bob   250
Tim   225
Tom   200
Steve   180
Mike   165
Jene   160
Shirley   155
Anne   130
Pam   125
Frank   120
Barbara   85

List of people in elevator:

Ralph   305
Bob   250
Tim   225
Tom   200

Number of people in the elevator: 4
Total weight: 980

-------------------------Method 3---------------------

List of the people in Array:
Barbara   85
Frank   120
Pam   125
Anne   130
Shirley   155
Jene   160
Mike   165
Steve   180
Tom   200
Tim   225
Bob   250
Ralph   305

List of people in elevator:

Barbara   85
Frank   120
Pam   125
Anne   130
Shirley   155
Jene   160
Mike   165

Number of people in the elevator: 7
Total weight: 940

Method 3 allowed most of the people in the elevator. Number of people: 7