210 likes | 267 Views
Learn about arrays, a convenient way to store multiple data values of the same type. Understand the syntax, initialization, and accessing elements in arrays. Explore examples to grasp the concept effectively.
E N D
Learning Objectives? • Arrays • What • Why • How • Looping through them • Examples
What is an Array? • So far, we have worked with a small number of variables in our example programs. • One variable can store One data value. • We’ve seen this in the example programs. i.e. averaging quizzes • What if we need to work with 100 integers or 100 doubles (100 data values) at the same time? • Do we declare 100 variables? (How about 1000?) • There’s gotta be a better way…..
Arrays • One solution is to use an ARRAY. • We use an array to store a number of elements of the same data type • Structures of related data items • Static entity (same size throughout program) • A one-dimensional array is a number of successive memory locations, each of which can store an item of data of the same type and which are all referenced through the same variable name. • Example: A grades array for our quiz average program.
Arrays • Array • Consecutive group of memory locations • Same name and type (int, char, etc.) • To refer to an element - • Specify array name and position number (index) • Format: arrayname[ position number ] • First element at position 0 • N-element array grades: • Grades[0], grades[1] … grades[n - 1] • Nth element as position N-1
Arrays • Syntax to declare an array Data Type Array_Name [Array Size]; • Example: int grades[6]; • The array holds 6 grades. • The array index ranges from 0 to 5
grades[0] grades[2] grades[3] grades[4] grades[5] grades[1] 73 62 51 42 41 34 intgrades[6];intgrades[6] = { 73,62,51,42,41,34 }; How many array elements (array size)? First element is grades[0] Last element is grades[5]
Name of array (Note that all elements of this array have the same name: grade) grade[0] 5 grade[1] 6 00xh7 0 grade[0] 7.2 grade[1] 4.3 grade[2] 8.9 grade[3] 0 grade[4] 10 grade[5] 3 grade[9] 10 grade[10] 9 grade[11] 8 Position number of the element within the array Arrays To manage grades, we declare an array of: Size = 12 Type = double Name = grade double grade[12];
Arrays • Array elements are like other variables • Assignment, printing for an integer array c c[ 0 ] = 3; cout << c[ 0 ]; • Can perform operations inside subscript c[5–2] is the same asc[3]
( Note all elements of this array have the same name: quiz ) Name of array quiz[0] 5 quiz[1] 6 quiz[2] 0 quiz[3] 7.2 quiz[4] 4.3 quiz[5] 8.9 grade[6] 0 quiz[7] 10 quiz[8] 3 quiz[9] 10 quiz[10] 9 quiz[11] 8 Position number of the element within array c Arrays To manage grades, we declare an array of: Size = 12 Type = double Name = quiz double quiz[12];
Arrays • When declaring arrays, specify • Name • Type of array (any data type) • Number of elements • type arrayName[arraySize ]; • int c[ 10 ]; // array of 10 integers • float d[ 3284 ]; // array of 3284 floats • Declaring multiple arrays of same type • Use comma separated list, like regular variables • int b[ 100 ], x[ 27 ];
grades[0] grades[0] grades[1] grades[1] grades[2] grades[2] grades[3] grades[3] grades[4] grades[4] grades[5] grades[5] grades[0] grades[1] grades[2] grades[3] 0 1 2 3 4 5 0 0 0 0 0 0 0 1 2 3 Arrays - Declaration & Initialization • We can • - Initialize the array when defined: • int grades[6] = {0,1,2,3,4,5}; - Initialize shorter than array size int grades[6] = {0}; - Initialize with a sequence, & no size int grades[ ] = {0,1,2,3};
Accessing Array elements • Fill an array with numbers 0,2,4,6,8,10,12 (0*2, 1*2, 3*2, …) • //define array • const int SIZE = 7; • int grades[SIZE]; • //assign grades to array elements • for (int k = 0; k < SIZE ; k++) • grades[k] = k *2; • //print array • for (int k = 0 ; k < SIZE ; k++) • cout << grades[k] << “\t” ;
Accessing Array elements • Printing an array in reverse order • for (int k = SIZE - 1 ; k >= 0 ; k--) • cout << grades[k] << “\t ”; • No check is performed that array index lies within the bounds of the array! • int grades[5];// index bounds from 0 to 4 • cout << grades[5];//What happens in this case?
Accessing Array elements • Reading from a file and assigning to array elements • //A file with 2 fields per line (id grade) id is an int and grade is a float • const int SIZE = 20; • int id[20]; //An array called id • float grade[20]; //A array called grade • fstream gradesf; • gradesf.open( “GradesFile”,ios::in); • for (k=0 ; k < SIZE; k++) • gradesf >> id[k] >> grade[k];
Accessing Array elements When you declare an array – specify: .. Name ….Type …….. Size ………. There are variations, that give you some flexibility.
Filling the array do { fill array (or block of statements) } while ( <there is still data> )
Coding • Array/Parameter Review page • A problem with an array of temperatures const int ARRAY_SIZE = 10; int temps[ARRAY_SIZE] = {50,29,58,44,53,45,42,54,67,60 };
Coding inttemps[ARRAY_SIZE] = {50,29,58,44,53,45,42,54,67,60 }; temps
Coding Write code to do the following: • Count the number of temperatures in the array that are below 5 0 ; print out the count • Print out temperatures in the array that are below 5 0. • Print out the temperatures below 45. • Prompt the user to input a temperature; print out the values below that temp.
Coding Write code to do the following: • We can write a function. void print B elow ( int temps [], int size, int whatTemp ) • Page 4 8 2 – 4 9 1 arrays and functions • Prompt for a temperature. Determine whether or not that value is in the array.
Coding Examples 9-6, 9- 7 • Array parameters are always passed by reference; do not use & • Constant arrays as formal parameters void printArray(const int list[], int listSize) void fillArray(int list[], int listSize) //Function to find and return the index of the //largest element in an int array. //parameter listSize specifies # array elements int indexLargestElement(const int list[], int listSize)