Brian is a shopaholic. Whenever there is a discount of the kind where you can buy three items and only pay for two, he feels the need to buy all items in the shop.
You have realised that the shops coming up with these offers are quite selective when it comes to which items you get for free; it is always the cheapest ones. As an example, when Brian goes to the till with six items, costing £40, £35, £30, £25, £15, and £10, he will have to pay £130. In this case he got a discount of £25. You realise that if he goes to the till twice, he might get a bigger discount. E.g. if he goes with the items that costs £40, £35 and £30, he will get a discount of £30 the first round. The next round he takes the items that costs £25, £15 and £10 giving a discount of an additional £10, adding up to a total discount of £40.
Write a new .cpp file to find the maximum discount Brian can get, assuming Brian will always buy 6 items. You will need to ask the user to enter the values of the 6 items and use these values to calculate the maximum discount. Your program should display the full retail total of the six items and the maximum discount.
It may be easier if you assume that the user will enter the values of the items in ascending or descending order. This should be solved WITHOUT the use of arrays or vectors.
Iv been given this program about calculating a maximum discount from 6 groceries to write using IF statement and loops, however Iv been told to do it without using vectors or arrays, which is annoying because its what my mind jumped to first.
I first planned to use a FOR loop to ask the user for 6 values, then calculate the discount but how would I do this because the variable being assigned in the loop will reset each iteration, meaning that in IF statement to compare each value and calculate the discount that way wont work.
it is annoying as all get out but you can just make 6 variables and deal with it.
how much do you know? A struct with an overloaded [] could make a pseudo array with about 10 min work, the [] just returns a reference to the particular variable you want.. does your professor have a sense of humor?
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
usingnamespace std;
struct notanarray
{
int a,b,c,d,e,f,g;
int& operator[](int index)
{
switch(index)
{
case 0:
return a; break;
case 1:
return b; break;
case 2:
return c; break;
case 3:
return d; break;
case 4:
return e; break;
case 5:
return f; break;
case 6: //adding an extra one for std::sort, lazy quick fix
return g; break;
}
}
};
int main()
{
notanarray arr;
arr.a = 12;
arr.b = 34;
arr[0] = 56;
// cout << arr[0];
for(int i = 0; i < 6; i++)
arr[i] = rand() %100;
for(int i = 0; i < 6; i++)
cout << arr[i] << endl;
std::sort(&arr[0], &arr[6]);
cout << "--------\n";
for(int i = 0; i < 6; i++)
cout << arr[i] << endl;
}
so a becomes [0], b becomes [1], and so on. it works just like an array, but you didn't use an array. and you can even make an assignment operator or whatever else if you want to really show off.
and it just gets better and better from here. With this, you can solve the problem the way it should be done (using an array) without (using an array).
Yeah I only really know FOR/WHILE loops and making decisions/comparisons using IF statements hahaha, only couple weeks in. I know a small amount about arrays from years ago when I used python in secondary school, but can only really use loops and if statements for this. Really annoying
if you don't want to use that, you can do it one variable at a time, but its going to be an unholy mess. Again, it kind of depends on whether the professor is grouchy or not.
Where are you initializing the total and discount?
Also, if conditions don’t work like that. If you can assume that the input will be sorted, then you do not need to bother with an if. Just take, say, value1 as the smallest, and assume your input is correct.
If you want to be exactly literal in your reading of the assignment, then either value1 or value3 will be the smallest. Write your if without value2.