Presentation is loading. Please wait.

Presentation is loading. Please wait.

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)

Similar presentations


Presentation on theme: "VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)"— Presentation transcript:

1 VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)

2 Objectives Visual C++ Programming 2  Learn how to declare and use one-dimensional arrays  Pass array elements and entire arrays into methods  Learn how to search for values in an array using the sequential search technique  Discover how two-dimensional arrays can be used to store and process tables of data  Gain further experience with nested loops

3 Arrays Visual C++ Programming 3  Data structures organize related items in memory  Arrays are data structures that consist of a contiguous block of memory cells  An array element is a single unit of data in an array  Index values (subscripts) are used to give an identifying number to each element  Index values are automatically assigned  Index values always start with 0  The last index value is one less than the number of elements If an array has 5 elements its index values go from 0 to 4

4 Visual C++ Programming 4

5 Array Declaration and Initialization Visual C++ Programming 5  Fixed-size arrays are declared in several ways  By naming the array and providing a size in square brackets Example: int num[5]; Initialization by using assignment statements: num[0] = 2;  By naming the array, leaving the size empty and providing a value list to initialize it Example: int num = { 2,7,3,8,6};  Static and dynamic allocation  The number of elements in a fixed size array cannot be changed (static allocation)  In later chapters we learn dynamic allocation to create an array at runtime

6 Visual C++ Programming 6

7 Array element assignment Visual C++ Programming 7

8 Using Arrays Visual C++ Programming 8 To reference the data in a particular element use the array name and the subscript of the element Example: the second element in array num is num[1] Loops work well with arrays, especially for loops The loop control variable is used as a subscript Example: for (int i=0; i < 5; i++) sum += num[i];

9 Value list declaration Visual C++ Programming 9

10 Subscript referencing Visual C++ Programming 10

11 Summation Visual C++ Programming 11

12 Visual C++ Programming 12

13 Visual C++ Programming 13

14 Arrays and Instance Methods Visual C++ Programming 14  Arrays may need to be passed into methods  Passing a single element into a method by value The actual argument is the array element name method_name(array_name[subscript]) Example of method call: ShowNum(num[0]);

15 Visual C++ Programming 15

16 Array elements as actual arguments Visual C++ Programming 16

17 Passing a single Element into a Method by Reference Visual C++ Programming 17  The actual argument must be a single array element  Example: GetNum(num[0]);  The formal parameter must be a reference parameter  Example: private: void GetNum( int& myNum) { … }

18 Visual C++ Programming 18

19 Passing an Entire Array into a Method Visual C++ Programming 19  The actual argument is the array name only – no subscripts  Example:  int num[5];  GenerateData(num);  The matching formal parameter  May have any name  Its data type must match that of the actual argument  The name is followed by an empty pair of square brackets  Example: private: void GenerateData(int numArr[]) { …}  When an entire array is passed in a copy of the values is not made – all changes to array values are made to the original (similar to pass by reference)

20 Visual C++ Programming 20

21 Visual C++ Programming 21

22 Visual C++ Programming 22

23 Visual C++ Programming 23

24 Visual C++ Programming 24

25 Visual C++ Programming 25

26 Visual C++ Programming 26

27 Sequential Search Visual C++ Programming 27  A sequential search is a process of looking for a particular value (target value) in an array  The array elements are accessed in order until  the target value is found  or there are no more array elements to examine

28 Visual C++ Programming 28

29 Visual C++ Programming 29

30 Searching with a for Loop Visual C++ Programming 30  A for loop can be used for a sequential search  The loop control variable starts at element 0  The loop condition is that the loop control variable must be less than the number of elements in the array  The update condition increments the loop control variable by 1  The loop body compares the element whose subscript is the loop control variable to the target value  If a match is found a break statement breaks out of the loop

31 Visual C++ Programming 31

32 Visual C++ Programming 32

33 Visual C++ Programming 33

34 Visual C++ Programming 34

35 Counting Comparisons Visual C++ Programming 35  To count the comparisons made by a sequential search use value in the loop control variable after the loop is over  The loop is over because either  The target was found and a break statement broke out of the loop  The target was not found after the values in all elements had been examined  In the first case the loop control variable contains the number of comparisons made  In the second case, the number of comparisons is one less than the loop control variable because the loop control variable

36 Visual C++ Programming 36

37 The Search Comparison Log Visual C++ Programming 37  Keeps track of the comparisons made  Provides the details of each comparison  Concatenates the details into a large output string  Displays the output string

38 Visual C++ Programming 38

39 Parallel Arrays Visual C++ Programming 39  Two or more arrays of the same length  Corresponding elements of each array are related  One array can be searched and the location result used to access related data in other arrays

40 Visual C++ Programming 40

41 Visual C++ Programming 41

42 Visual C++ Programming 42

43 Visual C++ Programming 43

44 Visual C++ Programming 44

45 Multidimensional Arrays Visual C++ Programming 45  Two or more dimensions  Two dimensional array  Rows (across)  Columns (down)  Each element has both a row and column identifier  Data is stored in contiguous fashion by row but can be envisioned as a table  Nested loops are used to traverse the array The outer loop identifies the row The inner loop identifies the column

46 Visual C++ Programming 46

47 Visual C++ Programming 47

48 Visual C++ Programming 48

49 Visual C++ Programming 49

50 Visual C++ Programming 50

51 Visual C++ Programming 51

52 Summary Visual C++ Programming 52 Arrays are data structures that store values in contiguous space in memory Fixed array sizes cannot be changed Array elements are designated using subscript values Loop control variables (especially in for loops) can be used to access values stored in each element of an array Summation Finding the largest value Sequential search

53 Summary (continued) Visual C++ Programming 53 Array elements can be passed into methods by value or by reference Actual arguments must identify the element by its subscript Entire arrays are passed into methods as actual arguments using the array name When entire arrays are passed in a copy of the array is not made (not passed by value) An array can be searched sequentially Until the target is found or there are no more elements to examine

54 Visual C++ Programming 54


Download ppt "VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)"

Similar presentations


Ads by Google