I need help modifying the following c++ code to use an array of Product Objects
ID: 3668858 • Letter: I
Question
I need help modifying the following c++ code to use an array of Product Objects instead of two parrelle arrays. The product class wil need member variables to hold a propduct name and a quanititty.
Thank you
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Function prototypes
int getSalesData(string[], int[]);
int posOfLargest(int[]);
int posOfSmallest(int[]);
void displayReport(string[], int[], int);
const int SIZE = 5;
int main()
{
string name[SIZE] = { "mild ", "medium", "sweet ", "hot ", "zesty " };
int sales[SIZE]; //holds jars sold for each salsa type
int totalJarsSold = getSalesData(name, sales);
displayReport(name, sales, totalJarsSold);
system("pause");
return 0;
}
/************************************************************
* getSalesData *
* Accepts the sales figures for each slasa type and stores *
* them in the array passed to the functions, as well as *
* accutmulating and returning the totla *
************************************************************/
int getSalesData(string name[], int sales[])
{
int total = 0;
for (int type = 0; type < SIZE; type++)
{
cout << "Jars sold last month of " << name[type] << ": ";
cin >> sales[type];
while (sales[type] < 0)
{ cout << "Jars sold last must be 0 or more. Please re-enter: ";
cin >> sales[type];
}
total += sales[type];
}
return total;
}
/********************************************************
* displayReport *
* Displays the sales reprot using information from the *
* arrays passed to it. *
********************************************************/
void displayReport( string name[], int sales[], int total)
{
int hiSalesProduct = posOfLargest(sales);
int loSalesProduct = posOfSmallest(sales);
cout << " Salsa Sales Report ";
cout << "name Jars Sold ";
cout << "______________________ ";
for (int type = 0; type < SIZE; type++)
cout << name[type] << setw(11) << sales[type] << endl;
cout << " Total Sales: " << total << endl;
cout << "Hight Seller: " << name[hiSalesProduct] << endl;
cout << "Low Seller: " << name[loSalesProduct] << endl;
}
/*************************************************************
* posOfLargest *
* Finds the returns the subscript of the array position *
* holding the largest value in the array passed to the *
* Functions. *
**************************************************************/
int posOfLargest(int array[])
{
int indexOfLargest = 0;
for (int pos = 1; pos < SIZE; pos++)
{
if (array[pos]> array[indexOfLargest])
indexOfLargest = pos;
}
return indexOfLargest;
}
/************************************************************
* posOfSmallest *
* Finds and returns the subscript of the array position *
* holding the smallest value in the array passed to the *
* function *
*************************************************************/
int posOfSmallest(int array[])
{
int indexofSmallest = 0;
for (int pos = 1; pos < SIZE; pos++)
{
if (array[pos] < array[indexofSmallest])
indexofSmallest = pos;
}
return indexofSmallest;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int SIZE = 10;
class Product
{
public:
string name;
int sales; //holds jars sold for each salsa type
/************************************************************
* getSalesData *
* Accepts the sales figures for each slasa type and stores *
* them in the array passed to the functions, as well as *
* accutmulating and returning the totla *
************************************************************/
int getSalesData()
{
cout<<"Product Name : ";
cin>>name;
cout<<"Jars sold last month of "<<name<<": ";
cin>>sales;
return sales;
}
/********************************************************
* displayReport *
* Displays the sales reprot using information from the *
* arrays passed to it. *
********************************************************/
void displayReport()
{
cout << name << setw(11) <<sales<< endl;
}
};
/*************************************************************
* posOfLargest *
* Finds the returns the subscript of the array position *
* holding the largest value in the array passed to the *
* Functions. *
**************************************************************/
int posOfLargest(int sales1,int i,int sales2,int j)
{
if (sales1>sales2)
{
return i;
}
else
{
return j;
}
}
/************************************************************
* posOfSmallest *
* Finds and returns the subscript of the array position *
* holding the smallest value in the array passed to the *
* function *
*************************************************************/
int posOfSmallest(int sales1,int i,int sales2,int j)
{
if (sales1<sales2)
{
return i;
}
else
{
return j;
}
}
int main()
{
int x,i,totalJarsSold=0,count=2;
int hiSalesProduct;
int loSalesProduct;
Product obj[count];
Product ob;
for(i=1;i<=count;i++)
{
x=obj[i].getSalesData();
totalJarsSold=totalJarsSold+x;
}
cout << " Salsa Sales Report ";
cout << "name Jars Sold ";
cout << "______________________ ";
for(i=1;i<=count;i++)
{
obj[i].displayReport();
}
for(i=1;i<=count;i++)
{
for(int j=1;j<=count;j++)
{
hiSalesProduct = posOfLargest(obj[i].sales,i,obj[j].sales,j);
}
}
for(i=1;i<=count;i++)
{
for(int j=1;j<=count;j++)
{
loSalesProduct = posOfSmallest(obj[i].sales,i,obj[j].sales,j);
}
}
cout << " Total Sales: " << totalJarsSold << endl;
cout << "Hight Seller: " <<obj[hiSalesProduct].name << endl;
cout << "Low Seller: " <<obj[loSalesProduct].name << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.