question archive A shopaholic has to buy a pair of jeans, a pair of shoes, a skirt, and a top with budgeted dollars

A shopaholic has to buy a pair of jeans, a pair of shoes, a skirt, and a top with budgeted dollars

Subject:StatisticsPrice:4.87 Bought11

A shopaholic has to buy a pair of jeans, a pair of shoes, a skirt, and a top with budgeted dollars. Given the quantity of each product and the price per unit, determine how many options of each item are present. If required, all budgeted dollars can be spent

Example

priceOfJeans - 12,3)

priceOfShoes = (4)

priceOfSkirts - (2, 3)

priceOfTops = [1, 2]

budgeted = 10

The shopper must buy shoes for 4 dollars since there is only one option. This leaves 6 dollars to spend on the other 3 items. Combinations of prices paid for jeans, skirts, and tops respectively that add up to 6 dollars or less are [2, 2, 2). 12. 2. 1). 13, 2, 1). [2, 3, 1). There are 4 ways the shopper can purchase all 4 items.

Function Description

Complete the getNumberofoptions function in the editor below.

getNumberOfOptions has 5 parameters:

int priceOfJeans(a): An integer array, which contains the prices of the pairs of jeans available.

int priceOfShoes(b): An integer array, which contains the prices of the pairs of shoes available.

int priceOfSkirts(c): An integer array, which contains the prices of the skirts available.

int priceortops(d): An integer array, which contains the prices of the tops available,

int budgeted the total number of dollars available to shop with. Return long: the number of options present to buy the four items Constraints
 

Shopper's Delight A shopaholic has to buy a pair of jeans, a pair of shoes, a skirt, and a top with budgeted dollars. Given the quantity of each product and the price per unit, determine how many options of each item are present. If required, all budgeted dollars can be spent

Example

priceOfJeans - (2,3)

priceOfShoes - (4)

priceOfSkirts = [2, 3]

priceOfTops = [1, 2]

budgeted = 10

The shopper must buy shoes for 4 dollars since there is only one option. This leaves 6 dollars to spend on the other 3 items. Combinations of prices paid for jeans, skirts, and tops respectively that add up to 6 dollars or less are (2, 223. 12, 2, 1). 13, 2, 1), (2, 3, 1). There are 4 ways the shopper can purchase all 4 items.

Function Description

Complete the getNumberOfOptions function in the editor below. getNumber of options has 5 parameters:

int priceofjeans(a): An integer array, which contains the prices of the pairs of jeans available.

int priceorShoes(b): An integer array, which contains the prices of the pairs of shoes available.

int priceofskirts[c): An integer array, which contains the prices of the skirts available,

int priceofTops(d): An integer array, which contains the prices of the tops available,

int budgeted the total number of dollars available to shop with Return long: the number of options present to buy the four items Constraints

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>


// Function to split the integer input from one line
// Note: this is not relevant for this question
void split(string str, vi &tokens) {
        stringstream ss(str); 
        string temp; 
        while(ss >> temp)
                tokens.push_back(stoi(temp));
}


// =========================== MAIN PART ========================================

// This function backtracts and visits every item and every price of each item.
// then if it is affordable then move to next item and if not it discards that price.

long visitAll(vector<vi > &choices, int index, int budgetleft, long count){
        // if we have visited all items then return
        if(index == 4){
                count += 1;
                return count;
        }

        // visit every price of that item
        for(int i=0; i<choices[index].size(); i++){
                
                // subtract price from budget only when it is not single priced item
                // as we have already subtracted that earlier
                if(choices[index].size() != 1)
                        budgetleft -= choices[index][i];
                
                // if budget left after purchasing is less than zero then discard
                // if budget left is zero and we have purchased last item then it's correct else not.
                if(budgetleft < 0 || (budgetleft == 0 && index != 3)){
                        continue;
                }

                // move to next item
                count = visitAll(choices, index+1, budgetleft, count);
                // again add the item price to budget left so as to perform same for next price.
                budgetleft += choices[index][i];
        }

        return count;
}
// ===============================================================================

// ================= REQUIRED FUNCTION =================================
long getNumberOfOptions(vector<vi > &choices, int budget){
        // first of all subtract the items which are have single prices from budget
        // as these are must to be purchased.
        for(int i=0; i<4; i++){
                if(choices[i].size() == 1){
                        budget -= choices[i][0];
                }
        }

        // ========== MAIN FUNCTION CALL ===============

        return visitAll(choices, 0, budget, 0);
}


int main(){

// ------ Taking input -------
        vi priceOfJeans;
        vi priceOfShoes;
        vi priceOfSkirts;
        vi priceOfTops;
        int budgeted;

        string str;
        getline(cin, str);
        split(str, priceOfJeans);
        getline(cin, str);
        split(str, priceOfShoes);
        getline(cin, str);
        split(str, priceOfSkirts);
        getline(cin, str);
        split(str, priceOfTops);
// ----------------------------------

// ----- Inserting all 4 items into 2d vector ----------
        vector<vector<int> > choices;
        choices.push_back(priceOfJeans);
        choices.push_back(priceOfShoes);
        choices.push_back(priceOfSkirts);
        choices.push_back(priceOfTops);

// --------------------------------------------------------

        cin >> budgeted;

// ============================= FUNCTION CALL ====================================
        cout << "Total Choices: " << getNumberOfOptions(choices, budgeted) << endl;;

}

Screenshot of program:

please use this google drive link to download the answer file.

https://drive.google.com/file/d/1IMSOVvZSB2yRWBpqC6Id7s5Z5Lm1i23k/view?usp=sharing

note: if you have any trouble in viewing/downloading the answer from the given link, please use this below guide to understand the whole process.

https://helpinhomework.org/blog/how-to-obtain-answer-through-google-drive-link

Related Questions