"Mathematics" task | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

"Mathematics" task

Hi all. I'm solving a "Mathematics" problem in C. I wrote the code, but I can not understand why task 4 does not work. https://code.sololearn.com/c455AXy4FVfr/?ref=app *** Find which math expression matches the answer that you are given, if you have an integer answer, and a list of math expressions. Task: Test each math expression to find the first one that matches the answer that you are given. Input Format: Two inputs: an integer and a space separated string of math expressions. The following operations need to be supported: addition +, subtraction -, multiplication *, division /. An expression can include multiple operations. Output Format: A string that tells the index of the first math expression that matches. If there are no matches, output 'none'. Sample Input: 15 (2+100) (5*3) (14+1) Sample Output: index 1 Explanation: Index counting starts at 0, so '(5*3)' is at index 1 and matches your answer of 15.

14th Dec 2022, 3:57 AM
Dmitry
Dmitry - avatar
5 Answers
+ 4
I am assuming by task 4 you're referring to the 4th expression E.g. 12 (5-4) (4*5) (5+10) (4*3) Outputs: none scanf("%s %s %s", index1, index2, index3); ^^^Here in this line, there supposed to be only three expressions else if(answer == func(index3)){ printf("index 2"); }else{ printf("none"); // here, anything other than the first 3 expressions will output none } Use loop to take input and if else won't work cause you can't add infinite if else.
14th Dec 2022, 7:05 AM
Kashyap Kumar
Kashyap Kumar - avatar
+ 2
Dmitry I tested the code with -1 {blank line} And it outputs "index 0" Not sure how it worked out that way in the code. Anyways, just wanted to let you know about it, I haven't looked deep into it ...
14th Dec 2022, 6:49 AM
Ipang
+ 2
Thanks for the help. Changed the code as suggested Kashyap Kumar. All tests passed. int count = 0; bool ind = false; while(scanf("%s", index1) == 1){ if(func(index1) == answer){ ind = true; break; } count++; } if(ind == true){ printf("index %d", count); }else{ printf("none"); }
14th Dec 2022, 9:01 AM
Dmitry
Dmitry - avatar
+ 1
Ipang Corrected the code to reflect your correction, but test 4 still fails
14th Dec 2022, 7:03 AM
Dmitry
Dmitry - avatar
0
Provide inputs and expected outputs. Your question doesn't explain the problem.
14th Dec 2022, 5:58 AM
Kashyap Kumar
Kashyap Kumar - avatar