Programming IN C
Chapter 1
Introduction to Computer Programming
Objectives
Programming Languages
Algorithms
The Software Development Process
Case Study: Design and Development
Common Programming Errors
2
Programming Languages
Computer program: data and instructions used to
operate a computer and produce a specific result
A program or set of programs is called software
Programming: writing instructions in a language that
the computer can respond to and that other
programmers can understand
Programming language: set of instructions that can be
used to construct a program
3
Machine Language
Executable program: program that can operate a
computer
Executable programs are written with binary numbers,
which is a computer’s internal language (machine
language)
An example of a simple machine language program
containing two instructions is:
11000000000000000001000000000010
11110000000000000010000000000011
Opcode is short for operation code; tells the computer
the operation to be performed
4
Assembly Language
Assembly language: uses the substitution of word-like
symbols for the opcodes, and decimal numbers and
labels for memory addresses
LOAD first
ADD second
MUL factor
STORE answer
5
Assembly Language (continued)
6
Low- and High-Level Languages
Machine and assembly languages are low-level languages
because they both use instructions that are directly tied to one
type of computer
Programs written in a computer language are referred to as
source programs and source code
When each statement in a high-level source program is
translated individually and executed immediately upon
translation, the programming language is called an interpreted
language
Interpreter: program that translates each statement in a high-
level source program and executes it immediately upon
translation
7
Low- and High-Level Languages
(continued)
Compiled language: the statements in a high-level source program
are translated as a complete unit before any individual statement is
executed
Compiler: translates a high-level source program as a complete
unit before any statement is executed
The output produced by the compiler is called an
object program (machine language version of the
source code)
Linker: combines additional machine language code with the
object program to create a final executable program
8
Low- and High-Level Languages
(continued)
9
Procedural and Object-Oriented
Languages
Procedural language: instructions are used to create
self-contained units, called procedures
Procedure: accepts data as input and transforms it in some
manner to produce a specific result as output
Also called function or method
Procedures conforming to structure guidelines are
known as structured procedures
10
Procedural and Object-Oriented
Languages (continued)
Structured language: high-level procedural language
(e.g., C) that enforces structured procedures
Object-oriented languages: languages with object
orientation such as C++, Java, Visual Basic, and C#
11
Procedural and Object-Oriented
Languages (continued)
12
Application and System Software
Application software: programs written to perform
particular tasks required by users
System software: collection of programs that must be
readily available to any computer system to enable
the computer to operate
The bootstrap loader is internally contained in ROM
and is a permanent, automatically executed
component of the computer’s system software
13
Application and System Software
(continued)
Operating system: set of system programs used to
operate and control a computer
Multiuser system: handles multiple users
concurrently
Operating systems that permit each user to run
multiple programs are referred to as both
multiprogrammed and multitasking systems
14
The Development of C
Developed in the 1970s at AT&T Bell Laboratories by K.
Thompson, D. Ritchie, and B. Kernighan
High-level structured language
Can also access the internal hardware of a computer
C permits a programmer to “see into” a computer’s
memory and directly alter data stored in it
Standard maintained by the American National Standards
Institute (ANSI)
In the 1980s, Bjarne Stroustrup (working at AT&T)
developed C++
C with object-oriented capabilities
15
Algorithms
Algorithm: specific steps required to produce a
desired result
Set n equal to 100
Set a equal to 1
Set b equal to 100
Calculate sum = n(a+ b)/2
Display the sum
When English phrases are used to describe an
algorithm, the description is called pseudocode
Input the three numbers into the computer
Calculate the average by adding the numbers and dividing the sum by three
Display the average
16
Algorithms (continued)
17
Algorithms (continued)
When mathematical equations are used to describe an
algorithm, the description is called a formula
Flowchart: provides a pictorial representation of an
algorithm using specifically defined shapes
18
Algorithms
(continued)
19
Algorithms (continued)
20
Algorithms (continued)
Converting an algorithm into a computer program,
using a language such as C, is called coding the
algorithm
The program instructions resulting from coding an
algorithm are called program code, or simply code
21
Algorithms (continued)
22
The Software Development Process
Each field of study has a name for the systematic
method used to design solutions to problems
In science: called the scientific method
In engineering: called the systems approach
The technique used by professional software
developers for understanding the problem that is
being solved and for creating an effective and
appropriate software solution is called the software
development process.
23
The Software Development Process
(continued)
24
Phase I: Specify the Program’s
Requirements
Begins with a problem statement or a specific request
for a program, which is called a program requirement
Suppose you receive an e-mail from your supervisor
that says: We need a program to provide information about
circles
This is not a clearly defined requirement
To clarify and define the problem statement, your first step
would be to contact your supervisor to define exactly what
information is to be produced (its outputs) and what data is
to be provided (the inputs)
25
Phase II: Design and Development
Step 1: Analyze the problem. You must understand:
The outputs that must be produced
The input data required to create the desired outputs
The formulas relating the inputs to the outputs
Step 2: Select an overall solution algorithm
26
Phase II: Design and Development
(continued)
27
Phase II: Design and Development
(continued)
For larger programs you will have to refine the initial
algorithm and organize it into smaller algorithms,
with specifications for how these smaller algorithms
will interface with each other
First-level structure diagram for an algorithm is the first
attempt at a structure for a solution algorithm
Top-down algorithm development starts at the topmost
level and proceeds to develop more and more detailed
algorithms as it proceeds to the final set of algorithms
28
Phase II: Design and Development
(continued)
29
Phase II: Design and Development
(continued)
30
Phase II: Design and Development
(continued)
Step 3: Write the program (or code the algorithm)
Sequence structure defines the order in which instructions
are executed by the program
Selection structure provides the capability to make a choice
between different instructions, depending on the result of
some condition
Repetition structure, also called looping or iteration,
provides the ability for the same operation to be repeated
based on the value of a condition
Invocation structure involves invoking specific sections of
code as they are needed (ie. call function)
31
Phase II: Design and Development
(continued)
Step 4: Test and correct the program
A program error is called a bug
Testing attempts to ensure that a program works correctly
and produces meaningful results
If you find an error, initiate debugging: locating, correcting,
and verifying the correction
Develop a set of test data that determines whether the
program gives correct answers
The tests should examine every possible situation under
which a program will be used
32
Phase III: Documentation
Six documents for every problem solution:
1. The requirements statement
2. A description of the algorithms that were coded
3. Comments within the code itself
4. A description of modification and changes made over time
5. Sample test runs, which include the inputs used for each run
and the output obtained from the run
6. A user’s manual, which is a detailed explanation of how to
use the program
33
Phase IV: Maintenance
How easily a program can be maintained (corrected,
modified, or enhanced) is related to the ease with which
the program can be read and understood
34
Phase IV: Maintenance (continued)
35
Backup
Making and keeping backup copies of your work
when writing a program is critical
Not part of the formal software development process
Backup is unimportant if you don’t mind starting all
over again
Many organizations keep at least one backup on site
where it can be easily retrieved, and another backup
copy either in a fireproof safe or at a remote location
36
Case Study: Design and Development
The circumference, C, of a circle is given by the formula C =
2r, where is the constant 3.1416, and r is the radius of
the circle. Using this information, write a C program to
calculate the circumference of a circle that has a 2-inch
radius.
Step 1: Analyze the problem
Determine the desired outputs
Determine the input items
List the formulas relating the inputs to the outputs
Perform a hand calculation
37
Case Study: Design and Development
(continued)
Step 2: Select an overall solution algorithm
Set the radius value to 2
Calculate the circumference, C, using the formula C = 2 r
Display the calculated value for C
Step 3: Write the program (see next slide)
Step 4: Test and correct the program
The circumference of the circle is 12.566400
Because only one calculation is performed by the program,
testing Program 1.1 really means verifying that the single
output is correct
38
Case Study: Design and Development
(continued)
39
Common Programming Errors
Rushing to write and execute a program without
spending sufficient time learning about the problem
or designing an appropriate algorithm
Forgetting to back up a program
Not understanding that computers respond only to
explicitly defined algorithms
40
Summary
The programs used to operate a computer are referred to
as software
Programming languages come in a variety of forms and
types
Compiler and interpreter languages are referred to as
high-level languages
41
Summary (continued)
Algorithm: step-by-step sequence of instructions that
must terminate and describes how to perform an
operation to produce a desired output
The software development procedure consists of the
following four phases:
Specification of the program’s requirements
Design and development
Documentation
Maintenance
42
Summary (continued)
Steps of the design and development phase are:
Analyze the problem
Select an overall solution algorithm
Write the program
Test and correct the program
Writing a program consists of translating the solution
algorithm into a computer language
Fundamental programming control structures
Sequence, selection, iteration and invocation
You always need at least one backup of a program
43