So I need help with coding this program. If you can help me that would be great!
ID: 3767433 • Letter: S
Question
So I need help with coding this program. If you can help me that would be great!
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 130
Bob 150
Ralph 305
Tim 225
Barbara 135
Jane 160
Steve 85
Tom 200
Mike 165
Shirley 90
Pam 125
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 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 descending 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.
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.
Explanation / Answer
Complete Program:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void printArrays(string names[], int weights[], int n);
void sortByNames(string names[], int weights[], int n);
void sortByWeights(string names[], int weights[], int n);
int howMany(int weights[], int n, int loadLimit);
int main()
{
const int SIZE = 12;
string names[SIZE] = {"Anne", "Bob", "Ralph", "Tim", "Barbara", "Jane", "Steve", "Tom", "Mike", "Shirley", "Pam", "Frank"};
int weights[SIZE] = {130, 150, 305, 225, 135, 160, 85, 200, 165, 90, 125, 120};
int count = 0;
cout << "Arrays (inititally):" << endl;
printArrays(names, weights, SIZE);
cout << "The elevators in our building have an 1100 lb. load limit:" << endl;
count = howMany(weights, SIZE, 1100);
printArrays(names, weights, count);
sortByNames(names, weights, SIZE);
cout << "Arrays in ascending order by names:" << endl;
printArrays(names, weights, SIZE);
cout << "The elevators in our building have an 1100 lb. load limit:" << endl;
count = howMany(weights, SIZE, 1100);
printArrays(names, weights, count);
sortByWeights(names, weights, SIZE);
cout << "Arrays in descending order by weights:" << endl;
printArrays(names, weights, SIZE);
cout << "The elevators in our building have an 1100 lb. load limit:" << endl;
count = howMany(weights, SIZE, 1100);
printArrays(names, weights, count);
system("pause");
return 0;
}
void printArrays(string names[], int weights[], int n)
{
int totalWeight = 0;
cout << left << setw(12) << "Name" << right << setw(6) << "Weight" << endl;
cout << "-------------------" << endl;
for(int i = 0; i < n; i++)
{
totalWeight = totalWeight + weights[i];
cout << left << setw(12) << names[i] << right << setw(6) << weights[i] << endl;
}
cout << " Total weight: " << totalWeight << endl;
cout << "Number of people: " << n << endl << endl;
}
void sortByNames(string names[], int weights[], int n)
{
for(int i = 1; i < n; i++)
{
string temeName = names[i];
int tempWeight = weights[i];
int j = i;
while(j > 0 && strcmp(temeName.c_str(), names[j - 1].c_str()) > 0)
{
names[j] = names[j - 1];
weights[j] = weights[j - 1];
j--;
}
names[j] = temeName;
weights[j] = tempWeight;
}
}
void sortByWeights(string names[], int weights[], int n)
{
for(int i = 0; i < n - 1; i++)
{
int minPos = i;
for(int j = i + 1; j < n; j++)
{
if(weights[j] < weights[minPos])
{
minPos = j;
}
}
if(minPos != i)
{
int tempWeight = weights[minPos];
weights[minPos] = weights[i];
weights[i] = tempWeight;
string temeName = names[minPos];
names[minPos] = names[i];
names[i] = temeName;
}
}
}
int howMany(int weights[], int n, int loadLimit)
{
int totalWeight = 0;
for(int i = 0; i < n; i++)
{
if(totalWeight >= loadLimit)
return i;
totalWeight = totalWeight + weights[i];
}
return 0;
}
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.