The process of problem solving

The four stages of problem solving are listed below:

  1. Analyze: Understand the available resources and constraints of a problem.
  2. Plan: Write the steps that solve the problem in the form of a program. This is the plan of action that will solve the problem.
  3. Implement: Execute a computer program to obtain the results.
  4. Evaluate: Match the results of the program with the desired output to determine success.

Simple language

One of the rules for good writing is to not repeat words. To ensure the readers enjoy what we write, we may use words with similar meanings instead of repeating them. For example, we may use any of the following similar but distinct phrases that mean roughly the same thing:

  • Display on the screen
  • Show on the monitor
  • Print on the display unit
  • Display
  • Show
  • Print
  • Output on the screen
  • Send to the screen
  • Write to the console

However, we don’t want this type of fancy writing with multiple variations in our programs. We should select one simple way of saying something and use it the same way everywhere, even if we use it ten times in a single program.

Let’s say we pick “print” and use it to mean that we want to show something on the screen. We call this an instruction in the programming language. Similarly, we use “input” to mean that we want input from the user. We only use lowercase for these commands. Using a standardized vocabulary will help others understand our programs.

We’ll create a few simple programs using the above-mentioned simple programming language. Keep in mind that all programs are sequential and follow the instructions one by one in a sequence. We’ll also demonstrate the implementation of the programs with the help of the execution sheets.

Example programs

Let’s look at some example programs, starting simply and adding complexity.

Print the product

First, let’s create a program to display the product of 55 and 77. This program has two steps—finding the product using the multiplication operator * and displaying the resultant answer.

prod = 5 * 7
print prod 

The program above is expected to display 35. In this program, prod is the name of the variable that holds the result of 5 * 7. When we use prod with the print command, the value will be displayed. Therefore, print prod will display the value of the prod.

Note: Remember that we’re not using Ruby syntax here. The word print is just a simple English word.

Label the product

Next, let’s create a program to display the product of 55 and 77 along with the text label "product:" written before the resulting prod.

prod = 5 * 7
print "product:"
print prod

The program above is expected to display product: 35. Notice that text written in double quotes will display as text in the output. Therefore, "product:" will be displayed before the value of the variable prod. This "product:" text is called a label.

More variables

Next, let’s create a program that displays the product of two values stored as separate variables.

a = 5
b = 7
prod = a * b
print "product:"
print prod

The program above is expected to display the product of two numbers stored in separate variables. Here, a and b are variables with values 5 and 7, respectively. In the third line, their product is stored in another variable, prod. So, the output product: 35 is displayed.

Interaction with the user

Now, let’s create a program that displays the sum of two input values.

input a
input b
sum = a + b
print "sum:"
print sum

This program is expected to display the sum of two numbers input by the user. In the first line, input is used to take input from the user and store it in the a variable. In the second line, input is used to take input from the user and store it in the b variable. The output of this program will change according to the input values. If the user enters 5 and 7, the output sum: 12 is displayed.

Prompt the user

Finally, let’s create a program that displays the difference of two values input by the user with prompt messages.

print "Please enter the first integer value:"
input a
print "Please enter the second integer value:"
input b
diff = a - b
print "difference:"
print diff

The program above is expected to display a prompt asking the user to enter an integer. After receiving the first input from the user, it shows another prompt to the user for the second input. Then, it shows the difference of the two numbers input by the user.

The text displayed before the input command is the prompt for the input of the user. The output of this program will change according to the input values. If we use 5 and 7, the output difference: -2 is displayed.

Execution sheet

A program is just a plan of action to solve a problem. The execution of the program follows its instructions step-by-step. It involves observing and updating the values of the variablesThe variable is a quantity that may change within the context of a problem or solution. at each step of the execution. The execution sheet is a simple tool to visualize what happens behind the scenes as the program is implemented.

Structure of the execution sheet

The execution sheet takes the form of a table with rows and columns. The program instructions are listed by rows on the sheet. Each row contains one of the program’s instructions. The columns of the sheet change depending on the variables in the program.

  1. Program sequence (PS#): This column shows the sequence number of each instruction in the program.

  2. Instruction: This column contains the actual instructions.

  3. Input/Output: This column shows the resulting output of the print instruction in the corresponding row.

  4. Variables: If there is only one variable in the program, we’ll have a column labeled with its name to show how its value changes across the program. If there are three variables in a program, there will be three columns. The portion shaded in gray indicates the nonexistence of the respective variable. The value of the variable remains unchanged unless it is modified by an instruction. The background color is used to quickly identify the instruction modifying the respective variable.

Sample problem

Let’s start with a sample problem to understand the execution sheet and the process of filling it. We want to display the sum and difference of two numbers input by the user.

Sample execution sheet

To solve this problem, here’s how we’ll fill the execution sheet:

  1. Write the line number of each instruction in the first column.
  2. Write instructions for the program in the second column, one instruction per row in order of execution.
  3. Provide one column for “input/output” and four columns for the variables a, b, sum, and diff.

Filling the execution sheet

  1. The first instruction, input a, asks the user to input the value of a. Let’s say we input 50. We also need to write it in the third column as “Input/Output” and in the fourth column as a new value of the variable a.
  2. The second instruction, input b, asks the user to input the value of b. Let’s say we input 35 here. We also need to note it in the third column as “Input/Output” and in the fifth column as a new value of the variable b.
  3. The third instruction, sum = a + b, calculates the sum of 50 and 35 to store 85 in the variable sum. Write this down in the sixth column as a new value of the variable sum.
  4. The fourth instruction, diff = a - b, calculates the answer of 50 - 35 to store the difference 15 in the variable diff. Write this in the last column as a new value of the variable diff.
  5. The fifth instruction, print sum, displays the latest value in the “sum” column, so we note 85 in the third column as “Input/Output”.
  6. The last instruction, print diff, displays the latest value in the “diff” column, so we note 15 in the third column as “Input/Output”.
Let us practice the above execution sheet with various input pairs.
First, type two integers in the input boxes below and press the 'Show' button to print the execution sheet.

For example,
When you type:

50
35
Then the execution sheet will be displayed.

Execution sheet for example programs

Let’s start typing our example programs into the execution sheet.

Product with the label

Let’s create a program to display the product of 5 and 7 along with the label "product:" written before the resulting value.

Use more variables

Let’s create another program to display the product of two values stored in separate variables.

Find the unit rate

Now, let’s create a program to find the unit rate from a total bill amount and quantity of units provided by the user.

In the execution sheet above:

  1. The first instruction, print "Please enter the bill amount:", shows a prompt for the user.

  2. The second instruction, input amount, asks the user to input the value to store in the amount variable. Let’s say we input 300 so we write it in the third column as “Input/Output” and in the fourth column as a new value of the variable amount.

  3. The third instruction, print "Please enter the quantity:", shows the second prompt to the user.

  4. The fourth instruction, input quantity, asks the user to input the value of the variable quantity by showing the given prompt. Let’s say we input 6 here so we write it in the third column as “Input/Output” and in the fifth column as a new value of the variable quantity.

  5. The fifth instruction, rate = amount / quantity, calculates the quotient of 300 / 6 and stores 50 in the variable rate. We note it in the sixth column as a new value of the variable rate.

  6. The sixth instruction, print "Unit rate is:", displays the label.

  7. The last instruction displays the latest value in the “rate” column. So, we write 50 in the third column as “Input/Output”.