+ 2

Duct Tape problem in C

Plz check once Only last case fails https://code.sololearn.com/cniu7w9t9ll2/?ref=app

12th Jan 2023, 10:39 AM
Wakil Ah Hamidi
Wakil Ah Hamidi - avatar
7 Antworten
+ 1
Wakil Ahmad Hamidi No, leave the code just like it was before, with the floats. Just add the .5 to the area. scanf("%f%f", &height, &width); area = height*width/5+.5; printf("%.f", area);
12th Jan 2023, 12:58 PM
Scott D
Scott D - avatar
+ 2
Scott D Okay, got it. Thanks for clarifying
12th Jan 2023, 1:24 PM
Wakil Ah Hamidi
Wakil Ah Hamidi - avatar
+ 1
Wakil Ahmad Hamidi "Output Format: An integer that represents the total rolls of duct tape that you need to buy." Integer, not float. So the problem is maybe that using %.f is rounding down to the closest integer, not up. Try area = height*width/5+.5; Also, float tape = 5 is an unused variable so you don't need it, or you could use something like: area = height*width/tape+.5; That might be better since it makes the code more descriptive. And, not really important, but you don't need to assign a value to area: float height, width, area;
12th Jan 2023, 11:36 AM
Scott D
Scott D - avatar
+ 1
Scott D Do you mean like this: #include <stdio.h> int main() { int height, width, area; scanf("%d%d", &height, &width); area = height*width/5+.5; printf("%d", area); return 0; } For this 2nd case is also wrong including the previous last one
12th Jan 2023, 12:55 PM
Wakil Ah Hamidi
Wakil Ah Hamidi - avatar
+ 1
Scott D But in your prevoius reply you confused me, you said int type But all what was required adding .5 to 5 (height*width/5+.5) The question is why we add this .5, what is its logic?
12th Jan 2023, 1:06 PM
Wakil Ah Hamidi
Wakil Ah Hamidi - avatar
+ 1
Wakil Ahmad Hamidi Sorry about the confusion, I guess I wasn't specific enough. I'd have to look it up to be sure, but I think when you use the float with the dot %.f it's rounding down to the nearest integer. So if the area was say 30.6 it rounds down to 30 (6 rolls of tape). But if the area was 30.6 you'd need 7 rolls. Adding .5 would make the area 31.1 So you'd now have enough tape.
12th Jan 2023, 1:14 PM
Scott D
Scott D - avatar
0
What is this 🧐
13th Jan 2023, 9:27 PM
Osmany Baluta
Osmany Baluta - avatar