Dry running a program: Everything you need to know

Dry running a program: Everything you need to know

·

4 min read

Table of contents

No heading

No headings in the article.

What does it mean to dry-run a program? Generally, a dry run refers to a process used to test something to ensure a proper output or response. For example, a company may conduct a 'dry run' test on a jet's new pilot ejection seat while the jet is parked on the ground, rather than while it is in flight.

A dry run, in any programming language simply means to execute your code manually by going over each iteration, operation, and function to test it before the compilation and real execution of the program by a legit compiler.

Dry runs are usually performed for the following purposes:

  1. To mitigate the effects of possible failure of a piece of code
  2. To validate a test case and improve it before documenting its steps and assertions down.
  3. To verify that a piece of code does not contain any bugs.
  4. To identify and solve logical errors
  5. To help understand code.

Traditionally, a dry run would involve a programmer sitting down with a pen and paper and manually following the value of a variable to check that it was used and updated as expected. If the value is not what it is supposed to be, they can identify and correct the section of code that resulted in the errors. For this example, consider the following piece of code that reverses a given number:

#include <stdio.h>

int main() {

  int n, reverse = 0, remainder;

  printf("Enter an integer: ");
  scanf("%d", &n);

  while (n != 0) {
    remainder = n % 10;
    reverse = reverse * 10 + remainder;
    n /= 10;
  }

  printf("Reversed number = %d", reverse);

}

We have three variables here, n, reverse and remainder. Let's track the value of each of these via a table.

image.png

In our code, we have assigned the value of the user's input to the variable 'n'. Say that the integer we enter is '3456'. The variable n sets to 3456. Next, there is a while loop, where the condition is set to n!=0 meaning that the loop will run as long as n is not equal to zero. The variable 'remainder' is set to n%10 which in this case is 3456%10, which equals 6. The variable 'reverse' is set to reverse x 10 + remainder. 'Reverse' is initialized above to zero, hence the condition becomes 0x10+remainder. Plugging in the value of 'remainder', we get reverse = 0x10+6, which equals 6 as well. Now, for the last statement in the while loop, n/=10. The '/=' operator is a shorthand operator which can be written alternatively as n = n/10. The updated value of n becomes 3456 (the original value of n) / 10, which evaluates to 345. Putting in the values of all our variables, our table should look something like this:

image.png

The loop will not end here, however. As the value of n is 345 which satisfies the condition n!=0, the loop will run a couple more times. Since the loop runs once more, the statements inside it will run again as well. The variable 'remainder' is set to n%10, that is, 345%10, which evaluates to 5. The variable 'reverse' becomes 6 (the new value of reverse) x 10 + 'remainder', which evaluates to 60+5, that is 65. The variable n is updated to n/10, which is 345/10, which evaluates to 34.

image.png

The value of n, which is 34, is still greater than zero. Thus, the loop runs once again. The variable 'remainder' is set to n%10, that is, 34%10, which evaluates to 4. The variable 'reverse' becomes 65 (the new value of reverse) x 10 + 'remainder', which evaluates to 650+4, that is 654. The variable n is updated to n/10, that is 34/10, which evaluates to 3.

image.png

The value of n, which is 3, is still greater than zero. The loop runs one final time. The variable 'remainder' is set to n%10, that is, 3%10, which evaluates to 3. The variable 'reverse' becomes 654 (the new value of reverse) x 10 + 'remainder', which evaluates to 6540+3, that is 6543. The variable n is updated to n/10, that is 3/10, which evaluates to 0.

image.png

Now that the value of n has finally become zero, the loop ends as the condition evaluates to false. Therefore, the line after the loop, that is the print statement, will be executed. The output produced would look something like this: Reversed number = 6543.

And that was the end of this article! Hope you learned something new.