CS31-4

# CS31-4 - CS 31: Introduction to Computer Science I Topic 4...

This preview shows pages 1–10. Sign up to view the full content.

CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 4-1 CS 31 : Introduction to Computer Science I Topic 4 Vectors

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 4-2 Vectors Rationale Many situations require many pieces of data of the same type One approach is to use separate variables for each piece of data
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 4-3 Vectors Separate Data Example Consider recording golf scores The score for each hole must be recorded The total score is the sum of the scores for all the holes

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 4-4 Vectors Separate Data Example Assuming one game of nine holes, the following data would be recorded: Hole 1 score Hole 4 score Hole 7 score Hole 2 score Hole 5 score Hole 8 score Hole 3 score Hole 6 score Hole 9 score
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 4-5 Vectors Separate Data Example The game score can be computed: gameScore = Hole 1 score + Hole 2 score + Hole 3 score + Hole 4 score + Hole 5 score + Hole 6 score + Hole 7 score + Hole 8 score + Hole 9 score

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 4-6 Vectors Separate Data Problems Many separate variables are required The number of holes cannot be changed without changing the program Many similar items must be repeatedly typed when writing the program
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 4-7 Vectors Another Approach An vector is a collection of multiple data items of the same type The C++ language does not include vectors as a built-in type Vectors can be added using a #include statement

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 4-8 Vectors Another Approach A C++ vector is an aggregate data type All items must be the same type of data The vector knows it size (number of elements) The vector size may grow by adding elements The array size may be specified at declaration or omitted for an initially empty vector
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

### Page1 / 33

CS31-4 - CS 31: Introduction to Computer Science I Topic 4...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online