+ 1
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
8 Antworten
+ 3
you are trying to multiply strings. you can only multiply numbers
+ 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!
+ 1
Can you please show your attempt by pasting your code in the code playground and attach it here
+ 1
Show your code. Mention the task name.
Your link will work only for you
+ 1
#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
+ 1
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
+ 1
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?
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