<\/span><\/h2>\n\n\n\nGiven the meal price<\/em> (base cost of a meal), tip percent<\/em> (the percentage of the meal price<\/em> being added as tip), and tax percent<\/em> (the percentage of the meal price<\/em> being added as tax) for a meal, find and print the meal’s total cost<\/em>. Round the result to the nearest integer.<\/p>\n\n\n\nExample<\/strong><\/p>\n\n\n\nmealcost <\/em>= 100
tippercent<\/em> = 15
taxpercent<\/em> = 8<\/strong><\/p>\n\n\n\nA tip of 15% * 100 = 15, and the taxes are 8% * 100 = 8. Print the value 123<\/strong> and return from the function.<\/p>\n\n\n\nFunction Description<\/strong>
Complete the solve<\/em> function in the editor below.<\/p>\n\n\n\nsolve<\/em> has the following parameters:<\/p>\n\n\n\n- int meal_cost:<\/em> the cost of food before tip and tax<\/li>
- int tip_percent:<\/em> the tip percentage<\/li>
- int tax_percent:<\/em> the tax percentage<\/li><\/ul>\n\n\n\n
Returns The function returns nothing. Print the calculated value, rounded to the nearest integer.<\/p>\n\n\n\n
Note:<\/strong> Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThere are 3<\/strong> lines of numeric input:
The first line has a double, mealcost<\/strong><\/em> (the cost of the meal before tax and tip).
The second line has an integer, tippercent<\/em><\/strong> (the percentage of mealCost<\/strong><\/em> being added as tip).
The third line has an integer, taxpercent<\/em><\/strong> (the percentage of mealCost<\/em><\/strong> being added as tax).<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n12.00\n20\n8<\/code><\/pre>\n\n\n\nSample Output <\/strong><\/p>\n\n\n\n15<\/code><\/pre>\n\n\n\nExplanation<\/strong><\/p>\n\n\n\nGiven:
mealcost<\/em> = 12<\/strong>, tip_percent<\/em> = 20<\/strong>, tax_percent<\/em> = 8<\/strong><\/p>\n\n\n\nCalculations:
tip<\/em> = 12 and 12\/100 * 20 = 2.4
tax <\/em>= 8 and 8\/100 * 20 = 0.96<\/strong>
total_cost <\/em>= meal_cost<\/em> + tip <\/em>+ tax <\/em>= 12 + 2.4 + 0.96 = 15.36<\/strong>
round(total_cost)<\/em> = 15<\/strong>
We round total_cost<\/em><\/strong> to the nearest integer and print the result, 15<\/strong>.<\/p>\n\n\n\n<\/span>Solution – Day 2: Operators Solution <\/strong><\/span><\/h2>\n\n\n\n<\/span>C<\/strong><\/span><\/h3>\n\n\n\n#include <assert.h>\n#include <limits.h>\n#include <math.h>\n#include <stdbool.h>\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nchar* readline();\n\n\/\/ Complete the solve function below.\nvoid solve(double meal_cost, int tip_percent, int tax_percent) \n{\n double tip=meal_cost*tip_percent\/100;\n double tax=meal_cost*tax_percent\/100;\n int totalCost=(int)round(meal_cost+tip+tax); \n printf(\"%d\",totalCost);\n}\n\nint main()\n{\n char* meal_cost_endptr;\n char* meal_cost_str = readline();\n double meal_cost = strtod(meal_cost_str, &meal_cost_endptr);\n\n if (meal_cost_endptr == meal_cost_str || *meal_cost_endptr != '\\0') \n { \n exit(EXIT_FAILURE); \n }\n\n char* tip_percent_endptr;\n char* tip_percent_str = readline();\n int tip_percent = strtol(tip_percent_str, &tip_percent_endptr, 10);\n\n if (tip_percent_endptr == tip_percent_str || *tip_percent_endptr != '\\0') \n { \n exit(EXIT_FAILURE); \n }\n\n char* tax_percent_endptr;\n char* tax_percent_str = readline();\n int tax_percent = strtol(tax_percent_str, &tax_percent_endptr, 10);\n\n if (tax_percent_endptr == tax_percent_str || *tax_percent_endptr != '\\0') \n { \n exit(EXIT_FAILURE); \n }\n\n solve(meal_cost, tip_percent, tax_percent);\n\n return 0;\n}\n\nchar* readline() {\n size_t alloc_length = 1024;\n size_t data_length = 0;\n char* data = malloc(alloc_length);\n\n while (true) {\n char* cursor = data + data_length;\n char* line = fgets(cursor, alloc_length - data_length, stdin);\n\n if (!line) { break; }\n\n data_length += strlen(cursor);\n\n if (data_length < alloc_length - 1 || data[data_length - 1] == '\\n') \n { \n break; \n }\n\n size_t new_length = alloc_length << 1;\n data = realloc(data, new_length);\n\n if (!data) { break; }\n\n alloc_length = new_length;\n }\n\n if (data[data_length - 1] == '\\n') \n {\n data[data_length - 1] = '\\0';\n }\n\n data = realloc(data, data_length);\n\n return data;\n}<\/pre>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <bits\/stdc++.h>\n\nusing namespace std;\n\n\/\/ Complete the solve function below.\nvoid solve(double meal_cost, int tip_percent, int tax_percent) \n{\n double tip=meal_cost*tip_percent\/100;\n double tax=meal_cost*tax_percent\/100;\n int totalCost=(int)round(meal_cost+tip+tax); \n cout<<totalCost;\n}\n\nint main()\n{\n double meal_cost;\n cin >> meal_cost;\n cin.ignore(numeric_limits<streamsize>::max(), '\\n');\n\n int tip_percent;\n cin >> tip_percent;\n cin.ignore(numeric_limits<streamsize>::max(), '\\n');\n\n int tax_percent;\n cin >> tax_percent;\n \n cin.ignore(numeric_limits<streamsize>::max(), '\\n');\n solve(meal_cost, tip_percent, tax_percent);\n \n return 0;\n}\n<\/pre>\n\n\n\n