Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

CSci 1113: Introduction to C/C++ Programming for Scientists and Engineers Homework 11 Introduction Due Date: Friday, April 23, 2021 before 11:55pm. Submission: Your final submission for each part of...

1 answer below »
CSci 1113: Introduction to C/C++
Programming for Scientists and Engineers
Homework 11
Introduction
Due Date: Friday, April 23, 2021 before 11:55pm.
Submission: Your final submission for each part of the homework will be a single file: hw11A.cpp
for part A and hw11B.cpp for part B. Each will be submitted on a different link on gradescope.
Grading: Grading for this homework will be fully automatic through the tests on gradescope.
Part A and Part B both have provided code with a section that cannot be modified. So the tests
will first check that the file is not modified, and only if the main functions are unmodified will
the tests run as normal. Some test results will not be shown to you in advance, and will only be
evealed after the deadline. All tests, however, will only test the behavior that is described in this
document - So if you solve the problem described here you should be fine.
Instructions: This is an individual homework assignment. There are two problems, each worth
20 points. Solve the problem below by yourself (unlike the labs, where you work collaboratively),
and submit the solution as a C++ source code file.
Some Rules/guidelines:
1. This is an individual assignment Unlike the lab assignments, this is not collaborative.
You must design, implement, and test the solution to each problem on your own without
substantial assistance from anyone other than the course instructor or TAs.
2. You may not include code, solutions, or portions of solutions from any external
source. You are only allowed to use sources that are part of class: examples from the
textbook, lectures, or code you and your partner write to solve lab problems. Using code
obtained in other ways, or letting others view or use your code is considered academic
misconduct. For more information, see the collaboration rules file on the class website, o
ask the instructor or TAs if you have any questions on what is and is not allowed.
3. Because this homework is submitted and tested electronically, and automatically, the fol-
lowing are more important than in a typical class. Failure to follow these instructions can
esult in a grade of 0.
• Follow the naming conventions mentioned at the end of the problems.
• Files must be submitted before the deadline or they will not be accepted.
• Files must be submitted to gradescope. Emailing the course staff will generally not
count as a submission. (although this is an OK way to request assistance if gradescope
is refusing to accept your files)
• The input and output formats listed in the problem description, and demonstrated in
the examples, are not suggestions. Follow the provided input and output format exactly,
and to the letter. (Remember that spaces and end-of-lines count as letters too!)
1
• Regardless of how or where you develop your solutions, we expect your programs to
compile and execute on gradescope. It is not enough for them to run on your computer.
Course staff can help you test in more detail if you have e
ors on gradescope, but no
e
ors on your computer.
• The problem descriptions will usually show at least one test case and the resulting
co
ect output. However, you should test your program on other test cases (that you
make up) as well. Making up good test cases is a valuable programming skill, and is
part of ensuring your code solution is co
ect.
2
Problem A: Grocery shopping (20 points)
In this program you will be building a GroceryList class that simulates checking out at a grocery
store. Your class will support adding items to the grocery list (think of scanning an item at the
checkout), as well as a function that will get a receipt (an a
ay of strings listing each distinct item
that was checked out, and how many times that specific item was added.)
Start with the checkout.cpp file provided on canvas. Without changing the provided main function,
write a c++ class that works with the existing main() function that simulates checking out at a
grocery store. You will want to carefully consider what functions are needed for the provided main
class to work. Ensure that you class makes the provided main() output match the examples ex-
actly. The add() function should add a checkout item to the items being bought, either increasing
the amount by one if the item is already present or adding it to the list if it is new. You may
directly compare strings when adding, thus if the string is different at all it can be treated as a
new item. The checkout() function should return a list of all items and their quantities, along
with a final additional line saying how many items were bought. Note that the item bought is the
sum of the quantities, not the amount of different types of items on the list. The order that you
display the items is not important (but the order the examples use is probably the easiest to do).
Some assumptions you can make to simplify this problem:
1. You can assume fewer than 200 items will be checked out.
2. You shouldn’t need to implement a destructor, copy constructor, or assignment operator fo
this assignment. (one or more non-dynamic a
ays will be sufficient)
Hints
• There are several ways to make the GroceryList class work.
• You do not need to limit yourself to only the functions, methods, and classes required to
make main work. Writing some helper functions, or even a helper class, can make this
problem easier to solve.
• An easy way to make a string piece by piece in C++ (such as the receipt string, which
contains multiple pieces of information) is to use the overloaded + or += string operators.
These operators, with strings, allow you to combine strings. To add a number into a string
you will first need to use the to_string function to convert that number into a string. (This
function should exist for any reasonably up-to-date C++ version) Example:
string x = "";
x += to_string (2);
x += "x Apples";
cout
x;
shows "2x Apples"
3
Examples
Example 1
apple
anana
apple
!checkout
Receipt:
2x apple
1x banana
---------
Total items: 3
Example 2
apple
anana
Apple
Checkout
!checkout
Receipt:
1x apple
1x banana
1x Apple
1x Checkout
-----------
Total items: 4
4
Example 3
pasta
milk
pasta
milk
ice
milk
ice
milk
!checkout
Receipt:
2x pasta
4x milk
2x rice
--------
Total items: 8
Submission
When you are done, name the source code file hw11A.cpp. Then log into gradescope and upload
your file for the “Homework 10A” submission. If you name your file inco
ectly it will be
unable to compile and run your code, so you will fail all test cases. You may submit cpp
files as many times as you want until the deadline to try and fix the code if you fail a test case.
Following rigorous naming conventions and using test cases are something computer programmers
often must do in “real life” programming, and so submitting your program with the co
ect name
and functionality is part of doing this assignment co
ectly.
5
Problem B: Spring Break Streaming
In this program we will build a simplified item crafting processor. This might be helpful in games
like Astroneer or Satisfactory where basic items can be combined to make more advanced items
(which themselves may be used to make further, more advanced items in a complicated technology
tree) Your program’s interaction with the user is simple, first it should prompt the user for a file,
then it should read the file itself, then it should output the price of each item. The real work,
however, is in reading the file. The file your program will read will contain three commands:
• Item: – This command is used to add a new item to a list. It will provide both the item
name and the price of this item.
• Recipe: – This command defines an item that is built from other basic items. The price of
this item will be the sum of the individual components to make the item. It will be a list of
names of other items (already defined) that it is made from.
• Change: – This will change price of an already defined item using the Item: command. It will
not be used to redefine recipes. This will provide both the name of the item being redefined
and the new price.
After all the items have been entered, For each “Item” and “Recipe” show the price to get this
item. The price changes from the “Change” command should be reflected on both the simple
“Item” and in the “Recipes” that use the item. In other words, the change in the price should
effect all products.
I recommend at this point downloading the provided example files from canvas and reading ove
them, make sure you understand the format and are able to perform this computation on your own.
(It’s not a te
ibly complicated computation for small files, but you will want a more organized
computation to allow you to perform this computation for big files)
Some assumptions you can make to simplify this problem:
1. recipes will only be used from simple “Items” (instead of more complex “Recipes”.
2. Item names will be single words.
3. One command per line.
4. Commands will be for mated as in the file (space after command and no trailing space).
5. “Recipes” will only use “Items” that have already been defined.
6. “change” will only modify “Items” that have already been defined.
7. Recipes will not be more than 50 items.
8. There will not be more than 500 commands in the file.
9. Products (both “Items” and “Recipes” are unique. In other words, there will never be two
products with the same name.
6
Hints
• It might be useful to think about how you want to store data and handle changes efficiently
efore starting this part.
• One change command may alter the cost of many items. Good use of indirection (of any sort)
can make this easy to implement. Not having a plan for this, or having a plan that makes
inco
ect assumption about how variables work in C++, will make this problem impossible.
• The string processing is a secret hard part of this program:
– The best way to read the commands file is line-by-line using the getline command,
this is because (if you read word by word) it’s hard to know when the command is over.
– You will probably need a way to process a string word-by-word. One way is to use the
istringstream class (which lets you read from a string the same way you read from a
file. Example:
#include #include #include using namespace std;
int main() {
istringstream line("Recipe: shuriken tape nail nail nail nail");
string command , name , part;
line
command
name
Answered 2 days After Apr 21, 2021

Solution

Kshitij answered on Apr 23 2021
157 Votes
c++ 2 assignment apple/1.cpp
c++ 2 assignment...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here