Directions This program will do the same that Assignment 4 did (assignment 4 bel
ID: 3716440 • Letter: D
Question
Directions
This program will do the same that Assignment 4 did (assignment 4 below), except this time the number of stores will be a number n from 1 to 10 (inclusively). The main method should obtain this number n from the user. The program will not proceed until a proper number n is given. Once the number n is obtained, the main method will initialize dynamic array of n structures. The structures will be defined in the program to contain 5 numeric attributes for each store. These will be integer and double number. We will not use string since they may complicate your programs.
After declaration, creation of initialization of this dynamic array, the main method will call the following functions:
1. printable to print all data in the arrays properly labeled and aligned.
2. sortData to sort array.
3. printable again to print all sorted data.
These functions above are described as follows:
printTable function
This function will receive the dynamic array defined in main and print a table containing the information contained within the array. Every row in the table will contain the information for the same store (n rows) and every column will contain the information of the same attribute for all stores (5 columns with number attributes). Print appropriate headers for the table and each column. Print an index number (from 1 to n+1) in front of each row. All columns should be aligned.
sortData function
This function will receive the dynamic array defined in main and sort it based on one of your integer attributes from lower to higher value. Remember that this time you are only sorting one array, but is an array of structures with multiple data per store.
Do not forget to add comments to the code. The .cpp file must have a head of comments indicating the name of the file, the name of the author, and a brief description of the program. Each function must have also have a head of comments indicating the name of the function and a brief description of what it does. All variable names must also be documented.
Assignment 4
#include <iostream>
#include <string>
using namespace std;
// prints the given arrays in tabular form along with the indices
// First, the header is printed then each of the parallel
// arrays elements is printed on the same row.
// Each column contains the data of the same array
void printTable(string[], float[], float[], int[], int[], int[]);
// Sorts all the parallel arrays based on the number of brancehes
// each of the arrays values are transformed to reflect the
// store with the minimum branches at the first location
void sortData(string[], float[], float[], int[], int[], int[]);
int main()
{
// create 6 parallel arrays representing stores
string store_name[] = {
"Sammy's HW", "Super Mart", "Roadside HW",
"Peter's HW", "Carl's HW", "Eric's HW",
"House Mart", "Lowe's HW", "Home Depot",
"True Value" };
float daily_sales[] = {
50780.65, 63000.56, 49560.45, 35235.85, 108658.48, 125895.75,
204859.51, 150324.25, 225785.36, 118965.42 };
float average_daily_customers[] = {
95.56, 256.5, 210.48, 250.5, 1100.36, 1325.53,
2080.8, 2155.56, 2525.6, 1835.8 };
int days_open[] = {
5, 6, 7, 7, 7, 5, 5, 6, 6, 6 };
int number_of_employees[] = {
5, 10, 15, 20, 20, 16, 28, 43, 55, 33 };
int number_of_branches[] = {
1, 3, 10, 800, 600, 15, 2, 500, 3000, 1500 };
// Display the original data
cout << "Unsorted data:" << endl;
printTable(store_name, daily_sales, average_daily_customers,
days_open, number_of_employees, number_of_branches);
// call sortData to sort the arrays based on number of branches
sortData(store_name, daily_sales, average_daily_customers,
days_open, number_of_employees, number_of_branches);
// display the sorted data
cout << " Data after sorting based on the number of branches:" << endl;
printTable(store_name, daily_sales, average_daily_customers,
days_open, number_of_employees, number_of_branches);
return 0;
}
// prints the given arrays in tabular form along with the indices
// First, the header is printed then each of the parallel
// arrays elements is printed on the same row.
// Each column contains the data of the same array
void printTable(string store_name[], float daily_sales[],
float avr_daily_cust[], int days_open[], int num_emp[],
int num_branches[])
{
// display the header of the table
cout << " Store name Daily Sales Average Customers "
"Days Open Number Of Employees Number of Branches" << endl;
// display elements of each array
for (int i = 0; i < 10; i++)
{
cout << i + 1 << " " << store_name[i] << " " << daily_sales[i] << " "
<< avr_daily_cust[i] << " " << days_open[i] << " "
<< num_emp[i] << " " << num_branches[i] << endl;
}
}
// Sorts all the parallel arrays based on the number of brancehes
// each of the arrays values are transformed to reflect the
// store with the minimum branches at the first location
void sortData(string store_name[], float daily_sales[],
float avr_daily_cust[], int days_open[], int num_emp[],
int num_branches[])
{
for (int i = 0; i < 9; i++)
{
int minIndex = i;
for (int j = i + 1; j < 10; j++)
{
// find the index with minimum branches
if (num_branches[j] < num_branches[minIndex])
{
minIndex = j;
}
}
// swap the first element in all arrays with the element
// at the minIndex
string temp = store_name[minIndex];
store_name[minIndex] = store_name[i];
store_name[i] = temp;
float tempF = daily_sales[minIndex];
daily_sales[minIndex] = daily_sales[i];
daily_sales[i] = tempF;
tempF = avr_daily_cust[minIndex];
avr_daily_cust[minIndex] = avr_daily_cust[i];
avr_daily_cust[i] = tempF;
int tempI = days_open[minIndex];
days_open[minIndex] = days_open[i];
days_open[i] = tempI;
tempI = num_emp[minIndex];
num_emp[minIndex] = num_emp[i];
num_emp[i] = tempI;
tempI = num_branches[minIndex];
num_branches[minIndex] = num_branches[i];
num_branches[i] = tempI;
}
system("pause");
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
// prints the given arrays in tabular form along with the indices
// First, the header is printed then each of the parallel
// arrays elements is printed on the same row.
// Each column contains the data of the same array
void printTable(string[], float[], float[], int[], int[], int[]);
// Sorts all the parallel arrays based on the number of brancehes
// each of the arrays values are transformed to reflect the
// store with the minimum branches at the first location
void sortData(string[], float[], float[], int[], int[], int[]);
int main()
{
// create 6 parallel arrays representing stores
string store_name[] = {
"Sammy's HW", "Super Mart", "Roadside HW",
"Peter's HW", "Carl's HW", "Eric's HW",
"House Mart", "Lowe's HW", "Home Depot",
"True Value" };
float daily_sales[] = {
50780.65, 63000.56, 49560.45, 35235.85, 108658.48, 125895.75,
204859.51, 150324.25, 225785.36, 118965.42 };
float average_daily_customers[] = {
95.56, 256.5, 210.48, 250.5, 1100.36, 1325.53,
2080.8, 2155.56, 2525.6, 1835.8 };
int days_open[] = {
5, 6, 7, 7, 7, 5, 5, 6, 6, 6 };
int number_of_employees[] = {
5, 10, 15, 20, 20, 16, 28, 43, 55, 33 };
int number_of_branches[] = {
1, 3, 10, 800, 600, 15, 2, 500, 3000, 1500 };
// Display the original data
cout << "Unsorted data:" << endl;
printTable(store_name, daily_sales, average_daily_customers,
days_open, number_of_employees, number_of_branches);
// call sortData to sort the arrays based on number of branches
sortData(store_name, daily_sales, average_daily_customers,
days_open, number_of_employees, number_of_branches);
// display the sorted data
cout << " Data after sorting based on the number of branches:" << endl;
printTable(store_name, daily_sales, average_daily_customers,
days_open, number_of_employees, number_of_branches);
return 0;
}
// prints the given arrays in tabular form along with the indices
// First, the header is printed then each of the parallel
// arrays elements is printed on the same row.
// Each column contains the data of the same array
void printTable(string store_name[], float daily_sales[],
float avr_daily_cust[], int days_open[], int num_emp[],
int num_branches[])
{
// display the header of the table
cout << " Store name Daily Sales Average Customers "
"Days Open Number Of Employees Number of Branches" << endl;
// display elements of each array
for (int i = 0; i < 10; i++)
{
cout << i + 1 << " " << store_name[i] << " " << daily_sales[i] << " "
<< avr_daily_cust[i] << " " << days_open[i] << " "
<< num_emp[i] << " " << num_branches[i] << endl;
}
}
// Sorts all the parallel arrays based on the number of brancehes
// each of the arrays values are transformed to reflect the
// store with the minimum branches at the first location
void sortData(string store_name[], float daily_sales[],
float avr_daily_cust[], int days_open[], int num_emp[],
int num_branches[])
{
for (int i = 0; i < 9; i++)
{
int minIndex = i;
for (int j = i + 1; j < 10; j++)
{
// find the index with minimum branches
if (num_branches[j] < num_branches[minIndex])
{
minIndex = j;
}
}
// swap the first element in all arrays with the element
// at the minIndex
string temp = store_name[minIndex];
store_name[minIndex] = store_name[i];
store_name[i] = temp;
float tempF = daily_sales[minIndex];
daily_sales[minIndex] = daily_sales[i];
daily_sales[i] = tempF;
tempF = avr_daily_cust[minIndex];
avr_daily_cust[minIndex] = avr_daily_cust[i];
avr_daily_cust[i] = tempF;
int tempI = days_open[minIndex];
days_open[minIndex] = days_open[i];
days_open[i] = tempI;
tempI = num_emp[minIndex];
num_emp[minIndex] = num_emp[i];
num_emp[i] = tempI;
tempI = num_branches[minIndex];
num_branches[minIndex] = num_branches[i];
num_branches[i] = tempI;
}
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.