Question
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;


Part 1. [30 points] In this part, your program loads a vending machine serving cold drinks. You start with many foods, some are drinks. Your code loads a vending machine from foods, or, it uses water as a default drink. Create class Drink, make an array of drinks, load it and display it.

Part 1 steps:

  1. [5 points] Create a class called Drink that contains information about a single drink. Provide private data members for name and calories.
  2. [5 points] To class Drink, add a single constant data member that is shared by all instances. It indicates that all drinks are of the same form_t : Liquid.
  3. [5 points] Provide a constructor which has parameters that specify the name and calories. In the constructor, validate the inputs – the name must not be empty (must have at least 1 letter), and the calories must be 0 or more. If either the name and/or price provided are missing or invalid, set the name to “water” and the calories to 0. Inside the constructor, do not prompt the user for values or corrections to values – just use the parameters as provided.
  4. [5 points] Provide a display “getter” method that displays the name and calories of the food item. Show name and calories in fixed-width columns so they line up. (Do not waste time creating unnecessary getters and setters.)
  5. [5 points] Create an array called drinks and load drinks with instances constructed from food from the vector foods. For each food in foods, if the food is Liquid and Cold, place into drinks; if not, use the default drink (described in step 1C). Elements in a vector can be accessed using notation: “foods[i]”; fields can be accessed using dot notation: .name, .calories, .form, etc.
  6. [5 points] Provide a loop that displays the contents of array drinks to the console. To display individual elements, use the display method from Step 1D above. This displays the items in array drinks that were originally in vector foods or generic drinks.

The left table shows the vector foods (provided). The right table is the contents of array drinks after loaded from the data in foods. Items (bold) are default drinks because foods were not Liquid and Cold.

vector: foods

name

calories

price

form

temp

beans

8

.18

Solid

Room

coke

240

.57

Liquid

Cold

oats

10

.12

Solid

Room

steak

310

12.34

Solid

Frozen

milk

80

1.21

Liquid

Cold

soup

95

2.34

Liquid

Hot

array: drinks

name

calories

water

0

coke

240

water

0

water

0

milk

80

water

0

Part 2. [20 points] For this part you will use pointers and dynamic memory allocation.

Part 2 steps:

  1. [5 points] Dynamically allocate one instance of class Drink. To construct this, use name “lemonade” and calories 123. Provide name and calories to the constructor, because there are no setters. Do this with one statement on one line of code, if possible.
  2. [5 points] Display the address in memory where the instance of Drink was allocated. Provide output such as: lemonade is at address: 0x356480
  3. [5 points] Display the contents of the lemonade instance (its name and calories) allocated in Step 2A using the display method provided for the Drink class in Step 1D.
  4. [5 points] De-allocate the instance of class Item that you allocated in Step 2A above. After de-allocation, set the Drink pointer to a safe value, so it can be determined later that it does not point to anything.

Part 3. [15 points] For this part you will overload operators. Provided is a class Fraction. You will add some new overloaded operator methods to class Fraction.

Part 3 steps:

  1. [5 points] Implement operator++ (pre-increment) for class Fraction such that it increments the numerator (top) by one and leaves the denominator alone. Testing code is provided: Fraction a=4; ++a; ++a; cout<<a.get()<<endl; // displays 6
  2. [5 points] Implement operator-- (pre-decrement) for class Fraction so it decrements the numerator (top) by one and leaves the denominator alone. Testing code is provided:
    Fraction b=5; --b; --b; cout<<b.get()<<endl; // displays 3
  3. [5 points] Implement operator* for class Fraction. It multiplies two fractions together. It multiplies the numerator times the numerator and the denominator times the denominator. For example: 6/1 * 7/1 = (6*7)/(1*1) = 42. Testing code is provided:
    Fraction c=6, d=7, e=c*d; cout<<e.get()<<endl; // displays 42.

Part 4. [20 points] For each step, write a recursive version of a provided iterative function. Provided is a recursive function body and a testing loop. Each recursive function should only call itself (not some other function). Avoid global and static local variables.

Part 4 steps:

  1. [5 points] Write a recursive function line(n) that draws a line (of dashes) of length n.
    Example: line(5) displays: -----
  2. [5 points] Write a recursive function left_arrow(n) that draws an arrow that points to the left (left arrow followed by a n dashes).
    Example: left_arrow(5) displays: <-----
  3. [5 points] Write a recursive function right_arrow(n) that draws an arrow that points to the right (n dashes followed by a right arrow).
    Example: right_arrow(5) displays: ----->
  4. [5 points] Write a recursive function double_arrow(n) that draws an arrow that points to the left and right (left arrow, n dashes, followed by a right arrow). (Challenge)
    Example: double_arrow(5) displays: <----->
  5. None of your functions should allow “run-away” recursion, regardless of the input.

Part 5. [5 points] Extra credit, up to 5 points. Write 5 different examples of C++ code that leads to “undefined behavior” and could crash a program. Each example must have a different fault. In a comment, explain the problem. An infinite loop is a hang, not a crash.

Example of crash:
     // float sum=123.45; int count=0; float avg=dbl/count; // divide by zero, undefined behavior

You cannot use divide by zero as one of your crash behaviors, because it was provided



#include <iostream> #include <iomanip> #include <vector> using namespace std; // This is used for step 1 and step 2; leave a
DEWIVEL <<setw(2)<<(char)food. form <<setw(2)<<(char) food. tempecendl; cout<<endl; // -section above code is provided, do no
7/ Fraction c=6, da7, e=cd; // cout<<e=<<e.get()<<endl<<endl; 77 test code, uncomment when ready // test code, expect: 42,


- Ay cout<<-; if (n>=0) cout<<>; // Put your new recursive versions for step 4 here... void line_recursive(int n) { // fi
void step4() { // Below is testing code for step 4; it will be called from main. No changes needed. 11 recurse(); // call to


1 Start... Step 1: Contents of vector foods, already loaded: beans $ 0.18 8 SR coke $ 0.57 240 LC oats $ 0.12 1.0 SR steak $1
; לת :3 ח n=2; :l-ת לל, ;0 ת ל-> <- - -:1 =n ------:לת ל--ל--- ;3 =ח -------- ;5 =ח Test students new recursive versions of
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer for your question in C++ Programming Language.

Kindly upvote if you find the answer helpful.

NOTE : According to the HOMEWORKLIB POLICY, experts are allowed to answer a max of one question, when given multiple questions in a given question. Answered as per the HOMEWORKLIB POLICY. Apologies for this.

I have answered Part - 1 (Steps A - F)

(I displayed vector data in the code, it is optional only as it is asked to display only drinks array in step F, So if you don't want the data to be displayed kindly comment the lines (66 to 73) from screenshots below)

CODE :

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

enum form_t { Solid = 'S', Liquid = 'L', Gas = 'G'};
enum temp_t { Frozen = 'F', Cold = 'C', Room = 'R', Hot = 'H'};
struct Food{
   string name;
   int calories;
   double price;
   char form;
   char temp;
};
//PART - 1
class Drink{
   /*(A)Create a class called Drink that contains information about a single drink.
   Provide private data members for name and calories.*/
   private:
       string name;
       int calories;
       /*(B) To class Drink, add a single constant data member that is shared by all instances.
       It indicates that all drinks are of the same form_t : Liquid.*/
       const char form_t;
   public:
       //(C) Provide a constructor which has parameters that specify the name and calories.
       Drink(string Name,int cal):form_t('L'){

           /*In the constructor, validate the inputs –
           the name must not be empty (must have at least 1 letter), and the calories must be 0 or more.*/
           if(Name.compare("") == 0 || cal < 0){
               name = "water";
               calories = 0;
           }
           else{
               name = Name;
               calories = cal;
           }
       }
       //(D) Provide a display “getter” method that displays the name and calories of the food item.
       void getter(){
           cout << left << setw(10) << name << calories << endl;
       }
};

int main(){
   struct Food food;
   string name;
   int calories;
   /*Initialise vector foods*/
   vector<Food> foods;
   food.name = "beans"; food.calories = 8; food.price = .18; food.form = Solid; food.temp = Room;
   foods.push_back(food);
   food.name = "coke"; food.calories = 240; food.price = .57; food.form = Liquid; food.temp = Cold;
   foods.push_back(food);
   food.name = "oats"; food.calories = 10; food.price = .12; food.form = Solid; food.temp = Room;
   foods.push_back(food);
   food.name = "steak"; food.calories = 310; food.price = 12.34; food.form = Solid; food.temp = Frozen;
   foods.push_back(food);
   food.name = "milk"; food.calories = 80; food.price = 1.21; food.form = Liquid; food.temp = Cold;
   foods.push_back(food);
   food.name = "soup"; food.calories = 95; food.price = 2.34; food.form = Liquid; food.temp = Hot;
   foods.push_back(food);
  
//Display foods data
   cout << "Vector : Foods" << endl;
   cout << left << setw(10) << "Name" << left << setw(10) << "Calories" << left << setw(10) << "Price" << left << setw(10) << "Form"
   << left << setw(10) << "Temp" << endl;
   for(int i= 0;i<(int)foods.size();i++){
       cout << left << setw(10) << foods[i].name << left << setw(10) << foods[i].calories << left << setw(10) << foods[i].price
       << left << setw(10) << foods[i].form << left << setw(10) << foods[i].temp << endl;
   }

   //(E)Create an array called drinks and load drinks with instances constructed from food from the vector foods.
   Drink *drinks[(int)foods.size()];
   for(int i=0;i< (int)foods.size();i++){
       if(foods[i].form == 'L' && foods[i].temp == 'C'){
           drinks[i] = new Drink(foods[i].name,foods[i].calories);
       }
       else{
           drinks[i] = new Drink(name,calories);
       }
   }
   //(F) Provide a loop that displays the contents of array drinks to the console.
   //To display individual elements, use the display method from Step 1D above.
   cout << endl << "Array : Drinks" << endl;
   cout << left << setw(10) << "Name" << "Calories" << endl;
   for(int i= 0;i<(int)foods.size();i++){
       drinks[i]->getter();
   }
   return 0;
}

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

1 2 3 4 #include <iostream> #include<string> #include <iomanip> #include <vector> using namespace std; 5 6 7 8 9 10 11 12 13

the name must not be empty (must have at least 1 letter), and the calories must be o or more. */ if (Name.compare () == 0 |

61 62 food.name = milk; food.calories = 80; food.price = 1.21; food. form = Liquid; food. temp = Cold; foods.push_back (foo

} return 0; 91 92 93 94

OUTPUT :

Vector : Foods Name Calories Price beans 8 0.18 coke 240 0.57 oats 10 0.12 steak 310 12.34 milk 80 1.21 soup 95 2.34 Form S L

Any doubts regarding this can be explained with pleasure :)

Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part,...
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
  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem;...

    #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem; double menuPrice; }; void getData(menuItemType menuList[]); void showMenu(menuItemType menuList[], int x); void printCheck(menuItemType menuList[], int menuOrder[], int x); int main() { const int menuItems = 8; menuItemType menuList[menuItems]; int menuOrder[menuItems] = {0}; int orderChoice = 0; bool ordering = true; int count = 0; getData(menuList); showMenu(menuList, menuItems); while(ordering) { cout << "Enter the number for the item you would\n" << "like to order, or...

  • #include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS =...

    #include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS = 8; vector <double> inverse(NUM_ITEMS); int j; double temp; for (int i = 0; i < NUM_ITEMS; i++) { inverse.at(i) = 1 / (i + 1.0); } cout << fixed << setprecision(2); cout << "Original vector..." << endl; for (int i = 0; i < NUM_ITEMS; i++) { cout << inverse.at(i) << " "; } cout << endl; cout << "Reversed vector..." << endl; for...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std;...

    Please complete Part 1. The code is also below: #include <pthread.h> #include <iostream> using namespace std; void *PrintHello(void *arg) { int actual_arg = *((int*) arg); cout << "Hello World from thread with arg: " << actual_arg << "!\n"; return 0; } int main() { pthread_t id; int rc; cout << "In main: creating thread \n"; int t = 23; rc = pthread_create(&id, NULL, PrintHello, (void*) &t); if (rc){ cout << "ERROR; return code from pthread_create() is " << rc <<...

  • Use this code to create multiple functions. #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() {...

    Use this code to create multiple functions. #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { cout << fixed << showpoint << setprecision(2); ofstream outFile; outFile.open("Feras's.txt"); outFile << "..Skinny Feras's Restaurant ..\n\n" << endl; int choise=10, quantity; float paid, SubTotal=0, Tax = .10, Total, RM, more; while(choise!=0) { system("cls"); cout << "\t**Welcome To Skinny Alsaif Restaurant Lol**" << endl; cout << "\nWhat would you like to have?" << endl; cout << "1. Burger." << endl; cout << "2. Pizza." <<...

  • Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height;...

    Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height; int weight; public: void setHeight(int h){ height = h; } int getHeight(){ return height; } void setWeight(int w){ weight = w; } int getWeight(){ return weight; } virtual void makeSound() = 0; virtual void eat(); }; class Dog: public Animal{ public: void makeSound(){ cout << "Bow Bow\n"; } void eat (){ cout <<"The dog is eating\n"; } void Catch(){ cout << "The dog is...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void...

    I need help with this code: #include <iostream> #include <cstdlib> #include <string> using namespace std; void dump(int ar[], int size) { cout << "\nDUMP [ "; for (int i=0; i<=size; i++) { cout << ar[i] << " "; } cout << "]\n\n"; } void quicksort(int ar[], int start, int end) { cout << "TOP OF SORT ===========================" << endl; dump(ar, start, end); int pivot = ar[end]; int left = start; int right = end - 1; int tmp; cout <<...

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