Docsity

C++ Programming: Arrays and Pointers, Lecture notes of Computer Programming

Partial preview of the text

Download C++ Programming: Arrays and Pointers and more Lecture notes Computer Programming in PDF only on Docsity!

Lecture 3

Arrays and Pointers

3.1 Introduction to one-dimensional arrays 3.2 Two-dimensional arrays

3. Introduction to one-dimensional arrays

Array Syntax

Declaring an Array

  • (^) An array, named score, containing five variables of type int can be declared as int score[ 5 ];
  • (^) This is like declaring 5 variables of type int: score[0], score[1], … , score[4]
  • (^) The value in brackets is called
    • (^) A subscript
    • (^) An index

Array Variable Types

  • (^) An array can have indexed variables of any type
  • (^) All indexed variables in an array are of the same type - (^) This is the base type of the array
  • (^) An indexed variable can be used anywhere an ordinary variable of the base type is used

Using [ ] With Arrays

  • (^) In an array declaration, [ ]'s enclose the size of the array such as this array of 5 integers: int score [5];
  • (^) When referring to one of the indexed variables, the [ ]'s enclose a number identifying one of the indexed variables - (^) score[3] is one of the indexed variables - (^) The value in the [ ]'s can be any expression that evaluates to one of the integers 0 to (size -1)

Loops And Arrays

  • (^) for-loops are commonly used to step through arrays - (^) Example: for (int i = 0; i < 5; i++) { cout << score[i] << " off by " << (max – score[i]) << endl; } could display the difference between each score and the maximum score stored in an array First index is 0 Last index is (size – 1)

Variables and Declarations

  • (^) Most compilers do not allow the use of a variable to declare the size of an array Example: cout << "Enter number of students: "; cin >> number; int score[number];
  • (^) This code is illegal on many compilers
  • (^) To do this, one must use DYNAMIC ARRAYS

Array Declaration Syntax

  • (^) To declare an array, use the syntax:
    • (^) Type_Name Array_Name[Declared_Size];
    • (^) Type_Name can be any type
    • (^) Declared_Size can be a constant to make your program more versatile
  • (^) Once declared, the array consists of the indexed variables: Array_Name[0] to Array_Name[Declared_Size -1]

Arrays and Memory

  • (^) Declaring the array int a[6]
    • (^) Reserves memory for six variables of type int
    • (^) The variables are stored one after another
    • (^) The address of a[0] is remembered
      • (^) The addresses of the other indexed variables is not remembered
    • (^) To determine the address of a[3]
      • (^) Start at a[0]
      • (^) Count past enough memory for three integers to find a[3]
  • Slide 7-
  • Slide 7-

Out of Range Problems

  • (^) If an array is declared as: int a[6]; and an integer is declared as: int i = 7;
  • (^) Executing the statement a[i] = 238; causes…
    • (^) The computer to calculate the address of the illegal a[7]
    • (^) (This address could be where some other variable is stored)
    • (^) The value 238 is stored at the address calculated for a[7]
    • (^) No warning is given!

Initializing Arrays

  • (^) To initialize an array when it is declared
    • (^) The values for the indexed variables are enclosed in braces and separated by commas
  • (^) Example: int children[3] = { 2, 12, 1 };
  • (^) Is equivalent to: int children[3]; children[0] = 2; children[1] = 12; children[2] = 1;