31
CS 115 Chapter 1 Overview of Programming and Problem Solving

problem solving

Embed Size (px)

Citation preview

Page 1: problem solving

CS 115 Chapter 1

Overview of Programming and Problem Solving

Page 2: 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

Page 3: problem solving

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

Page 4: problem solving

Computer Hardware Components

Arithmetic Logic Unit

Control Unit

Auxiliary StorageDevice

Memory Unit(RAM & Registers)

Central Processing Unit(CPU) Input Device

Output Device

Peripherals

Page 5: problem solving

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"

Page 6: problem solving

Memory Cells Address Contents

-27.2

354

0.005

-26

H

X

75.62

RTV 001

. . .

0

1

2

3

4

5

6

. . .

999

Page 7: problem solving

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

Page 8: problem solving

Languages

Machine languages take a step, lift arm, grasp knob, turn

knob... Assembly languages

LEAVE through DOOR High-level languages

"get outta here!"

Page 9: problem solving

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

Page 10: problem solving

A Program in Machine and Assembly Language

Page 11: problem solving

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

Page 12: problem solving

Software Development

Analyze and specify the problem

Design the algorithm Implement in code Testing Maintenance

Page 13: problem solving

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)

Page 14: problem solving

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

Page 15: problem solving

A Tempting Shortcut?

GOAL

THINKINGCODE

REVISEREVISE

REVISEDEBUG

DEBUG

DEBUG

TEST

CODEShortcut?

Page 16: problem solving

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

Page 17: problem solving

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?

Page 18: problem solving

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

Page 19: problem solving

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

Page 20: problem solving

Ethics

Responsibility comes with knowledge

hacking piracy data theft and data privacy Responsibility to develop programs

without errors

Page 21: problem solving

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

Page 22: problem solving

Objects in a hierarchy

Page 23: problem solving

Some C++ History

Dennis RitchieBjarne Stroustrup

Page 24: problem solving

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

Page 25: problem solving

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

Page 26: problem solving

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.

Page 27: problem solving

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.

Page 28: problem solving

Data Requirements

Problem Inputmiles distance in miles

Problem Outputkms the distance in

kilometers Relevant Formula

1 mile = 1.609 kilometers

Page 29: problem solving

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!

Page 30: problem solving

Miles to kilometers Implementation

Page 31: problem solving

Testing

Test with input data for which you can easily determine the expected results

E.g. 10 miles should convert to 16.09 kilometers