Numeric Arrays Page 1 Numeric Arrays Array def

  • Slides: 5
Download presentation
Numeric Arrays Page 1 Numeric Arrays • Array: (def) A Regular Order or Arrangement

Numeric Arrays Page 1 Numeric Arrays • Array: (def) A Regular Order or Arrangement • For our Purposes, we can define an array as a data structure containing a fixed number of contiguous storage elements all of the same type Assume we wished to store the squares of the numbers: Number: 0 1 2 3 4 5 6 7 8 9 Square: 0 1 4 9 16 25 36 49 64 81 We could store the numbers as scalar variables: int d 0 = 0, d 1 = 1, d 2 = 4, d 3 = 9, d 4 = 16, d 5 = 25, d 6 = 36, d 7 = 49, d 8 = 64, d 9 = 81; Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 2 The problem with this approach is: • Remembering the variables

Numeric Arrays Page 2 The problem with this approach is: • Remembering the variables names can become complex • Manipulating each of the variables is tedious For Example, suppose we were trying to find the value 64 in our list, but didn’t know where it was stored: if (d 0 == 64) printf(“at d 0’’); else if (d 1 == 64) printf(“at d 1’’); else if (d 2 == 64) printf(“at d 2’’); else if (d 3 == 64) printf(“at d 3’’); else if (d 4 == 64) printf(“at d 4’’); else if (d 5 == 64) printf(“at d 5’’); else if (d 6 == 64) printf(“at d 6’’); else if (d 7 == 64) printf(“at d 7’’); else if (d 8 == 64) printf(“at d 8’’); else if (d 9 == 64) printf(“at d 9’’); else printf(“Not found”); Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 3 A preferred alternative would be to store the squares of

Numeric Arrays Page 3 A preferred alternative would be to store the squares of the numbers as an integer array: int numvector[10]; What does this declaration do ? ? ? • Using the datatype int implies that we will require 2 bytes per element • the variable name numvector will be associated with the base address of the array • [10] indicates how many elements will be in our array • since we are creating an array of type int, we are requesting 2 * 10 = 20 CONTINGUOUS bytes of RAM Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 4 We could also initialize our array when we declare it:

Numeric Arrays Page 4 We could also initialize our array when we declare it: int numvector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; OR int numvector[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; Even though we do not explicitly indicate how many elements are in the array, we are still requesting 20 bytes of CONTIGUOUS storage (We are just asking the compiler to determine the number) Because we are specifying only one subscript or offset, offset we are declaring a vector (a one-dimensional array) Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Page 5 This Concludes The Slides for this Section Choose an Option: Repeat Slides

Page 5 This Concludes The Slides for this Section Choose an Option: Repeat Slides for this Section Go To Next Set of Slides For this Chapter Go To Slide Index For Chapter 4 Go To Slide Index For Chapter 5 Go To Slide Index For Textbook Go To Home Page Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft