C++ ( please help this is my second time posting) Do programming exercise top_di
ID: 3732370 • Letter: C
Question
C++ ( please help this is my second time posting)
Do programming exercise top_div_array.cpp: Winning Division app (top_div.cpp) revisited to use arrays This is the same program done last week but using and passing arrays instead of individual variables.
Start with the following file:
(code)
(code end)
Comments for the necessary work are included in the start file, but here they are in brief:
This app will use two arrays of equal length - one for sales and the other for the divisions names.
The data in these arrays will be positioned so that the divisions will share the same indexes in both arrays.
Work with the data, which is already included with the start file.
Note that there are three functions instead of two, this time.
From now on, use the convention of adding f_ in front of variables passed to a function definition, in order to be clear that it relates directly to the calling variable but lives in the function (f_). For example: populate_div_sales(div_sales, div_regions, 4); //function call void populate_div_sales(float f_div_sales[], string f_div_regions[], int size ) //function definition
Same as always, do not accept dollar amounts less than $0.00.
Do keep the commented program structure and formatting. Remember, something similar is required until the end of the semester.
Explanation / Answer
Please find my code:
#include <iostream>
#include <string>
#include <iomanip>
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 ";
for(int i=0; i<size;i++)
{
while(true)
{
cout <<i+1<<": ";
cin >> sale;
if(sale >=0)
{
f_div_sales[i]=sale;
break;
}
else
{
cout <<"Sale should be greater or equal to 0, try again.... ";
}
}
}
}
//*************************************************
//************ findHighest ************************
//*************************************************
int findHighest (float sales[], int size)
{
float greatestSalesAmount = 0;
int save_index;
greatestSalesAmount=sales[0];
for(int i=1; i<size;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] << " "<< sales[i]<< endl;
cout << "Highest sale: "<< f_sales[highest]<<" "<< f_divsions[highest ];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.