Question
Doprogramming exercisetop_div_structures.cpp: 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:Use the following
Doprogramming exercisetop_div_structures.cpp: 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:Use the following struct:
struct DIV { float sale; string name; };
We use all capitals here (DIV), just to make it stand out.
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.
Both arrays will be replaced by one:
DIV div_info[4]; // array of type DIV holding the divisions' struct info
Continue modifying the entire code to accommodate the array of structure DIV
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. Remember, small steps, compile often - keep the code error free as much as possible.To turn in:
Leave the top_div_structures.cpp file and its executable in the home directory.
There is no requirement for the final output - leave the debug statements or not, but dollar amounts need decimal.
here's the LAST WEEKS (top_div_arrays.cpp)
#include
#include
#include
using namespace std;
// Function Prototypes
// These prototypes already contain the proper parameters -
// match your work accordingly
void populate_div_sales(float[], string[], int);
int findHighest (float[], int); // this function will now return the index for
// the highest division sales
void print_result(float[], string[], int); // displays result based on the index of
// highest division sales
//*************************************************
//************ main *******************************
//*************************************************
main ()
{
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";
// etc.
populate_div_sales(div_sales, div_regions, 4); // params are already given to
// help get started
// leave debug statement in final product
//cout << "debug print for array div_sales_array" << endl;
for (int i=0; i<4; i++) {
cout << div_sales[i] << endl;
}
top_div_index = findHighest(div_sales,4); //will no longer prints the result
// leave debug statement in final product
//cout << "debug for top_div_index: " << top_div_index << endl;
print_result(div_sales, div_regions,4 );
return 0;
}
//************ subroutine definitions below *******************
//*************************************************
//************ populate_div_sales *****************
//*************************************************
// The params for the function definition are given to help get started
// - note the f_ preceding variable names. Use
// this convention to help relate and contrast both the calling and the
// receiving variables.
void populate_div_sales(float f_div_sales[], string f_div_regions[], int size )
{
float sale;
cout << "Enter Division Sales for 4 regions\n";
for(int i=0; i {
while(true)
{
cout < cin >> sale;
if(sale >=0)
{
f_div_sales[i]=sale;
break;
}
else
{
cout <<"Sale should be greater or equal to 0, try again....\n";
}
}
}
}
//*************************************************
//************ findHighest ************************
//*************************************************
int findHighest (float sales[], int size)
{
float greatestSalesAmount = 0;
int save_index;
greatestSalesAmount=sales[0];
for(int i=1; i {
if(sales[i]>greatestSalesAmount)
{
greatestSalesAmount=sales[i];
save_index=i;
}
}
return save_index;
}
//*************************************************
//************ print_result ***********************
//*************************************************
void print_result(float f_sales[], string f_divsions[], int size )
{
int highest=findHighest(f_sales,4);
//cout << divsions[i] << " \t"<< sales[i]<< endl;
cout << "Highest sale: "<< f_sales[highest]<<" "<< f_divsions[highest ];
Step by Step Solution
3.34 Rating (157 Votes )
There are 3 Steps involved in it
Step: 1
include include include using namespace std struct DIV float sale str...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Document Format ( 2 attachments)
6090bd5c0c71f_21899.pdf
180 KBs PDF File
6090bd5c0c71f_21899.docx
120 KBs Word File
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started