You are on page 1of 23

ELEC 1032 Computer Programming

Chapter 1 Introduction to Computer Programming

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

Machine Language
Executable program: program that can operate a computer Executable programs are written with binary numbers, which is a computers 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
3

Assembly Language
Assembly language: uses the substitution of wordlike symbols for the opcodes, and decimal numbers and labels for memory addresses
LOAD first ADD second MUL factor STORE answer

Assembly Language (continued)

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
6

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
7

Low- and High-Level Languages (continued)

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

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#

10

Procedural and Object-Oriented Languages (continued)

11

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 computers 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

12

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
13

Algorithms (continued)

14

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

15

Algorithms (continued)

16

Algorithms (continued)

17

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

18

Algorithms (continued)

19

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
20

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
21

Case Study: Design and Development (continued)

22

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

23

You might also like