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

Need help in C++ Project 4 - Elevator Objectives: Use single arrays to solve a p

ID: 3858461 • Letter: N

Question

Need help in C++

Project 4 - Elevator

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.

Extra Credit:

Read the data from a file and put the results on an output file. Print the output file using Notepad or some other editor. Include in the zipped folder.

Explanation / Answer

#include <iostream>

#include <fstream>

#include <vector>

#include<string>

int main2()

{

ifstream inputFile1("inputa.dat");

ifstream inputFile2("inputa.dat");

ofstream outputFile("output.dat");

int v1,v2,s,sum;

vector<int> vector1, vector2,vector3;

vector<int>::iterator it;

if (inputFile1) {

  

// read the elements in the file into a vector

while ( inputFile1 >> v1 )

{

vector1.push_back(v1);

//cout <<v1<<endl;

}

  

}

if (inputFile2) {

  

// read the elements in the file into a vector

while ( inputFile2 >> v1 )

{

vector2.push_back(v1);

}

// get size of smaller vector

if(vector1.size() < vector2.size())

s=vector1.size();

else s=vector2.size();

for(int i=0;i<s;i++)

{

// add each element and put in vector 3

sum= vector1[i]+ vector2[i];

vector3.push_back(sum);

}

// Create iterator to access values in vector3

for (it = vector3.begin(); it != vector3.end(); ++it)

{

//cout << *it<< endl ;

outputFile << *it<<endl;

}

}

}

void sort1(string names[], int w[], int n)

{

int t;

string s;

for(int i=0; i<n;i++)

{

for(int j=i+1; j<n;j++)

{

if(w[i] > w[j])

{

t=w[i];

w[i]=w[j];

w[j]=t;

s=names[i];

names[i]=names[j];

names[j]= s;

}

}

}

}

void sort2(string names[], int w[], int n)

{

int t,max=0;

string s;

for(int i=0; i<n;i++)

{

max=i;

for(int j=i+1; j<n;j++)

{

if(w[j] > w[max])

{

max=j;

}

}

t=w[i];

w[i]=w[max];

w[max]=t;

s=names[i];

names[i]=names[max];

names[max]= s;

}

}

void print(string names[], int w[], int n)

{

for(int i=0; i<n;i++)

{

cout << names[i]<<" "<<w[i]<<endl;

}

}

int getOnElevator(string names[], int w[], int n)

{

int sum=0,x=0;

int max=1100;

cout <<" Prople who can get on the elevator ";

for(int i=0; i<n;i++)

{

sum+w[i];

if(sum+w[i] <= max)

{

sum+=w[i];

cout << names[i]<<" "<<w[i]<<endl;

x++;

}

}

cout <<"Total No. "<<x<<" Total Weight: "<<sum<<endl;

return x;

}

int main()

{

int n=12;

int p=0,i=1,x=0;

string names[]={"Ane","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};

cout <<"Unordered ";

print(names,weights,n);

p=getOnElevator(names,weights,n);

cout <<" Descending order ";

sort2(names,weights,n);

print(names,weights,n);

x=getOnElevator(names,weights,n);

if(x>p)

{

p=x;

i=2;

}

cout <<" Aescending order ";

sort1(names,weights,n);

print(names,weights,n);

x=getOnElevator(names,weights,n);

if(x>p)

{

p=x;

i=3;

}

cout << "Max no of ppl got on "<<i<<" time with "<<p<<" people on elevator"<<endl;

}

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