CS31-9

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

Info iconThis preview shows pages 1–12. 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 9-1 CS 31 : Introduction to Computer Science I Topic 9 Arrays
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 9-2 Arrays 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 9-3 Arrays Separate Data Example Consider evaluating car gasoline mileage The initial mileage must be recorded Each time gasoline is purchased, at least two pieces of data are to be recorded Odometer mileage Quantity of gasoline purchased
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 9-4 Arrays Separate Data Example Assuming five purchases, eleven variables can be used to hold the data miles0 miles1 gallons1 miles2 gallons2 miles3 gallons3 miles4 gallons4 miles5 gallons5
Background image of page 4
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 9-5 Arrays Separate Data Example Overall mileage can be computed: milesPerGallon = (gallons1 + gallons2 + gallons3 + gallons4 + gallons5) / (miles5 – miles0)
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 9-6 Arrays Separate Data Problems Many separate variables are required The number of purchases 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 9-7 Arrays Another Approach An array is a collection of multiple data items of the same type A C++ array is an aggregate data type All items must be the same type of data The array size must be a constant The array size must be specified at declaration
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 9-8 Arrays Array Element Access Each item in an array is called an element of the array Array elements are accessed numerically The first element of an array is number 0 The last element of an array with n elements is number n-1
Background image of page 8
CS 31: Introduction To CS I 9/2009 John A. Rohr All Rights JAR 9-9 Arrays Array Element Declaration An array declaration consists of the type of the elements in the array, the name of the array and the number of elements it holds Example: int arr[50]; Array name: arr Element data type: int Number of elements: 50
Background image of page 9

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 9-10 Arrays Array Subscripts A subscript is a numerical value used to access an individual element of an array The subscript can be a constant, an integer variable, or any valid integer expression A valid subscript must be in the range 0 to one less than the number of elements
Background image of page 10
Introduction To CS I 9/2009 John A. Rohr All Rights
Background image of page 11

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 97

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online