New version page

# Saddleback CS 1B - Topic 4 - Arrays

Pages: 18
Documents in this Course

## This preview shows page 1-2-3-4-5-6 out of 18 pages.

View Full Document
Do you want full access? Go Premium and unlock all 18 pages.
Do you want full access? Go Premium and unlock all 18 pages.
Do you want full access? Go Premium and unlock all 18 pages.
Do you want full access? Go Premium and unlock all 18 pages.
Do you want full access? Go Premium and unlock all 18 pages.
Do you want full access? Go Premium and unlock all 18 pages.

Unformatted text preview:

1 Topic 4 - Arrays Chapter 8 in the shrinkwrap Chapter 9 in Malik Why do we need Arrays? int main () { int item0, item1,item2; int sum; 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 do we do if we have to store more than 1 piece of information of the same type? We declare different variables.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 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 43 Now on to Arrays Declaring an Array Declaring an array allocates the memory for the array Example int score; // declares an array of 5 integers // named score The number of elements can be… ● a literal (e.g. 5) int score ● Or a named constant const int NUMBER_OF_TESTS = 5; int score[NUMBER_OF_TESTS]; Topic 4 - ch 8 - Arrays 6 Syntax dataType arrayName[number_of_elements];4 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 score  the first element in our array The first element is ALWAYS zero So if we have 5 elements our indexes would be 0,1,2,3,4 or score, score, score , score, score Topic 4 - ch 8 - Arrays 7 Note: The brackets specify the size in the declaration and the subscript or index anywhere else Index Memory and Arrays Topic 4 - ch 8 - Arrays 8 int score; score; score; score; 4 bytes First element always has an index of 0 score; score; score; score  will not produce a compile-time error It will produce a run-time error. } 0,1,2,3,4 are in bounds all other values are out of bounds5 Indexes An index can be anything that evaluates to an integer ● A literal ◘ e.g. score ● A variable or a constant ◘ e.g. score[i] ● An expression ◘ e.g. score[2 * i - j] Topic 4 - ch 8 - Arrays 9 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;  Arrays can be initialized at declaration as well: int item = {2, 12, 1}; or equivalently in the code : int item; item = 2; item = 12; item = 1; Topic 4 - ch 8 - Arrays This is not considered good style  do not do this in this class please6 Initializing Arrays (2) if you have more elements than values in the list then the extras at the end default to 0 int item={2,12,1};  int item  = {2,12,1,0,0}; You can also initialize all the elements to 0 using this method int item={0} if you have more values than elements specified then you will get a compiler error int item={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 item[]={2,12,1,2,9,5};  children will default to 6 elements Topic 4 - ch 8 - Arrays This is not recommended! This is not recommended! This is okay! This is okay! Initializing using a FOR loop #include <iostream.h> int main() { float gpa; // 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 >> gpa[index]; } // output the contents of the array cout << "\n\nStudent Grade Point Averages\n"; for(int ind = 0; ind < 5; ind++) { cout << "\nGPA for student " << ind + 1 <<": " << gpa[ind]; } return 0; } Topic 4 - ch 8 - Arrays 12 NOTE: For Loops are very useful when you need to access every element in an array. This loop outputs the array This loop initializes the array7 int main () { int item; int sum, index; sum=0; for (index = 0; index < 3 ; index++) { cout << “Enter an integer: ”; cin >> item[index]; sum = sum + item[index]; } cout << “The sum of the numbers = “ << sum << endl; cout << “The numbers in reverse are: “; for (index = 2 ; ind > -1 ; ind-- ) { cout << item[ind] << “, ”; } return 0; } Example 13 Do a desk check with Inputs  5, 10, 15 What if we want to have 10 items? What changes will we have to make? index sum ind Output item 0 1 2 Defining a Constant as Array Size  Always use defined/named constant for array size  Example: const int AR_SIZE = 5; int score[AR_SIZE]; ● NOTE: Can‟t do this with a variable  Improves readability  Improves versatility  Improves maintainability Topic 4 - ch 8 - Arrays The number of elements must be known at compile time Note that it must be declared as an integer! Using a constant is considered a best practice8 int main () { const int AR_SIZE =10; int item[AR_SIZE]; int sum, index; sum=0; for (index = 0 ; index < AR_SIZE; index++) { cout << “Enter an integer: ”; cin >> item[index]; sum= sum + item[index]; } cout << “The sum of the numbers = “ << sum <<endl; cout << “The numbers in reverse order are: “; for (index = AR_SIZE-1; ind > -1 ; ind--) { cout << item[ind] << “, ”; } return 0; } Topic 4 - ch 8 - Arrays 15 Instead of all those changes we can use a constant and just change the constant. Note that this is AR_SIZE-1 Works well if they have to enter 10 items  Need to check for out of bounds as well as user controlled LCV … int item[AR_SIZE]; int index; int intInput; index = 0; // load the array from keyboard input cout << "Enter the item (enter -1 when done): "

View Full Document Unlocking...