Question: I'm very lost with how to do this assignment: Modify last week's program (top_div_arrays.cpp) one more time, by converting both related arrays to one array
I'm very lost with how to do this assignment:
Modify last week's program (top_div_arrays.cpp) one more time, by converting both related arrays to one array of structures.
Include the following:
a. Use the following struct:
struct DIV { float sale; string name; };
b. The first function's parameters will change from two arrays to one like this:
from: void populate_div_sales(float[], string[], int); to: void populate_div_sales (DIV [], int);
Then other function will need similar revisions.
c. Both arrays will be replaced by one:
DIV div_info[4]; // array of type DIV holding the divisions' struct info
d. Continue modifying the entire code to accomodate the array of structure DIV
e. Now that you have experience with prototypes, know that prototypes really do NOT need variable names.
Remember that the prototypes in the start file for top_div_array.cpp looked as follows:
void populate_div_sales(float[], string[], int); int findHighest (float[], int); void print_result(float[], string[], int);
No variable names.
This is the way prototypes need to look like, from this point.
Adding variable names to prototypes will cost 1 point in this assignment.
f. It is recommended to modify this code one section at a time, while commenting the rest.
For example, one could go as far as only making the hard coding of the division names into div_info[] work and do a debug print like this:
//keep debug printout cout << "debug print for array div_info" << endl; for (int i=0; i<4; i++) { cout << div_info[i].name << endl; }
From that point, uncomment the part of the code dealing with division sales input and debug-print it.
Here is the code to edit:
top_div_array.cpp
// // Description: // This program uses 2 functions to calculate which // division in a company has the highest quarterly sales.
#include < iostream> #include < string> #include < iomanip> using namespace std;
//############ function prototypes #############
void populate_div_sales(float[], string[], int); int findHighest (float[], int); // this function will now return the index for the // the highest division sales void print_result(float[], string[], int); // displays result based on the index // of highest division sales
//############ main #############
int main () { //############ declarations #############
int top_div_index = 0; // will be assigned the index of the top division sales float div_sales[4]; // array holding the divisions' sales amounts string div_regions[4]; // array holding division names
// populate div_regions array div_regions[0] = "Northeast"; div_regions[1] = "Southeast"; div_regions[2] = "Northwest"; div_regions[3] = "Southwest";
//############ input #############
populate_div_sales(div_sales, div_regions, 4);
cout << "debug print for array div_sales_array" << endl; for (int i=0; i<4; i++) { cout << div_sales[i] << endl; }
//############ processing #############
top_div_index = findHighest(div_sales, 4); //will no longer prints the result
cout << "debug for top_div_index: " << top_div_index << endl;
print_result(div_sales, div_regions, top_div_index);
return 0; }
//############ function definitions #############
//######################################### //############ populate_div_sales ############## //#########################################
void populate_div_sales(float f_div_sales[], string f_div_regions[], int size ) { for (int i=0; i < size; i++) { float quarterlySales = -1;
while (quarterlySales < 0) { cout << "What was the quarterly sales for the " << f_div_regions[i] << " division? $"; cin >> quarterlySales;
if (quarterlySales < 0) cout << " Invalid amount! The number must be positive."; } f_div_sales[i] = quarterlySales; } }
//################################### //############ findHighest ############# //###################################
int findHighest (float f_div_sales[], int size) { float greatestSalesAmount = 0; int save_index;
for (int i=0; i <= size; i++) { if (f_div_sales[i] > greatestSalesAmount) { greatestSalesAmount = f_div_sales[i]; save_index = i; }
//cout << " The " << greatestDivision << " had the highest quarterly sales "; //cout << " with a total of $"; //cout << setprecision(2) << fixed; //cout << greatestSalesAmount << ". "; } return save_index; }
//################################## //############ print_result ############# //##################################
void print_result(float f_div_sales[], string f_div_regions[], int f_top_div_index) { cout << " The " << f_div_regions[f_top_div_index] << " had the highest quarterly sales "; cout << " with a total of $"; cout << setprecision(2) << fixed; cout << f_div_sales[f_top_div_index] << ". "; }
Can someone show me how each step should be done? I have no idea where to start.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
