CIS 111 Programming basics

Objectives

  • List the four primary functions of a computer
  • Compare and contrast primary and secondary memory
  • Describe the program design process
  • Define the term algorithm
  • Define the term heuristic
  • Explain basic flowcharting symbols
  • Use flowcharts and pseudocode to describe algorithms
  • Translate flowcharts and pseudocode into Python programs

Primary computer functions

The four primary functions a computer performs are:

  • input
  • output
  • processing
  • storage

The primary functions of a computer helped to determine how components for computer systems were developed. Different components support the various functions. For example:

  • input: mouse, keyboard
  • output: printer, screen
  • processing: CPU, GPU
  • storage: main memory, secondary storage

Storage

Storage is generally divided into two main categories:
  • primary: main memory (usually RAM) - usually more expensive per byte, volatile (loses contents when power is lost), available in smaller amounts than secondary memory, and generally much faster than secondary storage - used for short-term storage
  • secondary: hard drives, memory sticks - usually less expensive per byte than primary memory, non-volatile (retains contents when power is lost), available in larger amounts, and generally much slower than primary storage - used for long-term storage

Data is stored in computers as a sequence of 0s and 1s. It doesn't matter whether that data is a picture, a video, a text document, or a program. There are many ways of representing 0s and 1s in digital equipment. You can use a high or low voltage, a hole or absence of a hole, oriented magnetization or non-oriented magnetization, etc. However the hardware distinguishes between two possibilities, it is presented to us humans as 0s and 1s. This representation is called binary. The individual 0s and 1s are called bits. Modern computers almost always group together eight bits to form a byte (because dealing with bytes is easier than always dealing with bits). Bytes are further grouped to form words. That's why processors are referred to as 16-bit, or 32-bit, or 64-bit - it is convenient to double the word size when moving to the next level of power. Most of the early personal computers such as the Apple ][ and IBM PC contained eight-bit processors.

Terms you must know

  • algorithm: step-by-step process for solving a problem (must be finite)
  • heuristic: a "rule-of-thumb"; may not be optimal but usually a quick way to get a very good solution; vital for computationaly difficult problems

Five steps of program design

  • analyze problem
  • develop algorithm
  • write code (translate algorithm)
  • test and debug
  • document (should have been going on throughout process)

Flowcharts

Wikipedia has an article on basic flowcharting. We will use the following flowchart symbols:

Flowchart symbolMeaning
flowchart symbol for terminal operationterminal (start/stop)
flowchart symbol for I/Oinput/output
flowchart symbol for calculationscalculations
flowchart symbol for decision (a test)decision (used for selection and repetition)
flowchart symbol for a connectorconnector
flowchart symbol for a flow lineflow line
flowchart symbol for a predefined processpredefined process
alternate flowchart symbol for a predefined processpredefined process (alternate)
Example for a program to calculate area:
flowchart for program that calculates area

Pseudocode

  • Wikipedia has an article on pseudocode
  • A suggested pseudocode "standard" is available at: users.csc.calpoly.edu/~jdalbey/SWE/pdl_std.html
  • Pseudocode is a cross between a programming language and natural language
  • Example pseudocode for a program to calculate area: get length get width area = length * width display area
  • Example of the same program in Python: length = float(input('Enter the length: ')) width = float(input('Enter the width: ')) area = length * width print('The area is', area)

Identifiers, variables, constants

Variables are memory locations that are used to store data. These locations are given names so you can work with them more easily. Modules within programs may also be given names. These names are called identifiers. You get to choose the names for identifiers. Naming rules vary between languages, but some common rules are:

  • names should start with a letter or underscore
  • names may contain only letters, digits, or underscores
  • names may be case sensitive (usually are in modern compilers)
  • some names are reserved by the programming language and you can't use them
  • names should be descriptive and help indicate their purpose
  • names are often "camel-cased" to make them more readable for programmers

Constants are also memory locations that are used to store data. The difference between variables and constants is that constants do not change value once they are set. Variables can be modified.