0% found this document useful (0 votes)
27 views16 pages

02c. Dry-Running and Pseudocode

Uploaded by

0987987972
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views16 pages

02c. Dry-Running and Pseudocode

Uploaded by

0987987972
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Dry-Running and Pseudocode

1
Learning Programming
• Introducing two methods that will help you in
learning programming
1. Dry-run: To run a program in your brain
2. Pseudocode: An informal, usually step-by-step
description of a program, written in a human readable
form
• If you master these two methods, you will gain
superpower* in learning programming!

*Slight exaggeration

2
Dry Running
• How to best understand the program code?
– Think like a computer!
• The computer executes the C program line by line
• You can also read it line by line
– This is called a “dry run”
• You may do it on a piece of paper
• You may use NotePad/TextEdit or other ways too
• NEVER read just the starting and ending lines of code and
attempt to guess the outcome of the program!

3
Dry Running with a Trace Table
1 #include <stdio.h> Dry Run Table
2 *****************
3 int main(void) **
4 {
5 int side, perimeter, area;
6 side perimeter
7 side = 3; area
8 perimeter = 4 * side;
9 area = side * side;
10
11 printf("Side : %d\n", side); The dry run table allow us
12 to track how each variable
printf("Perimeter: %d\n", perimeter);
13 printf("Area : %d\n", area); change as we run the
14 program
15 return 0;
16 }

Example from Lecture 01b: Computing the perimeter and the area of a square. 4
Dry Running with a Trace Table
1 #include <stdio.h> Dry Run Table
2 *****************
3 int main(void) **
4 {
5 int side, perimeter, area;
6 side perimeter
7 side = 3; area
8 perimeter = 4 * side; ? ?
9 area = side * side; ?
10
11 printf("Side : %d\n", side);Variables start with
12 printf("Perimeter: %d\n", perimeter);
unknown values, before
13 printf("Area : %d\n", area);
14
we assign values to them
15 return 0;
16 }

Example from Lecture 01b: Computing the perimeter and the area of a square. 5
Dry Running with a Trace Table
1 #include <stdio.h> Dry Run Table
2 *****************
3 int main(void) **
4 {
5 int side, perimeter, area;
6 side perimeter
7 side = 3; area
8 perimeter = 4 * side; ? ?
9 area = side * side; ?
10 3
11 printf("Side : %d\n", side);We step forward as if we
12 printf("Perimeter: %d\n", perimeter);
are the computer, line by
13 printf("Area : %d\n", area);
14
line, and write down how
15 return 0; the variables will change
16 }

Example from Lecture 01b: Computing the perimeter and the area of a square. 6
Dry Running with a Trace Table
1 #include <stdio.h> Dry Run Table
2 *****************
3 int main(void) **
4 {
5 int side, perimeter, area;
6 side perimeter
7 side = 3; area
8 perimeter = 4 * side; ? ?
9 area = side * side; ?
10 3
11 printf("Side : %d\n", side);We step forward as12if we
12 printf("Perimeter: %d\n", perimeter);
are the computer, line by
13 printf("Area : %d\n", area);
14
line, and write down how
15 return 0; the variables will change
16 }

Example from Lecture 01b: Computing the perimeter and the area of a square. 7
Dry Running with a Trace
Repeat until the program is
Table
1 finished
#include <stdio.h> Dry Run Table
2  You may even add a column *****************
3 int main(void) for the printf output. Try **
4 { it!
5 int side, perimeter, area;
6 side perimeter
7 side = 3; area
8 perimeter = 4 * side; ? ?
9 area = side * side; ?
10 3
11 printf("Side : %d\n", side); 12
12 printf("Perimeter: %d\n", perimeter);
13 printf("Area : %d\n", area); 9
14
15 return 0;
16 }

Example from Lecture 01b: Computing the perimeter and the area of a square. 8
Pseudocode
• Maybe your problem is not in understanding a
program, but writing one?
– For some beginners, it is very hard to start writing C from
nothing
• Programmers may be very good at thinking like a
computer, but we are still human beings!
– As a human, we use logic and natural language, which are
largely programming language independent
• To express our logic and communicate with others,
programmers often use a human readable form of
programming called pseudocode
9
Pseudocode
• Example: I wish to write a program to calculate the sum of an
arithmetic sequence from x1 to xn
• Recall that an arithmetic sequence is a sequence of numbers with a
constant difference Δ between consecutive terms,
e.g., given initial x1 = 5; difference Δ = 3; n = 7 terms; x7 = 23 :
5, 8, 11, 14, 17, 20, 23
• We may all know that the formula is:
sum = x1 + x2 + … + xn = n * (x1 + xn) / 2
where n is the number of terms in the sequence
• In the above example, the sum of the sequence is
7 * (5 + 23) / 2 = 98
10
Pseudocode
• Let's attempt to write the program in pseudocode
• There's no restriction in pseudocode syntax, but it
should "look like" your target language, i.e. C
• For example, we may write the pseudocode like
this. Notice how readable it is?
1. Declare integers x1, xn, n
2. Ask for values of x1, xn, n from user (assume valid)
3. Declare integer sum
4. Assign sum to be n * (x1 + xn) / 2
5. Print out sum
11
Pseudocode
1 #include <stdio.h>
2 int main(void)
3 {
4 // declare integers x1, xn, n
5 // ask for values of x1, xn, n from user
6 // declare integer sum
7 // sum = n * (x1+xn) / 2
8 // print sum
9 A good trick is to write down your pseudocode as comments into
10 return 0; your program first.
11 }
12 Notice that your pseudocode should be separated into multiple
13 statements, which could be executed step by step like a computer
program, to achieve our objective (hopefully).
14
15 We use terminology similar to, but not exactly same as, features of C,
16 such as declaring integers (implying integer variable), and printing
(implying printf).
12
Pseudocode
1 #include <stdio.h>
2 int main(void)
3 {
4 // declare integers x1, xn, n
5 int x1,xn,n;
6 // ask for values of x1, xn, n from user
7 scanf("%d%d%d", &x1, &xn, &n);
8 // declare integer sum
9 I can then "translate" each line of my
int sum;
pseudocode and insert one or more
10 // sum = (n * (x1+xn))/2 valid C statement(s) under each.
11 sum = n * (x1+xn) / 2;
12 // print sum It is not necessary a one-to-one
13 printf("Sum = %d\n", sum); translation, but the order of each
14 statement of your pseudocode must be
15 return 0; preserved.
16 }
Do you think the program is correct?
13
Pseudocode
// declare integers x1, xn, n
// ask for values of x1, xn, n from user
// declare integer sum
// sum = n * (x1+xn) / 2
// print sum
Pseudocode is very flexible. Here, both A and B are valid
A pseudocode for the sample program, but B is more concise, and
is targeted towards more mature programmers

// ask for integers x1, xn, n from user


// declare integer sum = n * (x1+xn) / 2
// print sum

B In B, the first step clearly implies you need to declare x1, xn and
n as integers first. In cases where the pseudocode is very concise
or ambiguous, it is up to the programmer to use their knowledge
and be smart!
14
Pseudocode
• If you are a beginner programmer and you feel
afraid to write in C
– Write in pseudocode as comments first
• If you face a hard programming problem of C
– It is also a good idea to write in pseudocode so that
you can put your thinking "on paper"
– You can even discuss with your classmate on its
correctness, and he/she will understand your logic
better than through a C program

15
Pseudocode in Lab Exercises
• In your Lab Exercises, sometimes we will give you
the correct pseudocode as a hint to the problems!
– Yay! It's almost like cheating!
– As long as you can "translate" from the pseudocode to
C correctly, your program will be correct!
– However, as a beginner, you might sometimes translate
the pseudocode into C incorrectly
• In such a case, please make sure you check your C syntax and
see if you understand how to express your logic in C properly
– Good luck!

16

You might also like