1 / 21

Understanding Arrays in Programming: Definition, Use, and Examples

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.

glenna
Download Presentation

Understanding Arrays in Programming: Definition, Use, and Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Learning Objectives? • Arrays • What • Why • How • Looping through them • Examples

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

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

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

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

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

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

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

  9. ( 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];

  10. 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 ];

  11. 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};

  12. 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” ;

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

  14. 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];

  15. Accessing Array elements When you declare an array – specify: .. Name ….Type …….. Size ………. There are variations, that give you some flexibility.

  16. Filling the array do { fill array (or block of statements) } while ( <there is still data> )

  17. 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 };

  18. Coding inttemps[ARRAY_SIZE] = {50,29,58,44,53,45,42,54,67,60 }; temps

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

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

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

More Related