CS31-4

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
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 Right Arrow Icon
Ask a homework question - tutors are online