Can You Solve This Coding Question Asked In Middle School?

The coding question that TÜBİTAK asked middle school students at the 30th Science Olympics became the agenda again. Even if you don't know any coding, we explained the solution clearly letter by letter.
 Can You Solve This Coding Question Asked In Middle School?
READING NOW Can You Solve This Coding Question Asked In Middle School?

Although many people today do not fully accept the relationship between mathematics and coding, basic mathematical knowledge is required to perform beginner-level coding. In this way, it is possible to understand and make understandable the codes that you have written, written or will write.

The latest example of this situation appeared in the 30th Science Olympics, which took place on May 21. The coding question asked to secondary schools in the Olympics organized by TÜBİTAK became the agenda again in the social media today. Let’s take a look at the question, its solution and the reactions.

Here’s the controversial basic-level coding question:

Don’t scroll the page right away! First try to understand a little and perceive what is being asked here.

So what’s the answer to this question?

In order to answer this question, you actually need a basic level of coding knowledge, which everyone should have today. The above question, written in C programming language, actually asks us about a mathematical situation. Let’s first explain the code line by line so that those who don’t know coding can understand the question.

What does main() {…} mean?:

This structure, which covers all our code, actually shows us that this code represents a function. Functions, which are the building blocks of a program, are codes that are put together to do a specific job. Variables and operations are defined within the functions, and when this function is run, a result emerges. It’s actually a function built into all programs, with the code that a program executes for the first time when it’s run.

int a = 0;

We said that we define variables inside each function. This line of code also defines a variable for us. The ‘int’ expression here refers to the data type that represents integer values. First of all, after specifying the data type of our variable in the code, we give this variable a name. Then we present the value of our variable to the program, so we say take a as 0.

for (int b = 55; b > 0; a++, b = b – a);

The answer to the question actually comes out in this line. In this line, we are making use of the ‘for(…;…;…)’ loop. Thanks to this loop, which is one of the most basic building blocks of programming, we can write repetitive operations in a single line instead of writing them line by line. Without this loop, we could write repetitive operations thousands of lines one after the other. For example, if we wanted to print numbers from 1 to 1000 on a screen, we would normally repeat each of these on each line, like “write 1, write 2, write 3, write 4, write 5…”.

The for loop wants three different cases, enclosed in parentheses and separated by semicolons. In the first of these, we define the initial value of our loop, then we tell the condition for the continuation of the loop, and finally we tell what to do in each loop. The order of spelling of these three cases certainly does not change. Let’s explain these three situations in the above question.

  • int b = 55

Here, we specify an integer type variable just like we did above and enter the name of this variable as ‘b’ and its value as 55.

  • b > 0

Here, too, we call our loop, continue this loop as long as ‘b’ is greater than 0. “Well, we just said b to 55, won’t this cycle go on forever?” You may be saying. But in the next operation, we will change the value of b in each iteration.

  • a++, b = b – a

We came to the place where the zurna calls zırt. This line shows how important punctuation is in coding. We just said that the for loop requires three states. But here we see two statements with a comma between them. This comma indicates that we actually want the for loop to perform two operations. With the comma, we can increase the number of transactions that we specify in the third case and that we want to be done.

In our first operation (a++), we say to the program that it increments the number of ‘a’ by 1 each loop. In this way, ‘a’, which is 0 in the first loop, will be valued as 1, then 2, 3, 4, 5… (In fact, we get rid of writing thousands of lines of code). But before we continue with this valuation, we want one more thing from the program: b = b – a.

What we want from the program in this expression is take ‘b’ with each loop, subtract ‘a’ from ‘b’ and assign the new number to ‘b’. So change the value of ‘b’ to ‘b-a’. When we express this in terms of the moment our cycle starts, the process will be as follows:

  • b = 55 (b) – 0 (a)
  • b = 55

This loop will continue until the last variable ‘b’ becomes 0 as a result of the operations. In each new loop, it will handle the variable ‘b’ determined in the previous loop. So in the first loop, b = 55, while in the second loop, b = 54.

printf(“%d”, a)

In this command line, we tell the program to write a statement to the console by using the ‘printf’ function. We express that this expression to be written with ‘%d’ will be an integer and this integer will be ‘a’. This command line will only work when the above loop ends, by handling the values ​​that occur as a result of the loop. So unless ‘b’ reaches 0 the program will not print anything.

We explained the code, it’s time for the solution:

Now let’s get to the solution. The operation of the loop in each loop and its results will be as follows:

  • b = 55 – 0 => b = 55, increment a by 1.
  • b = 55 – 1 => b = 54, increment a by 1.
  • b = 54 – 2 => b = 52, (a is constantly increasing)
  • b = 52 – 3 => b = 49,
  • b = 49 – 4 => b = 45….

The answer to the question, the last answer the loop will give and the last number at which it will stop the loop, will appear if b is set to 0. So what does it take to reset the ‘b’ at the end of the loop? Of course it’s equal to the number ‘a’. This also happens only when a reaches 10:

  • b = 19 – 9 => b = 10
  • b = 10 – 10 => b = 0

Our loop now ends here because we said stop when ‘b’ is 0. As a result of the loop, the new value of ‘b’ is defined as 0 and the new value of ‘a’ is defined as 10. The program now moves to the next line of code, ‘printf()’, where it writes the value ‘a’ to us:

  • Answer: 10.

Of course, it is not necessary to go through the steps of the program to do all this. That’s where math and ability to convey what you understand come into play. If this code were asked in a math exam, it would actually look like this:

  • Consecutive numbers starting at 1 add up to 55 when you include the last number in the total?

The mathematical answer to this question would be the solution of n(n+1)/2 = 55. Here, the only value that satisfies the equation was 10.

Here were the responses to the question:

  • A user of Ekşi Sözlük actually explains the importance of the problem very well as follows:

Comments
Leave a Comment

Details
242 read
okunma13149
0 comments