0

Code coach

Three of the test cases in the balconies code coach are correct but two of the locked cases are false , please do give a hint on what might be wrong in the code I have written https://www.sololearn.com/coach/24?ref=app

12th Oct 2024, 5:26 PM
Sanjana
Sanjana - avatar
8 Réponses
+ 3
you are trying to multiply strings. you can only multiply numbers
14th Oct 2024, 5:02 AM
Lisa
Lisa - avatar
+ 3
Sanjana as you figured out, the char arrays hold the ASCII character values and only one digit, whereas there could be more than one in each number. Besides that, the declared char arrays are too short to hold larger input numbers. That causes an out-of-bounds overrun of the array memory which corrupts other variables. Even though the task says you are given strings as input, with an adjustment to the format specifier you can have scanf() do the parsing and numeric conversion for you. It even can skip the comma between numbers. It is surprisingly versatile! Example: int lengthA, widthA; scanf("%d,%d", &lengthA, &widthA); After it reads the first item, the comma in the format specifier tells scanf() to look for a comma in the input, and skip past it to read the next item. Furthermore, you can have it read both lines of input at once by including a newline in the format specifier scanf("%d,%d\n%d,%d", ...); Now it skips the newline, too. Neat!
14th Oct 2024, 2:15 PM
Brian
Brian - avatar
+ 1
Can you please show your attempt by pasting your code in the code playground and attach it here
12th Oct 2024, 5:51 PM
Aysha
Aysha - avatar
+ 1
Show your code. Mention the task name. Your link will work only for you
12th Oct 2024, 5:51 PM
Lisa
Lisa - avatar
0
#include <stdio.h> int main() { char a[4],b[4]; // inputing the string scanf("%s",a); scanf("%s",b); if((a[0]*a[2])>(b[0]*b[2])) //multiplying the first and the third element of the string printf("Apartment A"); else printf("Apartment B"); return 0; } This is my code And the problem name is balconies
14th Oct 2024, 2:53 AM
Sanjana
Sanjana - avatar
0
Lisa is there any way I can convert the first and the third element of the string to an integer , I tried type casting but I realised it also gives the ASCII value of the character
14th Oct 2024, 11:20 AM
Sanjana
Sanjana - avatar
0
Brian thanks for such wonderful explanation!! but my doubt is in example you have given scanf statement takes in two more inputs right? how does it consider elements of the string?
14th Oct 2024, 4:45 PM
Sanjana
Sanjana - avatar
0
Sanjana my second example is incomplete. I left it open with an ellipsis for you to fill in whichever four integer variable names you would use to store the four values. BTW I noticed the task description shows surrounding quotes around the inputs in order to indicate strings, but the actual test cases are without quotes. Test case 1, for instance, is: 16,10 17,10
14th Oct 2024, 5:29 PM
Brian
Brian - avatar