+ 2

Why is this program a mistake [3]

Can anyone tell me what i did wrong? I can't make some case test right. Btw here is my code: https://sololearn.com/compiler-playground/cNTJGKRNGHfI/?ref=app If you have another solution. Please share cuz i'm still learning.

6th Jan 2024, 6:21 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
14 odpowiedzi
+ 1
I don't know if there are ever more than three items. All i said is that the task description does not say that there are exactly three. So, you may have to code in the possibility that you need to parse an indefinite number of items. You may use dynamic memory but it would actually be overkill in this situation. On the other hand, i do not want to discourage you from practising dynamic memory :) You can do fine with 'strtok' and a loop that reads the current item, updates the most expensive item, and accumulates all items. Then just subtract the most expensive one and calculate the saved amount after tax and tip.
6th Jan 2024, 9:35 AM
Gordie
Gordie - avatar
+ 5
As described below input is a string and not three numbers. So you could receive a string and convert that to as many as possible numbers. Input Format: An string of numbers separated by commas that represent the prices for all of the items that you want to purchase (without tax).
6th Jan 2024, 11:25 AM
JaScript
JaScript - avatar
+ 3
Did you have a task description?
6th Jan 2024, 7:34 AM
JaScript
JaScript - avatar
+ 1
For starters, it doesn't say that there are exactly three items. It says it is a comma separated list of items. In addition, you might be able to do a lot of the math beforehand and simplify the calculation.
6th Jan 2024, 7:38 AM
Gordie
Gordie - avatar
+ 1
You're welcome :) Let me know if you run into any trouble. I also have a sample solution ready for you. But i think it is better if you try again first.
6th Jan 2024, 9:44 AM
Gordie
Gordie - avatar
+ 1
It looks like there's a mistake in your code related to the format specifier used in the `printf` statement. The `calc` function returns a `float`, but in your `printf` statement, you are using `%d`, which is the format specifier for integers. To fix this, you should use `%f` instead, as follows: ```c printf("%f", calc(a, b, c)); ``` This will correctly print the floating-point result of the `calc` function. Additionally, you might want to consider adding a newline character `\n` at the end of your `printf` statement to improve the output format: ```c printf("%f\n", calc(a, b, c)); ``` This way, the result will be displayed on a new line. As for the logic in your `calc` function, it seems to be attempting to calculate the difference between the total cost of three items and the discounted cost based on the most expensive item. If there are issues with specific test cases, you might want to review and refine the conditions to cover all scenarios correctly.
8th Jan 2024, 2:23 AM
𝐀𝐲𝐞𝐬𝐡𝐚 𝐍𝐨𝐨𝐫
𝐀𝐲𝐞𝐬𝐡𝐚 𝐍𝐨𝐨𝐫 - avatar
+ 1
You're welcome :) I just changed my while loop to a for loop to see when each testcase triggers.
8th Jan 2024, 4:14 AM
Gordie
Gordie - avatar
0
JaScript sorry for late response. Here's the task descrition. https://www.sololearn.com/coach/58?ref=app
6th Jan 2024, 9:22 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
Gordie oh, there can be more than 3 items?! i think i overlooked that. Looks like i need to study dynamic memory again. Tbh it's my first tackling dynamic memory allocation. So it will be another big challenge.
6th Jan 2024, 9:31 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
Gordie i see. I'll try that. Thanks for the info.
6th Jan 2024, 9:39 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
𝐀𝐲𝐞𝐬𝐡𝐚 𝐍𝐨𝐨𝐫 in my printf, why i use '%d' instead of '%f' because i saw that the task insisted on omitting the decimal value. Also in my printf, i used '(int)' to covert the return value of function 'calc' from float to integer. And what makes this more challenging is that Gordie mentioned that the task input isn't exactly 3. And it seems (probably) true because i still get some error from result testing inside that code coach challenge.
8th Jan 2024, 3:49 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
I have just checked, the numbers of items purchased are in ascending order of the test cases: 3, 3, 3, 5, 2, 1, 10 With the curious fact that test case 4 is also correct with just four items. Which leads me to believe that the last item was free (cost 0).
8th Jan 2024, 3:56 AM
Gordie
Gordie - avatar
0
Seriously?! Wow thanks for the info.
8th Jan 2024, 4:11 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
26th Sep 2024, 3:53 AM
Celvien Kurniawan
Celvien Kurniawan - avatar