CS102 Computer Programming I: Problem-Solving, Programs and Programming Languages
CS102 Computer Programming I: Problem-Solving, Programs and Programming Languages
Computer Programming I
Lecture 1:
Problem-Solving, Programs and
Programming Languages
Algorithm Recipe
Test: 234784832792543
An alternate algorithm:
If the rightmost digit is 0, 2, 4, 6, or 8, write “even”
Otherwise, write “odd”
Next, a C Program
Now that have an algorithm, we would
like to write a C program to carry it
out.
Main
mouse
Memory Keyboard
Network
The actual program has LOTS of details – IGNORE THEM FOR NOW
Pay attention to the main ideas
The Program in C (part I)
/* read a number and report whether it is even or odd */
#include <stdio.h>
execution if (rem == 0) {
printf(“even\n”);
} else {
printf(“odd\n”);
}
that programmers be
able to understand old
code - good comments
are essential.
Variables
Variable declarations /* read a number … */
#include <stdio.h>
of your programs }
return 0;
From C to Machine
Language
The computer’s processor only understands
programs written in its own machine language
Sequences of 1’s and 0’s
Different for each processor family (x86, PowerPC, SPARC, ARM, …)
object
source code
code
What Could Possibly Go Wrong?
Lots!
Things are rarely perfect on the first attempt
Symbolic languages (assembly languages)
High level languages – FORTRAN, COBOL, C, C++,
Java, Pascal
Natural languages such as English or French (still quite
limited)
Computer Languages
Machine languages
• Strings of numbers giving machine specific instructions.
• Example: 11000100 11111000 00001000
Assembly languages
• English-like abbreviations representing elementary computer
operations (translated via assemblers).
• Example: LOAD BASEPAY
» ADD OVERPAY
» STORE GROSSPAY
High level languages – FORTRAN, COBOL, C, C++, Java, Pascal
• Codes similar to everyday English
• Use mathematical notations (translated via compilers)
• Example: grossPay = basePay + overTimePay
Programming Language
The instructions in a programming language
reflect the operations a computer can perform:
A computer can transfer data from one place to another.
A computer can input data from an input device (a
keyboard or a mouse, for example) and output data to
an output device (a screen, for example).
A computer can store data into and retrieve data from its
memory and secondary storage.
A computer can compare two data values for equality or
inequality.
A computer can perform arithmetic operations (addition
and subtraction, for example) very quickly.
Programming Language
Programming languages require that we use certain control
structures to express algorithms as programs.
Edsger Dijkstra proposed in 1968 that any program can be written
with only three constructs or types of instructions: (1) sequences,
(2) the if...else selection statement, and (3) the while loop.
There are four basic ways of structuring statements (instructions) in
most programming languages: sequentially, conditionally,
repetitively, and with subprograms.
• A sequence is a series of statements that are executed one after another.
• Selection (also called branch or decision), the conditional control
structure, executes different repeats statements while certain conditions
are met.
• The repetitive control structure, the loop (also called repetition or
iteration), repeats statements until certain conditions are met.
• The subprogram (also called procedure, function, method, or
subroutine) allows us to structure a program by braking it into a smaller
units.
Writing, Editing, Compiling, and
Linking Programs
Writing and editing programs
text editor, source file
Compiling programs
Compiler translates the source code into
machine language. The C/C++ compiler is
two separate programs:
• Preprocessor
• preprocessor directives
• the result of preprocessing is
called translation unit.
• Translator reads the translation unit
and writes the resulting object
module.
• Linking programs
• The linker assembles all functions
into a final executable program.
Program Execution
End of Lecture 1