Balconies Problem in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Balconies Problem in C

Hey everyone, I attempted this problem and solved up to an extent, but only last two cases are wrong, please check. Main reason is String to int (atoi) #include <stdio.h> #include<string.h> int main() { int A, B; char x[10], y[10], w[10], l[10]; scanf("%s %s", x, y); scanf("%s %s", w, l); A=x*y; B=w*l; if(A>B) { printf("Apartment A", x, y); } else { printf("Apartment B", w, l); } return 0; } https://sololearn.com/coach/24/?ref=app

25th Nov 2022, 9:44 AM
Wakil Ah Hamidi
Wakil Ah Hamidi - avatar
7 Answers
+ 2
#include <stdio.h> #include<stdlib.h> int main() { int A, B; char x[10], y[10], w[10], l[10]; scanf("%[^,],%s", x, y); // this reads first until comma into x, then skips comma, then remaining into y. // Similarly next : scanf("%[^,],%s", w, l); A=atoi(x) * atoi(y); // convert both to int and multiply.. B=atoi(w) * atoi(l); if(A>B) { printf("Apartment A"); } else { printf("Apartment B"); } return 0; } // you can also read entire string and split with comma by strtok( x, ",") ...
25th Nov 2022, 3:01 PM
Jayakrishna 🇮🇳
+ 1
Statement are evaluated from top to bottom, and left to right. So calculating A, B before taking values for x, y, w, l are result with garbage values.. First take input then use values in calculations. Also strings are comma separated. Your input accept words with space separated. So 2 inputs are gathered into x, w only.. edit: Wakil Ahmad Hamidi oh.. variables A, B, x, y, l, w are declared as int type but using %s in scanf will read it as string type. you cant store it int variables.
25th Nov 2022, 10:21 AM
Jayakrishna 🇮🇳
+ 1
%s reads a word only, until a space encounters. So your first scanf reads 2 inputs into x, y. Not as comma separated words. w, l will be empty strings.. So you need to split x, y by delimeter comma, and separate values. Then apply convertion function. In output statements, what is the need of x, y, w, l ? You can remove those.
25th Nov 2022, 12:19 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 Yeah, you are correct, check it now, I edited
25th Nov 2022, 10:32 AM
Wakil Ah Hamidi
Wakil Ah Hamidi - avatar
0
Can you please write or share the link? Jayakrishna🇮🇳
25th Nov 2022, 2:50 PM
Wakil Ah Hamidi
Wakil Ah Hamidi - avatar
0
Correct my code, or share the link to your code for this problem, then I may get it quickly
25th Nov 2022, 2:57 PM
Wakil Ah Hamidi
Wakil Ah Hamidi - avatar
- 1
How about this? #include <stdio.h> #include<string.h> int main() { int A, B; char x[10], y[10], w[10], l[10]; scanf("%s %s", x, y); scanf("%s %s", w, l); A=atoi(x*y); B=atoi(w*l); if(A>B) { printf("Apartment A", x, y); } else { printf("Apartment B", w, l); } return 0; }
25th Nov 2022, 10:39 AM
Wakil Ah Hamidi
Wakil Ah Hamidi - avatar