without using arrays or vectors

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace 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).

Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

int main()
{
    double value1, value2, value3, total=0, discount=0, overall_total=0;
    
    for (int counter = 0; counter<2; counter++)
    {
            cout <<"Enter a value: ";
            cin >> value1;
            cout <<"Enter a value: ";
            cin >> value2;
            cout <<"Enter a value: ";
            cin >> value3;
        
        if (value1 < value2 && value3)
        {
            discount = discount + value1;
            total = value1 + value2 + value3;
        }
    }
    
    overall_total = total - discount;
    
    cout <<"Total price is: " <<total;
    cout <<"\n";
    cout <<"discount is: " <<discount;
    cout <<"\n";
    cout <<"Overall price after discount is: " <<overall_total;
}



Iv managed to do this however, the "total" calculation doesnt work as it just adds the first 3 numbers twice.
Last edited on
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.

Hope this helps.
Topic archived. No new replies allowed.