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.

Although many people today do not fully accept the relationship between mathematics and coding, beginner level to perform coding Basic knowledge of mathematics is required. In this way, what you write, write or write understand and make the codes understandable becomes possible.

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 tells us that this code represents a function is showing. 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, integer values represents the data type. First of all, after specifying the data type of our variable in the code, this we give a name to our variable. 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. Instead of writing repetitive transactions line by line, we can write them in a single 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 is enclosed in parentheses and separated by semicolons. It asks for three different situations. In the first of these, our initial value we define, then for the continuation of the loop required condition we say and finally what to do in each loop we are telling. The order of spelling of these three cases certainly does not change. Let’s explain these three situations in the above question.

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.

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.

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. Here this comma is actually from the for loop shows that we want it 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 Increment ‘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.

If what we want from the program in this statement 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 cycle will handle the variable ‘b’ set 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. This expression with ‘%d’ will be an integer and this integer will be ‘a’ we express. This command line will only work when the above loop terminates, by handling the resulting values ​​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. your loop the action it will do in each cycle and its results it will be like this:

  • 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’. In this case but it happens 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. The new value of ‘b’ in the loop result is 0, 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:

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. This code If it 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 is of n(n+1)/2 = 55 solution would be. Here, the only value that satisfies the equation was 10.

Here were the responses to the question:

code question

code question

code question

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

code question

code question


source site-34