Download presentation
Presentation is loading. Please wait.
1
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 12: Arrays
2
In This Lesson We Will: Introduce arrays Introduce arrays Demonstrate how to declare and create arrays Demonstrate how to declare and create arrays
3
Topic Arrays
4
Arrays An array is a list of items An array is a list of items Usually associated with each other Usually associated with each other Of the same data type (though there are ways around this) Of the same data type (though there are ways around this) Declare arrays by adding square brackets to the declaration statement: Declare arrays by adding square brackets to the declaration statement: // Variables int grades[NUM_STUDENTS];
5
Worth Noting For now we will declare the number of elements in the array at the same time we declare the variable For now we will declare the number of elements in the array at the same time we declare the variable Number of elements is often a constant Number of elements is often a constant Declaration can take other forms Declaration can take other forms In advanced C++ classes you will learn about pointers; you may come to prefer that approach In advanced C++ classes you will learn about pointers; you may come to prefer that approach
6
Referencing Array Elements Access elements using index operator: [] Access elements using index operator: [] Example: Example: grades[5] = 80; Indices run from 0 to one less than the number of elements Indices run from 0 to one less than the number of elements
7
Array Initialization Often initialize with a for loop: Often initialize with a for loop: // Variables int grades[NUM_OF_STUDENTS]; for (int i = 0; i < NUM_OF_STUDENTS; i++) grades[i] = 0;
8
Array Initialization Also possible to initialize arrays explicitly Also possible to initialize arrays explicitly Enclose “array literals” in curly braces Enclose “array literals” in curly braces int grades[] = {20, 18, 25, 22, 15, 17, 21, 19}; Curly braces declare array, allocate space, and initialize values Curly braces declare array, allocate space, and initialize values NOTE: If size of the array is not specified the compiler sets its size to the number of elements NOTE: If size of the array is not specified the compiler sets its size to the number of elements
9
Topic Example
10
Problem Specification Write a program which will read a sequence of five numbers, sort the numbers into their proper order, then print out the results Write a program which will read a sequence of five numbers, sort the numbers into their proper order, then print out the results Use a selection sort algorithm to place the elements in their proper order Use a selection sort algorithm to place the elements in their proper order
11
Selection Sort Start at the first array element Start at the first array element Search through the remaining elements for the largest value Search through the remaining elements for the largest value Switch the position of the largest element with the first element Switch the position of the largest element with the first element Now repeat that process, but ignore the first element, since that one has already been set Now repeat that process, but ignore the first element, since that one has already been set Find the largest remaining element and move it to the earliest remaining position Find the largest remaining element and move it to the earliest remaining position
12
In This Lesson We Have: Introduced the concept of arrays Introduced the concept of arrays Discussed the declaration and initialization of arrays Discussed the declaration and initialization of arrays
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.