Week 1 slides - Computer Science

advertisement
Problem Solving via Computers
CS 1428 Week 1
Texas State University
Tom O’Hara
Spring 2011
Chapter 1 Synopsis

Important
◦ Sections 2, 3

Useful
◦ Sections 1, 4, 5, 6

Ignore
◦ Section 7
◦ Table 1-1 (programming languages)
◦ Serendipity Booksellers project
Overview
Syllabus and text
 Problem solving via algorithms
 Models for computers
 Hardware and software
 Features of C++ programming language
 Generating machine code from C++

Computer Science

Computers are general-purpose machines
for performing calculations
◦ Originally just a glorified calculator
◦ Now quite versatile and pervasive

Computer science studies of how to
solve problems on computers
◦ Focused on abstract model of computers
◦ Avoids vendor specifics (e.g., IBM vs. Apple)
◦ More than just computer programming
Problem Solving via Computer
Algorithms: precise specification of how
to solve a particular problem
 Flowcharts: schematic representation of
steps in solving a problem (old school)
 Pseudo-code: instructions that combine
aspects of programming languages and
natural language
 Program: instructions in artificial language
designed for computer problem solving

Sample algorithm
For each hour of day
Let temp be current temperature
If temp less than 32 then
print "Below freezing"
Otherwise
print "At or above freezing"
Wait one hour
Note: displays hourly status about freezing temperature
Sample flow chart
Sample pseudo-code
hour = 0
while (hour < 24) do
temp = read_value()
if (temp < 32) then
print("Below freezing")
else
print("Above freezing")
hour = hour + 1
end while
Sample C++ Program
#include <iostream>
// standard I/O operations
using namespace std;
// import standard names
int main () {
double temperature;
// temperature in Farhenheit
for (int hour = 0; hour < 24; hour++) {
cout << "Enter current temperature: ";
cin >> temperature;
if (temperature < 32) {
cout << "It is below freezing" << endl;
}
else {
cout << "It is at or above freezing" << endl;
}
}
return (0);
}
Algorithm
Precise specification on solving a problem
 Central to computer science

◦ First solve problem via algorithm
◦ Then implement algorithm via program

Example
◦ Goal: Drive from South Austin to Texas State
◦ High-level Algorithm
Get on I35 at exit 226 (Slaughter Lane)
Drive 20 miles south
Take right on exit 206 (Aquarena Springs Drive)
Characteristics of Bad Algorithms

Vague descriptions
ex: Take I35 to San Marcos

Omits important details
Not mentioning exit 206 in San Marcos

Overly wordy descriptions
ex: Get on I35 by Walmart, taking access road
along South Park Meadows, passing I30 toll, ...

In C++ or another programming language
This is a program not an algorithm
Characteristics of Good Algorithms
Mostly in a natural language (e.g., English)
 Precise specification of problem

Get on I35 at exit 226, drive for 20 miles south,
get off I35 at exit 206, ...

Avoids technical terms and jargon
Depress the decelerator if state officer detected
=> Hit the breaks if you spot a trooper

Minimal use of "programeese"
Not written using C++ syntax
Algorithm Development

Approach
◦ Top-level from high-level description of task
◦ Stepwise refinement from English into highlevel pseudo-code (but not program)

Example:
◦ Task: Convert dollars to pesos
◦ Initial algorithm
Get amount of dollars
Get currency exchange for pesos
Divide dollars by exchange rate
Group Exercise on Algorithms

Done in separate groups
◦ Have one person record algorithm to hand in

Task: Write algorithm to do following
◦ Convert Celsius to Fahrenheit
 Fahrenheit = 9/5 Celsius + 32
◦ Also give clothing advice for temperature
 ex: Wear heavy parka if temperature below 32

Tip:
◦ Be specific but not overly wordy
◦ Write as if explaining to a younger sibling
Computer Hardware
CPU: combines control unit and ALU
 Registers: high speed storage in CPU

◦ Usually just few dozen given expense
Main Memory: temporary storage for data
 Secondary Storage: persistent memory
for data
 Input Devices: keyboards, mice, etc.
 Output Devices: monitors, printers, etc.

Computer Software
Programs and associated data
 Types

◦ System
 Operating System
 Device Drivers
 System utilities
◦ Application
 Word processing and other office applications
 Web browsers
Turing Machine
Note: For CS1428, this is mainly FYI material
Early model of computer
 Designed by English mathematician Alan Turing

◦ Famous for cracking German Enigma machine
Important in computational theory
 Features

◦ Infinite tape
◦ Operates only on current cell based on contents
◦ Can just change symbols or erase
Turing Machine Example
von Neumann Machine

Model developed by John von Neumann
◦ Mathematician at Princeton
◦ Pronounced as /noy man/

Foundation for modern computers
◦ Thus very important (see CS 1428 objectives)

Features:
◦ Programs and data both reside in memory:
“stored program computer”
◦ Control unit for fetch/decode/execute cycle
◦ Separate unit for basic operations (add, etc.)
Example von Neumann Machine
Programming Language Types

Machine: based on CPU operations
10001001000011011100000010000011

Assembly: symbolic version of above
ADL $13, %eax

High-level: usual mathematical notation
x + 13
C Language
Developed at Bell Labs in early 70’s
 High-level language
 Some lower-level features

Direct access to memory

Somewhat cryptic
*p++; // result = *p; p = p + 1;
rate = (age>17) ? adult : child;
C++ Language
Developed circa 1980 by Bjarne Stroustrup
 Adds additional features to C

◦
◦
◦
◦
◦

Object-oriented programming
Namespaces
Templates
Exception handling
Operator overloading
Not covered in depth until CS 2308
Program Elements in C++
Variables: user names for memory
 Constants: numbers and strings
 Operators: mathematical and logical
 Comments: notes about program
 Keywords: reserved words
 Functions: user-defined operations

Currency Conversion Program
#include <iostream>
// standard I/O operations
using namespace std;
// simplify access to internals
int main ()
{
double rate = 13.07;
double dollars, pesos;
// Get number of dollars and convert to pesos
cout << "Amount in dollars: ";
cin >> dollars;
pesos = dollars * rate;
// Output result with optional qualification
cout << "In pesos: " << pesos << endl;
if (pesos < 100) cout << "Pobrecito!" << endl;
return (0);
}
Miscellaneous C++ Issues

Lines versus statements
Statements don’t end until semicolon occurs
Ex: value = 1 * digit4 + 2 * digit3
+ 4 * digit2 + 8 * digit1;

Syntax versus Semantics
Syntax indicates format of expressions, etc.;
semantics determines how interpreted.

Procedural versus Object-oriented
Step by step specification versus use of agent-like
objects that encapsulate their behavior.
From C++ to Machine Code

Preprocessor
◦ Loads common definitions:
 Ex: #include <iostream>
◦ Performs miscellaneous text substitutions

Compilation
◦ Creates relocatable machine code

Linking
◦ Adds library code and creates executable
Student Exercise
Done by each student
 Convert Celsius to Fahrenheit

◦ Fahrenheit = 9/5 Celsius + 32

Tip
◦ Use currency conversion program as sample
◦ Don't panic: just to get you thinking in C++
Download