Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9

download Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9

of 43

  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Embed Size (px)

Transcript of Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9

  • Slide 1
  • Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9
  • Slide 2
  • 3/13/02 Program 4 questions? revised test1 and my version of the program program should always process at least one customer Program 6 (and HW2) may be done by two people working together
  • Slide 3
  • 2 9.1 The Array Data Type Array collection of elements with a common name Entire array is referenced through the name Array elements are of the same type the base type Base type can be any fundamental, library- defined, or programmer -defined type For now: arrays are static - they have a fixed number of elements
  • Slide 4
  • 4 Array Elements Individual elements of the array are referenced by sub_scripting the group name Subscripts are denoted as expressions within brackets: [ ] The index type is integer and the index range must be 0... n -1 where n is the number of elements in the array
  • Slide 5
  • 6 Array Declaration
  • Slide 6
  • 7 Sample Declarations Suppose const int N = 20; const int M = 40; const int MaxStringSize = 80; const int MaxListSize = 1000 ;
  • Slide 7
  • 8 Sample Declarations Then the following are all correct array declarations. int A[10]; char B[MaxStringSize]; float C[M*N]; int Values[MaxListSize]; Rational D[N-15];
  • Slide 8
  • Array Initialization An array can be initialized at the time it is declared char grades[5] = {'A', 'B', 'C', 'D', 'F'}; int primes[] = {1, 2, 3, 5, 7, 11, 13, 17, 19, 23} if values provided do not fill the array, remainder of elements will be 0 double x[10] = {1.5, 2.5, 3.5, 4.5, 5.5} > A[k]; // where the next input value is 3">
  • 13 Array Element Manipulation cin >> A[k]; // where the next input value is 3
  • Slide 14
  • Array Processing None of the operators are defined for an array > arithmetic comparison To do the same thing to every element in an array, use a loop
  • Slide 15
  • 14 Inputting Into An Array int A[MaxListSize]; int n = 0; int CurrentInput; while((n > CurrentInput)) { A[n] = CurrentInput; ++n; }
  • Slide 16
  • 15 Displaying An Array // List A of n elements has // already been set for (int i = 0; i < n; ++i) { cout