CS 115 Chapter 1
Overview of Programming and Problem Solving
Chapter 1
Every programmer needs a mental model of a computer
Every program does: input / process / output
von Neumann model - the "stored program" concept
Computer Hardware CPU - central processing unit
Where decisions are made, computations are performed, and input/output requests are delegated
Main Memory Stores information being processed by
the CPU short-term (volatile) Secondary Memory (Mass Storage)
Stores data and programs long-term
Computer Hardware Components
Arithmetic Logic Unit
Control Unit
Auxiliary StorageDevice
Memory Unit(RAM & Registers)
Central Processing Unit(CPU) Input Device
Output Device
Peripherals
Memory (RAM)
a memory cell (also called a word) has an address, has contents
Contents expressed in bits / bytes - binary code
Holds a value which may be data, may be an instruction
data retrieval = a "copy" not a "cut"
Memory Cells Address Contents
-27.2
354
0.005
-26
H
X
75.62
RTV 001
. . .
0
1
2
3
4
5
6
. . .
999
Secondary Storage files - source files, data file, output file hard disk, floppy, CD, flash memory
stick slower than RAM, and cheaper per byte usually much larger capacity than RAM Units of capacity - Kilobyte, Megabyte,
Gigabyte, Terabyte
Languages
Machine languages take a step, lift arm, grasp knob, turn
knob... Assembly languages
LEAVE through DOOR High-level languages
"get outta here!"
Programming Languages
Machine Language Most fundamental language of the
computer, the one the CPU "understands"
Unique for each processor type, so not "portable"
Binary 0s and 1s that specify what to do 0010 0000 0000 0100 1000 0000 0000 0101 0011 0000 0000 0110
A Program in Machine and Assembly Language
High Level Languages
Are portable
Programmer writes program in language similar to natural language (human)
Examples -- FORTRAN, COBOL, Pascal, Ada, Modula-2, C++, Java
Most are standardized by ISO/ANSI to provide an official description of the language
Software Development
Analyze and specify the problem
Design the algorithm Implement in code Testing Maintenance
Software Development Problem Analysis
Identify data objects Determine Input / Output data Constraints on the problem
Design Decompose into smaller problems Top-down design (divide and conquer) Develop Algorithm (Desk check)
Algorithm refinement (Pseudocode)
Software Development Method
Implementation Converting the algorithm into
programming language Testing
Verify the program meets requirements System and Unit tests
Maintenance All programs undergo change over time
A Tempting Shortcut?
GOAL
THINKINGCODE
REVISEREVISE
REVISEDEBUG
DEBUG
DEBUG
TEST
CODEShortcut?
Translators and Tools
Assemblers for assembly languages Compilers for high-level languages IDE (integrated development environment)
compiler editor (creates text format files) linker loader debugger
Translation Syntax - the rules governing the
formation of statements in the language Spelling Order of words, statements Punctuation
Semantics – meaning of the statement What does it DO? What action does the
computer do when the statement is executed?
Processing a Program Editor used to enter the program
Creates source program file Compiler translates the source program
Displays syntax errors Creates (usually) temporary object code
Linker to combine object file with other object files (like library code) Creates final executable program (.exe)
Loader copies exe file into RAM to be run by machine
Three C++ Program Stages
other code from libraries,
etc.
other code from libraries,
etc.
written in machine language
written in machine language
written in machine language
written in machine language
written in C++
written in C++
via compiler via linker
SOURCE OBJECT EXECUTABLEmyprog.cpp myprog.obj myprog.exe
Ethics
Responsibility comes with knowledge
hacking piracy data theft and data privacy Responsibility to develop programs
without errors
Some C++ History
1972 : Dennis Ritchie at Bell Labs designs C and 90% of UNIX is then written in C
Late 70’s : OOP becomes popular
Bjarne Stroustrup at Bell Labs adds features to C to form “C with Classes”
1983 : Name C++ first used
1998 : ISO/ANSI standardization of C++
Objects in a hierarchy
Some C++ History
Dennis RitchieBjarne Stroustrup
Problem Solving Techniques
Ask questions -- about the data, the process, the output, error conditions
Look for familiar things -- certain situations arise again and again
Solve by analogy -- it may give you a place to start
Use means-ends analysis -- determine the I/O and then work out the details
More Problem Solving Techniques
Divide and conquer -- break up large problems into manageable units
Building-block approach -- can you solve small pieces of the problem?
Merge solutions -- instead of joining them end to end to avoid duplicate steps
Overcome mental block -- by rewriting the problem in your own words
Case Study: Converting Miles to Kilometers
Problem Your summer surveying job requires you to study some maps that give distances in kilometers and some that use miles. You and your coworkers prefer to deal in metric measurements. Write a program that performs the necessary conversion.
Case Study Analysis The first step in solving this
problem is to determine what you are asked to do. You must convert from one system of measurement to another, but are you supposed to convert from kilometers to miles, or vice versa? The problem states that you prefer to deal in metric measurements, so you must convert distance measurements in miles to kilometers.
Data Requirements
Problem Inputmiles distance in miles
Problem Outputkms the distance in
kilometers Relevant Formula
1 mile = 1.609 kilometers
Design an Algorithm that solves the problem
Algorithm (Pseudocode)1. Get the distance in miles.2. Convert the distance to kilometers.3. Display the distance in kilometers.
Algorithm Refinement2.1 The distance in kilometers is
1.609 times the distance in miles Desk check!
Miles to kilometers Implementation
Testing
Test with input data for which you can easily determine the expected results
E.g. 10 miles should convert to 16.09 kilometers
Top Related