Question

Need to revise the following code to use an array of product objects instead of two...

Need to revise the following code to use an array of product objects instead of two parallel arrays. The product class will need member variables to hold a product name and a quantity.

#include<iostream>

#include<string>

using namespace std;

int main()

{

//Declare variables

const int salsaTypes = 5;

const int jarsSold = 5;

string salsa[salsaTypes] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };

int jars[jarsSold];

int totalSales, highestSales, lowestSales;

string highestSoldProduct;

string lowesetSoldProduct;

//Repeat loop for all salsas

for (int i = 0; i < salsaTypes; i++)

{

//Ask user for the number of jars sold

cout << "Enter the number of jars sold for: " << salsa[i] << ": ";

cin >> jars[i];

}

//Initalize variables

totalSales = 0;

highestSales = jars[0];

highestSoldProduct = salsa[0];

lowestSales = jars[0];

lowesetSoldProduct = salsa[0];

//Repeat loop for all types of salsas

for (int i = 0; i < salsaTypes; i++)

{

//Calculate the total sales of the jars

totalSales += jars[i];

//Find the highest sales and product

if (highestSales < jars[i])

{

highestSales = jars[i];

highestSoldProduct = salsa[i];

}

//Find the lowest sales and product

if (lowestSales > jars[i])

{

lowestSales = jars[i];

lowesetSoldProduct = salsa[i];

}

}

//Display the details

cout << "Sales for each salsa type:" << endl;

for (int i = 0; i < salsaTypes; i++)

{

cout << "The number of jars sold for the salsa type:" << salsa[i] << " is " << jars[i] << endl;

}

//Display the total number of jars sold, highest amount and product, and lowest amount and product

cout << "The total number of jars sold in the past month:" << totalSales << endl;

cout << "The highest number of jars sold:" << highestSales << endl;

cout << "The higest selling product is:" << highestSoldProduct << endl;

cout << "The lowest number of jars sold:" << lowestSales << endl;

cout << "The lowest selling product is:" << lowesetSoldProduct << endl;

system("pause");

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CPP code:

#include<iostream>
#include<string>
using namespace std;

class Product
{
public:
string productName;
int quantity;
};

int main()
{
Product array[5];
array[0].productName="Mild";
array[1].productName="Medium";
array[2].productName="Sweet";
array[3].productName="Hot";
array[4].productName="Zesty";

const int salsaTypes = 5;
const int jarsSold = 5;

int totalSales, highestSales, lowestSales;
string highestSoldProduct;
string lowesetSoldProduct;

//Repeat loop for all salsas
for (int i = 0; i < salsaTypes; i++)
{
//Ask user for the number of jars sold
cout << "Enter the number of jars sold for: " << array[i].productName << ": ";
cin >> array[i].quantity;
}

//Initalize variables
totalSales = 0;
highestSales = array[0].quantity;
highestSoldProduct = array[0].productName;
lowestSales = array[0].quantity;
lowesetSoldProduct = array[0].productName;

//Repeat loop for all types of salsas
for (int i = 0; i < salsaTypes; i++)
{
//Calculate the total sales of the jars
totalSales += array[i].quantity;
//Find the highest sales and product
if (highestSales <array[i].quantity)
{
highestSales = array[i].quantity;
highestSoldProduct = array[i].productName;
}
//Find the lowest sales and product
if (lowestSales > array[i].quantity)
{
lowestSales = array[i].quantity;
lowesetSoldProduct = array[i].productName;
}
}
//Display the details
cout << "Sales for each salsa type:" << endl;

for (int i = 0; i < salsaTypes; i++)
{
cout << "The number of jars sold for the salsa type:" << array[i].productName << " is " << array[i].quantity << endl;
}

//Display the total number of jars sold, highest amount and product, and lowest amount and product
cout << "The total number of jars sold in the past month:" << totalSales << endl;
cout << "The highest number of jars sold:" << highestSales << endl;
cout << "The higest selling product is:" << highestSoldProduct << endl;
cout << "The lowest number of jars sold:" << lowestSales << endl;
cout << "The lowest selling product is:" << lowesetSoldProduct << endl;

return 0;
}

Screenshot of the code for better understanding(with comments):

SCreenshot of the code's output:

NOTE : I made the minimal changes possible so that my code doesn't have a significant deviation from yours.Hence, I have avoided the usage of private variables and member functions for those.

PLEASE UPVOTE IF THE ANSWER IS HELPFUL

Add a comment
Know the answer?
Add Answer to:
Need to revise the following code to use an array of product objects instead of two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program 2 <100 points>: Chips and Salsa (chipsSalsa.cpp) Write a program that lets a maker of...

    Program 2 <100 points>: Chips and Salsa (chipsSalsa.cpp) Write a program that lets a maker of chips and salsa keep track of sales for five different types of salsa: mild, medium, sweet, hot, and zesty. The program should use two parallel 5-element arrays: an array of strings that holds the five salsa names and an array of integers that holds the number of jars sold during the past month for each salsa type. The salsa names should be stored using...

  • Write a C++ program that lets a maker of chips and salsa keep track of their...

    Write a C++ program that lets a maker of chips and salsa keep track of their sales for five different types of salsa they produce: mild, medium, sweet, hot, and zesty. It should use two parallel five-element arrays: an array of strings that holds the five salsa names and an array of integers that holds the number of jars sold during the past month for each salsa type. The salsa names should be stored using an initialization list at the...

  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • Need help with a C++ program. I have been getting the error "this function or variable...

    Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) {    // average function , declaring variable    int i;    char str[40];    float avg = 0;    // iterating in...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Hello, I have written a code that I now need to make changes so that arrays...

    Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...

  • Modify PartTwo.cpp so that it is uses a vector, named vect, instead of an array. PartTwo.cpp...

    Modify PartTwo.cpp so that it is uses a vector, named vect, instead of an array. PartTwo.cpp Is posted here: // This program reads data from a file into an array. Then, it // asks the user for a number. It then compares the user number to // each element in the array, displays the array element if it is larger. // After looking at entire array, it displays the number of elements in the array larger // than the user...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to...

    This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT