CS31-9

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

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

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

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 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
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

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 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
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)

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 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
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

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 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
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

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 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
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.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online