What is a Computer?
Computer
Device capable of performing computations and making logical decisions Computers process data under the control of sets of instructions called computer programs
Hardware
Various devices comprising a computer Keyboard, screen, mouse, disks, memory, CD-ROM, and processing units
Software
Programs that run on a computer
Introduction to Programming
A program is a set of step by step instructions which a computer follows to process data into useful information. Eg. To calculate the total marks of a student: Obtain Exam marks Obtain Coursework marks Total marks = Exam Marks + coursework marks Display Total Marks
SDLC (1)
Analysis
Decide what the project is supposed to do Do not think about how the program will accomplish tasks At the end of this process, we get a requirements document that
Describes what program will do once completed
Design
Plan how to implement the system Discover structures that underlie problem to be solved
SDLC (2)
Implementation
Write and compile the code Code implements classes and methods discovered in the design phase At the end of the process we will obtain the completed program
Testing
Run tests to verify the program works correctly
Deployment
Users install program Users use program for its intended purpose
Expected output
Expected input
Examples:
Input: An integer value N Output: Is the number prime? Input: A list of names of people Output: The same list sorted alphabetically
Algorithm Correctness
For any algorithm, we must prove that it always returns the desired output for all legal instances of the problem.
For sorting, this means even if
1. the input is already sorted, or 2. it contains repeated elements.
Properties of algorithms
Correctness Composed of a series of concrete steps No ambiguity as to next step Composed of a finite number of steps Will terminate
Examples of algorithms
shampoo instructions
Lather. Rinse. Repeat. Does not terminate
No:
If not an algorithm, then what?
Procedure Heuristic
13
END
14
Pseudocode
READ name, hoursWorked, payRate gross = hoursWorked * payRate WRITE name, hoursWorked, gross
Pseudocode
READ name, hoursWorked, payRate gross = hoursWorked * payRate WRITE name, hoursWorked, gross
Selection:
Indent statements that fall inside selection structure, but not the keywords that form the selection
Loop:
Indent statements that fall inside the loop but not keywords that form the loop
READ name, grossPay, taxes IF taxes > 0 net = grossPay taxes ELSE net = grossPay ENDIF WRITE name, net
See the IF/ELSE/ENDIF as constructed above, the ENDIF is in line with the IF. The same applies for WHILE/ENDWHILE etc
Language Independence
Resist the urge to write in whatever language you are most comfortable with, in the long run you will save time. Remember you are describing a logic plan to develop a program, you are not programming!
interestRate = .06
interestRate = .10
Pseudocode
WHILE / ENDWHILE
Start
count = 0 WHILE count < 10 ADD 1 to count WRITE count ENDWHILE WRITE The End
count = 0
Mainline count = 0 WHILE count < 10 DO Process ENDWHILE WRITE The End Process ADD 1 to count WRITE count
Modular
add 1 to count
write count
Stop
Pseudocode Advantages
Easily modified Implements structured concepts Done easily on Word Processor
30
2. Assembly languages
English-like abbreviations representing elementary computer operations (translated via assemblers) Example:
LOAD ADD STORE BASEPAY OVERPAY GROSSPAY
31
32
Coding programs
A program is usually written in a high (or mid) level programming language. Well use C as our programming Language. A program written in C has to be entered in the computer using an editor. A C program has to be translated into machine language so that the computer can understand it. The process of translating a program from high-level language to machine-language is known as compiling. For the Compiler, well use the gnu C Compiler.
33
Writing C Programs(1)
A programmer uses a text editor to create or modify files containing C code. Code is also known as source code. A file containing source code is called a source file. After a C source file has been created, the programmer must invoke the C compiler before the program can be executed (run).
A Simple C Program
/* Filename: hello.c Author: Brian Kernighan & Dennis Ritchie Date written: ?/?/1978 Description: This program prints the greeting Hello, World! */ #include <stdio.h> int main ( void ) { printf ( Hello, World!\n ) ; return 0 ; }
Anatomy of a C Program
program header comment preprocessor directives (if any) int main ( void ) { statement(s) return 0 ; }
Preprocessor Directives
Lines that begin with a # in column 1 are called preprocessor directives (commands). Example: the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code. This header file was included because it contains information about the printf ( ) function that is used in this program.
stdio.h
When we write our programs, there are libraries of functions to help us so that we do not have to write the same code over and over. Some of the functions are very complex and long. Not having to write them ourselves make it easier and faster to write programs. Using the functions will also make it easier to learn to program!
return 0 ;
Because function main() returns an integer value, there must be a statement that indicates what this value is. The statement
return 0 ;
indicates that main() returns a value of zero to the operating system. A value of 0 indicates that the program successfully terminated execution. Do not worry about this concept now. Just remember to use the statement.