Question 7
Back to Exercise
Discount depends on the cart total (below Rs. 1000: 0%, up to 4999: 5%, from 5000: 10%) and membership (+5% for members). Compute the final amounts with a table.
Solution
#include <stdbool.h>
#include <stdio.h>
static const int discount[3][2] = { /* [total band][is member] in percent */
{0, 5},
{5, 10},
{10, 15},
};
int band(long rupees) { return rupees < 1000 ? 0 : rupees < 5000 ? 1 : 2; }
int main(void) {
struct { long total; bool member; } orders[] = {{800, true}, {2500, false}, {6000, true}, {6000, false}};
for (int i = 0; i < 4; i++) {
int pct = discount[band(orders[i].total)][orders[i].member];
long paise = orders[i].total * 100 * (100 - pct) / 100;
printf("Rs. %ld (%s): %d%% off -> Rs. %ld.%02ld\n", orders[i].total,
orders[i].member ? "member" : "guest", pct, paise / 100, paise % 100);
}
return 0;
}
Rs. 800 (member): 5% off -> Rs. 760.00 Rs. 2500 (guest): 5% off -> Rs. 2375.00 Rs. 6000 (member): 15% off -> Rs. 5100.00 Rs. 6000 (guest): 10% off -> Rs. 5400.00
The six possible rules live in one 3 × 2 table; a bool indexes the column directly (false = 0, true = 1). Working in paise keeps the money exact.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!