+ 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.
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.
+ 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.
+ 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.
+ 1
I see. Thanks again for the info.
+ 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.
+ 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.
+ 1
I see. Thanks for the info Brian.
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.
0
I see. Thanks for the info.
0
You're welcome :)
0
What did atoi
0
From what i know atoi is used to convert string/char data type into integer data type from a variable.
0
#include <stdlib.h>
0
a1=s1[i]; s1 is an array, aito is not needed.
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.
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'}
0
I see. Thanks for the info Сергей Лансков.