DOC PREVIEW
Saddleback CS 1B - Array

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Topic 4 Arrays Chapter 8 in the shrinkwrap Chapter 9 in Malik Why do we need Arrays int main What do we do if we have to store more than 1 piece of information of the same type int item0 item1 item2 int sum We declare different variables cout Enter 3 integers cin item0 item1 item2 sum item0 item1 item2 cout The sum of the numbers sum endl cout the numbers in reverse order are cout item2 item1 item0 endl return 0 Topic 4 ch 8 Arrays 2 What are arrays A collection of data of the same type A special group of variables Arrays can hold many pieces of data all have the same data type and name but different values Aggregate data type Means grouping Used for lists of like items Test scores temperatures names etc Avoids declaring multiple simple variables Can manipulate list as one entity Topic 4 ch 8 Arrays 3 1 Simple Composite data types Simple Data Types Data types that store only one piece of information What we have been using thus far Int float double char long Structured Composite Data types Each data item is a collection of other data items Topic 4 ch 8 Arrays 4 Now on to Arrays Declaring an Array Syntax dataType arrayName number of elements Declaring an array allocates the memory for the array Example int scoresAr 5 declares an array of 5 integers named score The number of elements can be a literal e g 5 int scoresAr 5 Or a named constant const int NUMBER OF TESTS 5 int scoresAr NUMBER OF TESTS 6 Topic 4 ch 8 Arrays 2 Elements and Indexes Each individual item in an array is called an element Each element has an index associated with it An index is a number which indicates which value we are referring to Example scoresAr 0 the first element in our array Index The first element is ALWAYS zero So if we have 5 elements our indexes would be 0 1 2 3 4 or scoresAr 0 scoresAr 1 scoresAr 2 scoresAr 3 score 4 Note The brackets specify the size in the declaration and the subscript Topic 4 ch 8 Arrays or index anywhere else 7 Memory and Arrays 4 bytes scoresAr 0 scoresAr 1 scoresAr 2 scoresAr 3 scoresAr 4 scoresAr 5 int scoresAr 5 First element always has an index of 0 0 1 2 3 4 are in bounds all other values are out of bounds score 5 will not produce a compile time error It will produce a run time error 8 Topic 4 ch 8 Arrays Initializing Arrays Simple variables can be initialized at declaration int price 0 0 is initial value of after declaration or equivalently in the code int price price 0 This is not considered good style do not do this in this class please Arrays can be initialized at declaration as well int itemsAr 3 2 12 1 or equivalently in the code int itemsAr 3 itemsAr 0 2 itemsAr 1 12 itemsAr 2 1 Topic 4 ch 8 Arrays 3 Initializing Arrays 2 if you have more elements than values in the list then the extras at the end default to 0 int itemsAr 5 2 12 1 int itemsAr 5 2 12 1 0 0 This is okay This is not recommended You can also initialize all the elements to 0 using this method This is okay int itemsAr 5 0 if you have more values than elements specified then you will get a compiler error int itemsAr 5 2 12 1 2 9 5 compiler error if you don t specify the number of elements it will default to the number of values in the list int itemsAr 2 12 1 2 9 5 children will default to 6 elements This is not recommended Topic 4 ch 8 Arrays Initializing using a FOR loop include iostream h NOTE For Loops are very useful when you need int main to access every element in an array float gpasAr 5 an array holding 5 grade point averages INP OUT load the array from the keyboard for int index 0 index 5 index cout Enter the gpa for student index 1 cin gpasAr index output the contents of the array cout n nStudent Grade Point Averages n This loop initializes the array for int ind 0 ind 5 ind cout nGPA for student ind 1 gpasAr ind return 0 This loop outputs the array 11 Topic 4 ch 8 Arrays Example Do a desk check with Inputs 5 10 15 int main int itemsAr 3 int sum index itemsAr 0 1 2 index sum sum 0 for index 0 index 3 index cout Enter an integer cin itemsAr index sum sum itemsAr index Output cout The sum of the numbers sum endl cout The numbers in reverse are for index 2 index 1 index cout itemsAr index return 0 What if we want to have 10 items What changes will we have to make 12 4 Defining a Constant as Array Size Always use defined named constant for array size Example const int AR SIZE 5 int scoresAr AR SIZE Note that it must be declared as an integer NOTE Can t do this with a variable Improves readability Improves versatility Improves maintainability The number of elements must be known at compile time Using a constant is considered a best practice Topic 4 ch 8 Arrays int main const int AR SIZE 10 int itemsAr AR SIZE int sum index sum 0 Instead of all those changes we can use a constant and just change the constant for index 0 index AR SIZE index Works well if they have to enter 10 items cout Enter an integer cin itemsAr index sum sum itemsAr index cout The sum of the numbers sum endl cout The numbers in reverse order are for index AR SIZE 1 index 1 index cout itemsAr index Note that this is AR SIZE 1 return 0 Topic 4 ch 8 Arrays 14 Initializing using while loops Need to check for out of bounds as well as user controlled LCV int itemsAr AR SIZE int index int intInput index 0 Need to make sure we don t Go out of bounds load the array from keyboard input cout Enter the item enter 1 when done cin intInput while intInput 1 index AR SIZE itemsAr index intInput Initialize Both LCVs Change Both LCVs cout Enter the item enter 1 when done cin intInput index Topic 4 ch 8 Arrays What if we want to read in from a file 15 5 Initializing from a File Need to check if we are not at the end of our input file while inFile will handle this We need to check 2 things then inFile will return False if it is at the end of file While we are not at the end of the file AND while we are still within bounds of our array int index load the array from the keyboard What is wrong with this index 0 code while inFile index AR SIZE cout Enter the gpa for student index 1 cin itemsAr …


View Full Document

Saddleback CS 1B - Array

Download Array
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Array and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Array and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?