Question

C++ program c that reads a list of costs for a few items. Program should calculate...

C++ program c that reads a list of costs for a few items. Program should calculate the best price for the “buy-two-get-one-free” deal.

The first line (= 5 in the example) indicates the number of items for you to buy. The second line presents the costs of five items. Note that if you buy two items with your money, you can get one item free. For example, let’s say that you buy the first two items (= 2 and 7) with your money. Then, you can choose one item for free. Let’s say that you select the item with the cost 6 for free. After that, there are two items left (= 5 and 5). Since you have to buy the two items anyway, you have to buy them with your money. In this case, you have to pay total 19 (= 2, 7, 5, and 5).

Sample Input 0

5
2 7 5 6 5

Sample Output 0

18

Sample Input 1

3
10 30 20

Sample Output 1

30
0 0
Add a comment Improve this question Transcribed image text
Answer #1

IDE Used: DEV C++

Explanation of algorithm used: First, calculate the number of sets including 3 items. For this, we divide the total items by 3. The dividend in integer value will be the total number of free items. Free items will be those items with the highest costs. For example, let the total number of items be 8 then 8/3 will be 2 so 2 free items. Then arrange the items in ascending order of their cost. Calculate the sum of cost without the two maximum costs i.e the cost of 2 free items. This sum of the cost will be the best cost of buy2 get 1 free offer.

Code

#include <iostream>
using namespace std;


int main() {
  
   //number of items to buys
   int nof_items;
  
   //number of free items
   int free_items;
  
//to store the final best cost
   int best_cost;
  
   //counter variables i and j can also be used
   int ct1, ct2;
  
   //to store temporary cost while swapping
   int temp_cost;
  
   //array to store the cost of all items
   int price_array[999];
  
   //getting the number of items from the user
   cout<<"Enter the number of items you want to buy: ";
   cin>>nof_items;
  
   cout<<"\nEnter the cost of each item\n";
   for(ct1=0;ct1<nof_items;ct1++ )
   {
       cin>>price_array[ct1];
   }
  
   //calculate the numeber of free items
   //which is equal number of sets of 3 items

   free_items = nof_items/3;
  
   //find the best cost for buy 2 get 1 offer
   //for this we will arrange the cost and array in ascending order
  
for(ct1=0;ct1<nof_items;ct1++)
   {
       for(ct2=0;ct2<nof_items-ct1-1;ct2++)
       {
           if(price_array[ct2]>price_array[ct2+1])
           {
               //swapping the values
               temp_cost = price_array[ct2];
               price_array[ct2] = price_array[ct2+1];
               price_array[ct2+1] = temp_cost;
           }
       }
   }
  
   //then we will find the sum of array excluding the free items
   //that is removing the highest cost items

   for(ct1=0;ct1<nof_items-free_items; ct1++)
   {
       best_cost = best_cost+price_array[ct1];
   }
  
   //printing the sum of best price
   printf("Best price of buy2 get 1 deal is %d", best_cost);
   return 0;
}

Code Snippets

Sample Outputs

Add a comment
Know the answer?
Add Answer to:
C++ program c that reads a list of costs for a few items. Program should calculate...
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
  • Write a Python program stored in a file q3.py that asks for the prices of items...

    Write a Python program stored in a file q3.py that asks for the prices of items and your money amount as input, and prints the options of items along with cost as output. Enter list of prices : 7 3 9 12 26 17 8 9 Enter dollar amount : 16 You have total 8 options Option 1: You can buy item 1 and item 2 with $10 .0. Option 2: You can buy item 1 and item 3 with...

  • Q1 (2 pts) Shopping list Write a program that prompts a user for items on their...

    Q1 (2 pts) Shopping list Write a program that prompts a user for items on their shopping list and keeps prompting the user until they enter "Done" (make sure your program can stop even if the user enters "Done" with different cases, like "done"), then prints out the items in the list, line by line, and prints the total number of items on the shopping list. Output (after collecting items in the while loop) should look something like this: Apple...

  • Write a program that first asks the user how many budget items they have. Then using...

    Write a program that first asks the user how many budget items they have. Then using dynamic arrays, create the appropriate arrays to hold the following data: Allow the user to enter the budget items and amount budgeted for the number items created in the dynamic array. (For your testing, just use four or five.) Once all budget items are entered, the program should then output each budget item, the amount of money budgeted for that item, and the percentage...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

  • In this project you will write a C++ program that simulates the purchase of a single...

    In this project you will write a C++ program that simulates the purchase of a single item in an online store. What to do Write a C++ program that: 1. Prints out a welcome message. 2. Prompts the user for the following information: (a) Their first name (example: Roger) (b) Their last name (example: Waters) (c) The name of the product (example: Brick) (d) The unit price of the product (example: 1.99) (e) The quantity to buy (example: 200) (f)...

  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

  • Write a C++ program that reads in input until the EOF. The program should output the...

    Write a C++ program that reads in input until the EOF. The program should output the input exactly...with the following changes: Anytime the substring "New York University", "American", or "Lady gaga" appears in the text, it should be replaced with 5 asterisks "*****". Example: Input: New York University safmsa qwqoca   safffja qwrq ssss 23333 af21jspof American shfafh121 Lady gaga spdj1- (Note that this is a paragraph with ONE line in the middle) Output: ***** safmsa qwqoca safffja qwrq ssss 23333...

  • using C programming Date Write a program that reads a month and a day in that...

    using C programming Date Write a program that reads a month and a day in that month and prints the date in format "month day". The input is two integers, one between 1 and 12 (inclusive) and one between 1 and 31 (inclusive) and the output is a single line in format "month day". For example, for input 2 10 the program will print "February 10th". The day will always have the ordinal indicator, e.g. 1st, 2nd, 3rd, 4th. Example...

  • This application is for you, the shopper in mind. Putting together an itemized shopping list that...

    This application is for you, the shopper in mind. Putting together an itemized shopping list that contains the items you need, the items you want, and the anticipated cost, this program will generate a total price. This way you can decide if your shopping budget fits the bill. Example list: Price List Binder paper: 2.29 Mop: 7.50 Scouring pads: 5 Your application program will create and write a new list to an output file in which the need and wish...

  • Consider the following C++ program. It reads a sequence of strings from the user and uses...

    Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...

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