Teaching_programming_through_problem_solving
Teaching_programming_through_problem_solving
Abstract—In this short paper, we advocate the importance of proper exposure to programming in high school (and this is
problem solving for teaching “Introduction to Programming”, the majority of our students). Thus, this goal translates to:
instead of merely teaching the syntax and semantics of a
• a quick educational introduction to self-evident program-
programming language. We focus on the role of the programming
language used for an introductory course. For this purpose we ming concepts and tools, without cryptic, hardware-
propose C AL, a C-like algorithmic language, which is essentially dependent and special purpose structures; but also
a well-defined and behaved subset of C with a small number of • fluency in a programming language which can be easily
modest, “educational” extensions. We present the design rationale extended and/or modified to a language that is currently
for C AL, its main features, syntax and illustrative examples.
useful in practice, without a total rethinking of the basic
I. I NTRODUCTION algorithmic techniques.
N THIS short paper, we present our experiences with teach- Our historic prototype for a self-evident educational program-
I ing programming through problem solving in the School of
Electrical and Computer Engineering of the National Technical
ming language is of course Pascal [10], whereas programming
languages that are currently useful in practice are of course C
University of Athens. We focus on the role of the programming [7], [5], C++ [9] and Java [2].
language for this purpose and describe the approach that we II. T HE ROLE OF THE L ANGUAGE
have taken. Let us begin with two observations: Since the 1950s, scores of different programming languages
1) In some students’ minds, algorithmic programming is have been designed and implemented and many more are yet
strangely enough an intellectual process that is not to come. Those with a relative experience in the field will agree
connected to everyday problem solving. that there is no such thing as “the best programming language”
2) Students often have no sense of what is good and what and this is what we need to explain to our students early
is bad in programming, even after taking a number of on. Some languages are better than others for some specific
courses on the design of algorithms and complexity. purpose and indeed: (a) some specific languages are almost
We believe that these are both due to the way we teach exclusively used for some specific purposes, and (b) for some
students how to program. Starting from secondary school, specific purposes people use almost exclusively some specific
students often begin by learning a Pascal-like programming languages.
language. They learn to use variables, assignments, control- For example, C [7], [5] is a very good language for systems
flow statements, arrays. They do not learn, however, because programming. It is a low-level language, offering programmers
we do not teach them early enough, what these should be the opportunity to directly interact with the hardware, but not
used for! Young children realize early that they need to solve the best language for numerical and scientific computing today.
problems; they are hungry and they want their parents to feed We believe that C is an inappropriate language for teaching
them, they want to play with that shiny car in the toy shop’s “Introduction to Programming”. Some of its characteristics are
window, etc. Later on, they learn to speak and use the language so low-level that tend to focus on the hardware, instead of
to communicate their needs. Children learn to speak after they on the algorithms. When you start learning how to program,
know what they want to say! Why do we teach programming you don’t need twelve different types for integer numbers
languages to students before they know what to use them for? (including characters and Boolean values) and three more for
The main goal of our proposal is a quick introduction to real (floating-point) numbers. You don’t need a for statement
programming for absolute beginners. Such an introduction so powerful that you can use it to implement a binary search
would be useful for teenagers in high-school, who may not algorithm in just one line. You don’t need to struggle to un-
continue to be programming specialists but who want to derstand the meaning of x = x++; (most people, including
support their literacy in mathematics by hands-on attractive some who teach C, think that it does something although
algorithmically solvable problems. It would also be useful to opinions vary when it comes to what exactly this is; the truth is
first-year university students who have (somehow) escaped a that this statement is illegal, or causes “undefined behaviour”
as the ANSI C standard puts it, because the value of variable III. T HE D ESIGN OF C AL
x changes twice between two successive sequence points). Disregarding some drawbacks, C is an adequate choice
Pascal [10], [6] is arguably one of the best programming for teaching “Introduction to Programming”, with emphasis
languages for teaching purposes. It is a concise, general on problem solving and algorithmic thinking. Its core is a
purpose language which supports a systematic, structured quite simple algorithmic language, easy to explain and use.
and algorithmic approach to problem solving. Programs in Moreover, it is a useful language to know, heavily used
Pascal are usually easy to read and understand with a clear in practice, either directly or indirectly, through a line of
structure that favours stepwise refinement. The language helps descendants that share a large part of its syntax and semantics
programmers to avoid programming mistakes and to be able (C++, Java, C#, etc.). A list of drawbacks:
to verify the correctness of their programs. On the other hand, • Its syntax and semantics is often cryptic and obfuscated;
Pascal is very little used today by software practitioners. e.g., allowing side-effects anywhere inside expressions.
Java and other object-oriented languages are also poor • The use of “declarators” (as in int*(f[3])(int);)
candidates for teaching “Introduction to Programming”. If the is counter-intuitive and hard to explain.
focus is on problem solving and algorithmic thinking, such • Non trivial library functions (e.g., printf and scanf)
languages add an unbearable level of noise. Using Java, the are required for beginners to write programs that input
only logical approach is to teach programming in a purely and output data. The corresponding header files must be
object-oriented fashion and this necessarily takes the focus #included. Pointers are required for scanf.
away from problem solving, although OOP might be a good • Before the simplest program is written, students must
choice for a second (e.g., data structures) course. see int main() and return 0; unless of course we
There is a trend towards Python, in the last few years. want to teach them to be sloppy from the first lecture...
Python is a relatively good candidate; its syntax is concise • The type system allows programmers to deliberately
and enforces proper indentation, it supports imperative and misuse data and to neglect declaring function prototypes.
object-oriented programming equally well, and it is widely Both are bad from an educational point of view.
used in the software industry. The drawbacks for Python are: We therefore base C AL on an appropriate “educational
(a) it is dynamically typed and requires very few declarations; subset” of C, which we extend with a number of macros,
this is bad if you want the students to detect programming library functions and one extra feature (call by reference)
errors early and to learn to program in a disciplined way; to suit the needs of our introductory course. The result is
(b) it is so high-level that students do not develop an intuition a language reminiscent of Pascal but with C notation. All
about how data are represented and how operations are im- extensions are written with uppercase letters (e.g., WRITE),
plemented; this is bad if you want the students to understand so that students immediately know if something that they have
the connection between the programming language and the learnt exists in C or is one of our educational extensions. The
underlying hardware; and (c) its data structures are so high- main characteristics of C AL, whose complete syntax is defined
level that algorithmic complexity issues are obscured by the in figure 1, are the following:
way data structures are implemented; e.g., in Python there are
• A program is organized as a set of modules, each consist-
no arrays, but there are lists and dictionaries; however, in a
ing of constant and type definitions, variable definitions,
data structure course, all three would need to be covered and
routine declarations, routine definitions and (optionally)
with three different implementations, requiring O(1), O(n)
the body of the main program, which must only be
and O(n log n) time for accessing an element, respectively.
present in one module. The visibility of module defini-
The second drawback (b) is also true for functional lan- tions is controlled with PRIVATE and extern.
guages, like Scheme, ML or Haskell, which are also very good • There are functions and procedures, defined with FUNC
from an educational point of view and are indeed used for and PROC respectively; the misleading type void is not
teaching “Introduction to Programming” in several Computer used. The main program begins with the special keyword
Science departments [8], [3], [1], [4]. PROGRAM.
All this said, we decided to design a new educational • The type system is simplified. There are types for
programming language for an introductory course in which Boolean values (bool, as in C99, with constants true
emphasis is on problem solving. However, this educational and false), integers (int), characters (char) and real
language will naturally evolve before the students’ eyes to numbers (REAL). There are also enumerations, structures
a full-scale programming language, useful later on. In the and unions, but these must be defined and given a name
next sections we describe C AL, a C-like algorithmic language, before they can be used. Arrays and pointers complete
starting from the design choices that we had to make, proceed- the picture of types. However, the syntax for declarators
ing with the syntax, the main characteristics of the language is very simplified in comparison with C; type synonyms
and concluding with a few examples.1 (typedef) can be used for defining, e.g., double point-
ers, arrays or pointers, pointers to arrays, etc.
1 An implementation of C AL , based on GCC and using macros, is available • Operators NOT, AND, OR and MOD are synonyms of C’s
from https://github.com/softlab-ntua/pazcal. (not so intuitive) standard operators !, &&, || and %.
NIKOLAOS S. PAPASPYROU, STATHIS ZACHOS: TEACHING PROGRAMMING THROUGH PROBLEM SOLVING 1535
Fig. 1. A context-free grammar defining the syntax of C AL in EBNF. Operator precedence and associativity are the same as in C.
• Assignment (simple or composite) is a statement. (Alas, • The switch statement is sanitized; case labels cannot
for compatibility purposes we have to give up the as- appear everywhere. Furthermore, clauses are required to
signment operator := and accept the commonly used =, end either with a break, or with the new keyword NEXT
which we would prefer not to confuse with the equality in order to explicitly proceed to the next clause.
operator known from mathematics.) There is just one • Four kinds of WRITE statements are used for the output
type of increment and decrement operators (postfix, e.g., of data, allowing any number of arguments of any type,
x++), used again as statements. Therefore, expression with a number of formatting options that are useful for
evaluation cannot contain direct side-effects. Also, we an introductory course. Library functions READ_INT,
omit bitwise operators and conditional expressions (?:). READ_REAL and getchar are used to input data.
• We omit the for statement, which is too general for our • The use of pointers has also been sanitized. The connec-
purposes, and replace it with a FOR statement following tion between pointers and arrays is still present, but it
the style of Pascal, mentioning the control variable and only allows the use of a pointer as an array; no pointer
a (precomputed) range of values that the control variable arithmetic is allowed and there is no “address of” operator
will take. Using FOR, the maximum number of iterations (&). Pointers are used for dynamic memory allocation;
is always finite and known before the loop starts execut- NEW and DELETE help students for this purpose (in the
ing; break and continue can be used to exit the loop spirit of C++, instead of malloc and free in C).
and proceed with the next iteration. • Call by reference is allowed, using the same notation that
1536 PROCEEDINGS OF THE FEDCSIS. KRAKÓW, 2013