Parsing Arrays in C++An array can be defined as a data structure consisting of a group of elements. Basically, all these elements have the same data type, for example, a string or integer. Arrays are used in C + + and other programming languages to organize data or information, so that, values and v...
Homework sample to demonstrate programming fundamentals
#include "Item.h"
#include
using namespace std;
// A default constructpr
Item::Item()
: ID(0), price(0), units(0)
{
}
// Create an item
Item::Item(int ID, double price, int units)
: ID(ID), price(price), units(units)
{
}
// Initialize the ID
void Item::setID(int ID)
{
this->ID = ID;
}
// Return the ID
int Item::getID()
{
return ID;
}
// Initialize the price
void Item::setPrice(double price)
{
this->price = price;
}
// Return the price
double Item::getPrice()
{
return price;
}
// Initialize the units
void Item::setUnits(int units)
{
this->units = units;
}
// Return the units
int Item::getUnits()
{
return units;
}
// Print out the item info
void Item::displayItemInfo()
{
cout<< " Item ID: " << ID << ", ";
cout<< "Price per item: $" << price << ", ";
cout<< "Units: " << units << ", ";
cout<< "Total cost: $" <
#include
#include
#include
#include
#include "Item.h"
#include "Receipt.h"
using namespace std;
// Load the items from file
void loadItems(double pricePerID[])
{
// Load the items for sale
ifstreamitemsInFile("itemInfo.txt");
for (int i = 1; i<= 10; i++)
{
int id;
double price;
itemsInFile>> id >> price;
pricePerID[id] = price;
}
itemsInFile.close();
}
// Load the receipts from file
void loadWeeklySales(double pricePerID[], vector&receipts)
{
ifstreamsalesInFile("weeklySale.txt");
string line;
while (getline(salesInFile, line))
{
if (line == "")
break;
// Create a receipt for each shopper
stringstream ss(line);
string shopperName;
ss >>shopperName;
Receipt receipt(shopperName);
int numItems;
ss >>numItems;
// Load the items of each shopper to the receipt
for (int i = 0; i> id >> units;
receipt.addItem(Item(id, pricePerID[id], units));
}
receipts.push_back(receipt);
}
salesInFile.close();
}
// Print the total charges for each receipt
void displayTotalCharges(vector&receipts)
{
for (unsigned i = 0; i&receipts)
{
int numTimesBought[11];
double totalSalesMade[11];
// Set variables for keeping track
for (int i = 1; i<= 10; i++)
{
numTimesBought[i] = 0;
totalSalesMade[i] = 0;
}
// Scan each receipt
for (unsigned i = 0; i&receipts)
{
// Do a tally first
int numTimesBought[11];
// Set variables for keeping track
for (int i = 1; i<= 10; i++)
numTimesBought[i] = 0;
// Scan each receipt
for (unsigned i = 0; ihighestCount)
highestCount = numTimesBought[i];
// Display the most popular items
cout<< "Most popular item(s) are/is: ";
for (int i = 1; i<= 10; i++)
if (numTimesBought[i] == highestCount)
cout<&receipts)
{
cout<< "Enter the ID of the defective product(1-10): ";
int defectiveId;
cin>>defectiveId;
if (defectiveId< 1 || defectiveId> 10)
{
cout<< "Error: Invalid ID" <&receipts)
{
for (unsigned i = 0; i= 5)
// Get 1 item free
totalCost -= pricePerId[itemId];
}
cout<< " Updated Total Charge: $" < receipts;
loadWeeklySales(pricePerID, receipts);
// Start the menu
while (true)
{
cout<< "1. Display the total charges per receipts." <> option;
if (option == 1)
displayTotalCharges(receipts);
else if (option == 2)
tallyItems(receipts);
else if (option == 3)
findMostPopularItem(receipts);
else if (option == 4)
displayTotalChargesExcludeDefectiveProducts(pricePerID, receipts);
else if (option == 5)
displayTotalChargesIncludePromo(pricePerID, receipts);
else if (option == 6)
break;
cout<
Process scheduling
#include
#include
#include
#include
#include "process.h"
using namespace std;
// Entry point of the program
int main()
{
queue processes;
// Load and print all processes
ifstreaminFile("input.txt");
if (!inFile.is_open())
{
cout<< "Error: Failed to open 'input.txt' file" <>processId>>execTime)
{
cout<< "Process: " <execute();
if (process->getTime() == 0)
{
cout<< "+ Process " << process->getId() << " is completed!" <getId() << " sent back of the queue! Remaining Time: " << process->getTime() << "ms" <