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 Main Memory Where decisions are made, computations are performed, and input/output requests are delegated Stores information being processed by the CPU short-term (volatile) Secondary Memory (Mass Storage) Stores data and programs long-term Computer Hardware Components Peripherals Input Device Central Processing Unit(CPU) Control Unit Arithmetic Logic Unit Output Device Auxiliary Storage Device Memory Unit(RAM & Registers) 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 0 1 2 3 -27.2 354 0.005 -26 4 5 6 H RTV 001 ... ... 999 X 75.62 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 Assembly languages take a step, lift arm, grasp knob, turn knob... 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 Testing Converting the algorithm into programming language Verify the program meets requirements System and Unit tests Maintenance All programs undergo change over time A Tempting Shortcut? DEBUG REVISE REVISE DEBUG DEBUG REVISE CODE GOAL TEST THINKING CODE 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 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 source program file Creates final executable program (.exe) Loader copies exe file into RAM to be run by machine Three C++ Program Stages myprog.cpp SOURCE myprog.obj OBJECT written in C++ via compiler myprog.exe EXECUTABLE written in machine language written in machine language via linker other code from libraries, etc. 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 Ritchie Bjarne 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 Input miles distance in miles Problem Output kms 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 Refinement 2.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