How do I calculate a bill using vectors?
4 views (last 30 days)
Show older comments
Right now, I have an assignment to do this:
Please write a MATLAB script that will allow the farm to calculate the bill and provide a detailed invoice when someone visits. The user of the program will need to weight their products, and enter the product code of their product and the weight for each type of product they have. Use Comments in your code.
Product Product Code Cost per kg
Pumpkin 1 $5.99
Fuji Apple 2 $2.50
Gala Apple 3 $3.50
Red Apple 4 $4.50
Note that cost is per kilograms (kg), but users will enter the weight in grams (g).
The program should prompt the user to enter the product code and weight as a vector, or [0, 0] to check-out.
Once the user enters [0, 0] the program should calculate the subtotals of each product type and grand total including a 7% sales tax. The program should then output an itemized receipt with total at the bottom.
I have this so far:
%%Product Code
PRODUCT = [5.99, 2.50, 3.50, 4.50];
%%Price
weights = [0, 0, 0, 0];
%%Calculate
code = -1;
gram = -1;
while code ~= 0 && gram ~= 0
input_line = input('Input the product code and weight as a vector or [0, 0] to check-out: ');
code = input_line(1);
gram = input_line(2);
if code > 0 && code <= 4
weights(code) = weights(code) + gram;
end
if gram > 0
total = weights(code) * code / 1000;
end
end
while code == 0 && gram == 0
The first method is to try and calculate the total of the price by first adding the weight the user inputs to the "weights" variable, and then multiplying that by the product code. After, I'm trying to calculate all the totals the user inputs and add them all together to get the final total, but one, I don't know if my first method is even right, and two, I don't know how to add all the totals up. Any help? I'm new to matlab and don't really know what I'm doing.
If it helps, the sample run is supposed to look like this:
Input the product code and weight as a vector or [0, 0] to check-out: [2, 180]
Input the product code and weight as a vector or [0, 0] to check-out: [1, 1500]
Input the product code and weight as a vector or [0, 0] to check-out: [3, 75]
Input the product code and weight as a vector or [0, 0] to check-out: [2, 200]
Input the product code and weight as a vector or [0, 0] to check-out: [0,0]
1 1500 g @ 5.99 / kg 8.98
2 380 g @ 2.50 / kg 0.95
3 75 g @ 3.50 / kg 0.26
Total with 7% tax: 10.91
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!