Given a budget (type long) and a list of prices represented as entries of a container of type vector, find out how many items on the price_list you can afford at most. This is the return value of the function. Here is its prototype: int get_most_items(std::vectorprice_list, long budget) To solve this homework, it is a good idea to first sort the price_list in ascending order. For sorting in ascending order, you can use the function std::sort(). It can be called as std::sort(v.begin(), v.end()) .Note that there may be items on the price list whose prices are negative to make the problem a bit more interesting. If the size of price_list is zero, the function returns 0. All necessary libraries have been included. All numbers are expressed in Cents to avoid rounding errors.   Here is a simple example: std::vector price_list = { 10, 199, 1};  long budget = 11; int no_items = get_most_items(price_list, budget); The variable no_items will then have the value 2 as two items can be afforded from the price list given the available budget.   main.cpp: #include #include "shopping.h" int main(int argc, char **argv) {     std::cout << "Please enter positive integer size of vector: ";     std::vector price_list;     size_t vector_size{ 0 };     std::cin >> vector_size;     std::cout << "You entered " << vector_size << " as the size of the vector." << std::endl;     std::cout << "\nEnter integer entries followd by Return (vector is called price_list):" << std::endl;     long price{ 0 };     for (size_t i = 0; i < vector_size; i++)     {         std::cout << "price_list[" << i << "]: ";         std::cin >> price;         price_list.push_back(price);     }     std::cout << "Please enter positive value for your budget: ";     long budget;     std::cin >> budget;     int no_items = get_most_items(price_list, budget);     std::cout << "You were able to afford " << no_items << " items." << std::endl;          return 0;          }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Given a budget (type long) and a list of prices represented as entries of a container of type vector<long>, find out how many items on the price_list you can afford at most. This is the return value of the function. Here is its prototype:

int get_most_items(std::vector<long>price_list, long budget)

To solve this homework, it is a good idea to first sort the price_list in ascending order. For sorting in ascending order, you can use the function std::sort(). It can be called as std::sort(v.begin(), v.end()) .Note that there may be items on the price list whose prices are negative to make the problem a bit more interesting.

If the size of price_list is zero, the function returns 0.

All necessary libraries have been included. All numbers are expressed in Cents to avoid rounding errors.
 
Here is a simple example:
std::vector<long> price_list = { 10, 199, 1}; 
long budget = 11;
int no_items = get_most_items(price_list, budget);
The variable no_items will then have the value 2 as two items can be afforded from the price list given the available budget.
 
main.cpp:

#include <iostream>
#include "shopping.h"

int main(int argc, char **argv)
{
    std::cout << "Please enter positive integer size of vector: ";
    std::vector<long> price_list;
    size_t vector_size{ 0 };
    std::cin >> vector_size;
    std::cout << "You entered " << vector_size << " as the size of the vector." << std::endl;
    std::cout << "\nEnter integer entries followd by Return (vector is called price_list):" << std::endl;
    long price{ 0 };
    for (size_t i = 0; i < vector_size; i++)
    {
        std::cout << "price_list[" << i << "]: ";
        std::cin >> price;
        price_list.push_back(price);
    }
    std::cout << "Please enter positive value for your budget: ";
    long budget;
    std::cin >> budget;
    int no_items = get_most_items(price_list, budget);
    std::cout << "You were able to afford " << no_items << " items." << std::endl;
    
    return 0;
    
   

Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education