Writing a computer program is like writing a play

advertisement
Sridhar Narayan
Department of Computer Science
narayans@uncw.edu
Program vs. a recipe - 1
 A recipe describes a culinary process that
produces a dish
 Different recipes produce different dishes
 The same dish may have different recipes (variations on
a theme)
 A computer program describes a computation
process that produces one or more results
 Different programs describe different computations
 The same computation can be described by different
programs
Program vs. a recipe - 2
 A recipe is a script
 Script is written by the author of the recipe
 Author envisions the dish to be produced and creates
the corresponding recipe
 A computer program is a script
 Script written by the programmer
 Programmer envisions the computational process to be
performed and creates the appropriate program
Program vs. a recipe - 3
 A chef follows a recipe
 Chef may have some freedom to deviate from the script
and improvise
 In a computer program, the computer (i.e. CPU)
follows the program
 CPU has no freedom to deviate from the script. It
follows it exactly as written.
Program vs. a recipe - 4
 The recipe must be written in a language the chef
understands
 If the author of the recipe writes in a foreign
language, the recipe must be translated before the
chef can use it
 A computer’s native language is machine language
 If the computer program is written in a foreign
language, i.e. non machine-language, it must be
translated before it can be executed. A compiler is a
computer program that performs this translation.
Program Translation
 You can write computer programs in any language for
which such a translator exists, or can be written
 At this time, translators do not exist (nor is it known
how to write them) for natural languages, i.e. English,
Spanish, German, Russian etc.
Programming Languages
 A computer programming language is typically
English-like but is considerably more restrictive
 Like natural languages, a computer programming
language has a vocabulary and rules of grammar.
 You have to be familiar with the vocabulary and
the rules of a programming language before you
can write computer programs in it.
The C programming language
 Java, Python, Fortran, C, are all examples of
programming languages.
 In this class, you will be programming in a language
named C.
Program vs. a recipe - 5
 The recipe author may use a text editor (like Notepad)
or a word processor to write the recipe.
 Note that the word processor cannot write the recipe
on its own. However, it may help the process, for
example by providing templates for recipes.
 A programmer may use a text editor (like Notepad) to
write the computer program
 Note that the editor cannot write the program on its
own. However, it may help the process, for example by
providing templates for programs.
Program vs. a recipe - 6
 An Interactive Development Environment (IDE) like
Code::Blocks is an environment that allows a
programmer to use a single interface to conveniently:
 Write the program using an editor
 Compile it
 Debug it
 Execute it
 Revise it
Program vs. a recipe - 7
 An involved recipe may be divided into many parts.
For example, preparation the night before, preparation
right before you cook, etc.
 A C program may be divided into many functions,
each of which has a unique name.
 Execution of a C program always begins with a
function named main.
 Initially, all the programs you write will feature only
the main function. Later, you will write programs that
define other functions besides main.
Program vs. a recipe - 8
 Recipe
 Has many steps
 Chef follows the steps in sequence, starting with step 1
 Later steps often depend on the results of earlier steps
 A C program
 Has many statements
 Computer follows the statements in sequence, starting
with statement 1
 Statements that appear later in a program often depend
on the results of statements that appear earlier
Recipe example
 List of ingredients
 3 potatoes
 1 cup olive oil
 1 teaspoon salt
 Detailed description of what to do, in what order, with
those ingredients
Chop potatoes into 1 inch cubes
2. Grease baking sheet with olive oil
3. Spread potatoes evenly on baking sheet
4. Roast potatoes at 300 degrees Fahrenheit for 10 minutes
5. Season with the salt.
1.
Computer program: ingredients
 List of variables. Think of these as ingredients for
a computation.
 Each variable has a name: x, y, grade
 Each variable has a type : int, float, double
 A type specifies what values can be assigned to that
variable. For example,


int – integers, i.e. whole numbers without a fractional
component. For example, 10, 34, 23456.
float – floating point numbers, i.e. numbers that can have a
fractional component. For example, 12.2, 345.102
 Example: int a = 24;
Computer program
 A detailed description of what operations need to be
performed, in order, using the variables
A simple C program
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a, b, c;
a = 10;
b = 20;
c = a + b;
printf(“Here is the result\n”);
printf(“%i + %i = %i\n”, a, b, c);
return 0; //that says return zero
}
Download