Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
That is, in a goal-directed backward chaining, top-down manner.
We will use abstraction and some knowledge of the programming language being used. In this case, C.
2. String variables for literals.
To get started, note that the following program shows how to declare a string variable that will be assigned a string literal. That is, it will not work with string input nor dynamic strings.
Here is the C code.
Here is the output of the C code.
3. Output
Let use assume we know nothing about the problem being solved.
Here are three inputs and outputs for a given program.
The customer-facing output is the place to start. This is the goal so we start with the outputs. In the following, the differences in the output are underlined.
4. Input and output #1
Input #1:
3 4 5 blue
Output #1:
Enter three sides of a triangle as positive integers and the color of the triangle.
You entered sides of length 3, 4 and 5 with color blue.
The blue triangle with sides 3, 4 and 5 is a right triangle.
5. Input and output #2
Input #2:
11 14 5 green
Output #2:
Enter three sides of a triangle as positive integers and the color of the triangle.
You entered sides of length 11, 14 and 5 with color green.
The green triangle with sides 11, 14 and 5 is not a right triangle.
6. Input and output #3
Input #3:
12 13 5 red
Output #3:
Enter three sides of a triangle as positive integers and the color of the triangle.
You entered sides of length 12, 13 and 5 with color red.
The red triangle with sides 12, 13 and 5 is a right triangle.
7. Start
To get started, Let use combine all three outputs into one program - not very useful as the program has exactly the same output every time it is run.
An extra print statement is between each group for visual separation in the output.
Here is the C code.
Here is the output of the C code.
Here is the C code.
Here is the output of the C code.
8. Observations
Note the following observations.
The code abstraction into similarities and differences can be done without any understanding of the actual problem being solved. Thus, it can be done using a computer program.
After the abstraction, the differences, to be inserted, can be given more meaningful names than i1, i2, i3, s1 and s2. This is easier for a human to do than a computer.
Since variables i1, i2, i3, and s1 appear in the input, the crucial part of the program is how one goes from these variable values to the unknown (in the input) variable s2. The key in this respect is the rule for right triangles where the color is irrelevant to determining whether a triangle is a right triangle. (The computer may have a difficult time determining this from the observed input and output).