C++ Data Structures
C++ Data Structures
TA Matthew Hawthorne
MS in SE in the UT Option 3 Program Significant experience in SE & C++ PhD Student working with me on SW Architecture
ECE322C in Context
Software Dev./ Engineering Introductory Programming In C Systems Software Embedded Systems Other ECE areas Other non ECE areas
Objectives: teach you data structures, and to prepare you for later courses
Fall 2004 322C Lecture 1
Schedule Highlights
Aug 25 is our first day of class, Dec 1 is our last day of class Lectures every Monday and Wednesday in CPE 2.210 from 5- 6:15 Exams (3): Sep 27, Oct 27, and Dec 1 Programming assignments (6-8) will come out throughout the semester No class Nov 24 (evening before Thanksgiving)
Fall 2004 322C Lecture 1 6
You are responsible for all materials presented in classes, whether you attend or not. Material presented in class is in addition to the notes. The purpose of the lecture notes is to help you listen in class.
Fall 2004 322C Lecture 1 5
Three exams during semester. Pop quizzes at any time Grades made up of:
all equal to 1 assignment 60 % exams 40 % assignments/quizzes
322C Lecture 1
Fall 2004
Fall 2004
322C Lecture 1
Syllabus
All the remaining details of the course policies, rules, grading criteria, and procedures are in the syllabus document on the class web page Various C/C++ documents will also be available on the web page Will have the web page set up by next week
Fall 2004 322C Lecture 1 10
Questions?
Programming is about
Processing Data
Standard View of SE
Basic SE life-cycle processes
Requirements Architecture & Design Construction Deployment & Maintenance
Standard View of SE
We will become acquainted with aspects of each of these
Requirements: the problem to solve Design: the shape of the problem influencing the shape of the solution Construction: integrating multiple pieces Documentation: describing the solutions M&E various forms of analysis and testing Teamwork: will do some projects in teams Evolution: will evolve some projects
Fall 2004 322C Lecture 1 14
Fall 2004
13
A Different View of SE
Three elements in engineering SW systems
Theory Experience Process
Intricacy (may find some of this in some data structures) Wealth of detail (probably not in this class)
16
Managing Complexity
Modularity
Divide and conquer Break things up into manageable pieces Localize similar things Localize expected changes Functional: generalize and parameterize Implementation:
Define simple interface Hide implementation details
Encapsulation Abstraction
Language limitations here C++ Resource limitations time, PCs, cycles, etc Inadequate support tools, environments, etc
Fall 2004 322C Lecture 1 17
Fall 2004
322C Lecture 1
18
What is a Program?
Algorithms + Data Structures = Program Data
Is information represented in a manner suitable for communication or analysis by humans or machines A data structure is a systematic way of organizing, holding, and accessing computerized data
Structured Programming
A disciplined style of programming where the Static structure mirrors the dynamic structure
Modularization and scoping of programs Restricted set of control structures Indentation of subordinate structures Sequence Selection (if, Case, etc) Iteration (for, while, etc) Tuples Ordered elements Unordered elements
Fall 2004 322C Lecture 1 20
An algorithm
Is a logical sequence of discrete steps that describes a complete solution to a given problem computable in a finite amount of time. The key to packaging and time Vs. space tradeoff decisions
Fall 2004 322C Lecture 1
Control Structures
Data Structures
Data Structures
Base Types Tuples
struct int, float, char, bool, enum, pointer
Ordered types
Un-ordered types
sets, heaps
Fall 2004
string, array, vector, stack, queue, linked list, tree, graph, table, hash table
When do we use types, ordered or unordered structures? What are the costs and benefits? Open structures or abstract data types? What operations are needed? What else is needed?
322C Lecture 1
21
Fall 2004
322C Lecture 1
22
OBJECT-ORIENTED DESIGN Identifies various objects composed of data and operations, that can be used together to solve the problem.
FOCUS ON:
Underline the verbs if you want to focus on procedural aspects, Underline the nouns if you want to focus on the data aspects
processes
data objects
Fall 2004
322C Lecture 1
Functional Design
Main
Object-Oriented Design
A technique for developing a program in which the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data. cin cout
Get Data
Print Data
>>
<<
get
. . .
Private data
setf
. . .
Private data
Print Heading
ignore
Fall 2004
322C Lecture 1
Fall 2004
322C Lecture 1
Next Time
We get started on C++
Fall 2004
322C Lecture 1
29
Fall 2004
322C Lecture 1
30
Announcements
http://www.ece.utexas.edu/courses/fall_04/ee322c-15515
Class web site is up Basic C to C++ reading notes out Syllabus is there Class lectures will be there after Assignments will be announced there
Fall 2004
322C Lecture 2
C++ References
Books
C++ Programming Language, 3rd Edition, B. Stroustrup, Addison Wesley, 2000
OBJECT-ORIENTED DESIGN Identifies various objects composed of data and operations, that can be used together to solve the problem. FOCUS ON: data objects
4
Online sources
Fall 2004
322C Lecture 2
Algorithm
General - a step by step procedure for solving some problem or accomplishing some goal (Websters) - e.g. a recipe Computer - A logical sequence of discrete steps that describes a complete solution to a given problem that is computable in a finite amount of time.
Find the largest prime number is NOT amenable Often appears at several levels of abstraction/detail Algorithms operate on data structures from a functional viewpoint
322C Lecture 2 6
Fall 2004
322C Lecture 2
Fall 2004
Algorithm (cont.)
A given problem may be solvable by a number of different algorithms. Its importance is crucial in designing a solution program. An algorithm may be transformed into a working program if its computable An algorithm will typically use levels of abstraction to make the solution clearer and implementation easier.
Fall 2004 322C Lecture 2 7
Algorithm (cont.)
An algorithm may be represented in several ways:
Pseudocode - structured English language used to help design an algorithm (free form; e.g. recipe) Flowchart - a graphical representation of an algorithm. It shows control and data flow. Formal languages - outside the scope of this course Computer program - eventually an algorithm is written in a programming language
Fall 2004
322C Lecture 2
Algorithms in General
1) 2) 3) 4) 5)
Steps 1 - 4 depict a sequential flow of instructions Step 5 introduces the notion of repetition/iteration of instructions
Fall 2004 322C Lecture 2 11
4)
A. B. C. D.
Stop
Algorithm Exercises
Process/ Task/action
Fall 2004 322C Lecture 2 15
selection
T
sequence
1. 2. 3.
selection
If condition is true then else end if
condition
condition
...
one or a block of statements grouped together
Fall 2004 322C Lecture 2 17
start
Making Cookies
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
soft
Step 8a
Step 9
stop
Step 8b
Fall 2004
322C Lecture 2
19
Fall 2004
322C Lecture 2
20
Numerics - Example
Problem - given the right triangle depicted in the figure below, and given values for the lengths of sides a and b, what is the length of side c?
start
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
yes no Do it Again?
Step 7
soft
Step 8a
Step 9
stop
Step 8b b
21 Fall 2004 322C Lecture 2 22
Fall 2004
322C Lecture 2
Algorithm
1. Input values for sides a and b 2. Compute the length of the hypotenuse c 3. Output the answer
Code Example
#include <iostream> #include <cmath> using namespace std; int main ( ) { // find the length of the hypotenuse c of a right triangle // with sides a,b,c double a, b, c; cout << enter a value for side a << endl; cin >> a; cout << enter a value for side b << endl; cin >> b; c = sqrt (a*a + b*b); cout << the value of c is << c << endl; return 0; } // end of main
Fall 2004 322C Lecture 2 24
Fall 2004
322C Lecture 2
23
Numerics - Exercise
Problem - given the right triangle depicted in the figure below, and given values for the lengths of sides a and b, what is the length of side c?
c a b
Algorithm
Repeat the following steps until ?? 1. input values for sides a and b 2. compute the length of the hypotenuse c 3. output the answer
Code Example
#include <iostream> #include <cmath> using namespace std; int main () { // find the length of the hypotenuse c of a right triangle // with sides a,b,c double a, b, c; cout << enter a value for side a << endl; cin >> a; cout << enter a value for side b << endl; cin >> b; c = sqrt (a*a + b*b); cout << the value of c is << c << endl; return 0; } // end of main
Fall 2004 322C Lecture 2 27 Fall 2004 322C Lecture 2 28
Fall 2004
Fall 2004
Work out the details of the algorithm/data structures and their sub-parts until it is ready for coding in a programming language Code, test and verify the program
Revisit and revise the plan as necessary Stop when the solution is correct
Fall 2004 322C Lecture 2
31
Fall 2004
322C Lecture 2
32
Noun phrases typically will denote potential data types and variables (and later: classes, objects) Verb phrases will denote potential processes/functions/actions Outputs can often be related to inputs by a transformation process (perhaps needing intermediate variables)
Work examples all the way through by hand Seek clarification and more information as needed from the problem specifiers (the teaching team in this case) Create a high level sketch of the flow of the algorithm DO NOT start by trying to write C++ code!!
Fall 2004 322C Lecture 2 33
Fall 2004
322C Lecture 2
34
Example Problem
Problem - You have $1,000 to invest in a Certificate of Deposit (CD) that earns 3% interest per year. Interest is compounded annually. How many years will it take to double your initial investment?
Example Problem
Problem - You have $1,000 to invest in a Certificate of Deposit (CD) that earns 3% interest per year. Interest is compounded annually. How many years will it take to double your initial investment? Questions to you (the problem solver): Do you fully understand the problem statement? If not, what are your questions? Can you solve the problem by hand? Can you then teach the computer how to do it? Ask yourself, if I was a computer, what would I do first? then next, etc. What if the problem was changed slightly? Can you design a solution to solve this problem?
Fall 2004 322C Lecture 2 36
Fall 2004
322C Lecture 2
35
Example Problem
You have $1,000 to invest in a Certificate of Deposit (CD) that earns 3% interest per year. Interest is compounded annually. How many years will it take to double your initial investment?
Inputs
Process
To begin, represent the program as a closed, black-box system. Identify the inputs and outputs before you begin to describe and outline (plan) the steps inside the process box.
Fall 2004 322C Lecture 2 37 Fall 2004 322C Lecture 2 38
Example Problem
You have $1,000 to invest in a Certificate of Deposit (CD) that earns 3% interest per year. Interest is compounded annually. How many years will it take to double your initial investment? outputs inputs Process
Number of years
Example Problem
You have $1,000 to invest in a Certificate of Deposit (CD) that earns 3% interest per year. Interest is compounded annually. How many years will it take to double your initial investment?
inputs
Rate Initial balance
outputs Process
Number of years
Fall 2004
322C Lecture 2
39
Fall 2004
322C Lecture 2
40
Example Problem
You have $1,000 to invest in a Certificate of Deposit (CD) that earns 3% interest per year. Interest is compounded annually. How many years will it take to double your initial investment? Process inputs
Rate Initial balance Determine the # of years it takes to double a given investment at a given interest rate in my CD
322C Lecture 2