+ 1

Why is this program a mistake?[2]

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

24th Dec 2023, 5:04 PM
Celvien Kurniawan
Celvien Kurniawan - avatar
17 Antworten
0
This is the conversion segment of my solution: typedef struct { int width; int height; } rect; int main() { rect balcony_a; rect balcony_b; scanf("%d,%d", &balcony_a.width, &balcony_a.height); scanf("%d,%d", &balcony_b.width, &balcony_b.height); // to be continued Should you be unfamiliar still with structs and typedefs, think of it as combining variables to a new type that logically belong together. But you can just use arrays or four int variables as you have now.
25th Dec 2023, 6:50 AM
Gordie
Gordie - avatar
+ 1
Your conversion algorithm is more complicated than it needs to be. The 'scanf' family of functions allows for conversion text patterns, e.g. "%d,%d" for the intended input of this task. My guess is that something gets messed up in your conversion.
24th Dec 2023, 10:57 PM
Gordie
Gordie - avatar
+ 1
While it is correct that atoi is for conversion of type, and atoi is being taught in the SL lessons, it is not recommended to use them. The replacement functions are strtol and strtoll. They are a bit more complex, but allow for more flexibility in the bases, more control of how much of the string got converted, and error information. Especially the last part is what is missing in atoi; i.e. an error in conversion is indistinguishable form a conversion of a zero. While one gets through SL fine using atoi, I definitely recommend getting familiar with the strtol function.
25th Dec 2023, 12:01 PM
Gordie
Gordie - avatar
+ 1
I see. Thanks again for the info.
26th Dec 2023, 2:34 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
+ 1
Note though that it will yield the ASCII value of the digit not its numeric value. Also, you cannot cast multi-digit numbers that way. To extract for example the j-th digit in a string s of digits, you may use int i = (int) (s[j] - '0'); Subtracting '0' char removes the offset of the digit chars in the ASCII table.
26th Dec 2023, 9:21 AM
Gordie
Gordie - avatar
+ 1
Celvien Kurniawan the mistake in the posted code is that it breaks when the first input number has more digits than the second input number. It writes the first number into s1 and later overwrites it with the second number. However, it fails to add a terminating null so there might be digits left over from the first number. Insert this line as line 25: s1[j-k] = '\0'; This fix allows the original code to pass all tests.
26th Dec 2023, 10:11 AM
Brian
Brian - avatar
+ 1
I see. Thanks for the info Brian.
26th Dec 2023, 3:23 PM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
I see. Then can you give another method that simpler to solve this? I need that for learning. Cuz i'm still rather new in here.
25th Dec 2023, 5:45 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
I see. Thanks for the info.
25th Dec 2023, 7:10 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
You're welcome :)
25th Dec 2023, 7:20 AM
Gordie
Gordie - avatar
0
What did atoi
25th Dec 2023, 11:20 AM
Boris Rudikov
Boris Rudikov - avatar
0
From what i know atoi is used to convert string/char data type into integer data type from a variable.
25th Dec 2023, 11:23 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
#include <stdlib.h>
25th Dec 2023, 9:43 PM
apl
0
a1=s1[i]; s1 is an array, aito is not needed.
26th Dec 2023, 7:04 AM
Сергей Лансков
Сергей Лансков - avatar
0
Well, a1 = s1[i] in the code, actually like this. (int)a1 = (char)s1[i]. And during that code creation, i have assumption that assigning value with different data type will cause an error. So i used atoi to convert data type in s1 to integer. Sorry if my assumption is wrong.
26th Dec 2023, 7:31 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
Type conversion is not need in char case, actually char is a symbol from ASCII table. Char s[] {49,50,51} is the same as Char s[] {'1','2','3'}
26th Dec 2023, 8:32 AM
Сергей Лансков
Сергей Лансков - avatar
0
I see. Thanks for the info Сергей Лансков.
26th Dec 2023, 9:05 AM
Celvien Kurniawan
Celvien Kurniawan - avatar