C Arrays MELJUN CORTES revised2015 may Outline At

  • Slides: 11
Download presentation
C++ Arrays MELJUN CORTES (revised>2015. may)

C++ Arrays MELJUN CORTES (revised>2015. may)

Outline At the end of the lesson, students should be able to: 1. 2.

Outline At the end of the lesson, students should be able to: 1. 2. 3. 4. 5. 6. 7. Describe arrays Explain why arrays are necessary Identify a one-dimensional array and how they are declared and used Create programs using one-dimensional arrays Identify operations that can be performed on one-dimensional arrays Identify a two-dimensional array and how they are declared and used Create programs using two-dimensional arrays

Introduction Consider the following declarations: int a, b, c, d, e, f, g, h,

Introduction Consider the following declarations: int a, b, c, d, e, f, g, h, i, j; Issues: 1. Variables of the same datatype, i. e. int. 2. Individual operation (e. g. , input) is needed for each variable Better Solution: • Use LOOPS. (One variable will be enough for the 10 input values) Issue: • Impossible to access or recall let say the 5 th or even the 9 th value after executing the needed operation. Best Solution: • Declare an array variable that will allow us to store values in contiguous memory location, perform some operations, and still have the control over previously accessed values. This principle is what we call an array.

Introduction (Arrays) • • An array in C++ is a group of elements with

Introduction (Arrays) • • An array in C++ is a group of elements with the same data type referenced by a single name. Characterized by: 1. 2. 3. 4. • data type name size dimension The elements in the array can then be referenced using a valid index.

One-Dimensional Array • • A one-dimensional array in C++ is also referred to as

One-Dimensional Array • • A one-dimensional array in C++ is also referred to as a list. A one-dimensional array declaration has the format : <data type> <array name> [<size>] • To reference an element, the syntax is <array name> [<index>] • For example, to declare an array that can store a string of up to 10 characters, we can use char string[10]; • In this declaration, the first element can be referenced through string[0], the second element with string[1], the third element using string[2], and runs up to the last element accessed through string[9]. In general, the range of values that we can use as an array index is from 0 to <size> – 1. •

One-Dimensional Array (cont. ) • Below are some more examples of one-dimensional array declarations.

One-Dimensional Array (cont. ) • Below are some more examples of one-dimensional array declarations. int n 1[5], n 2[100], n 3[200]; float real[5]; double doub[1000]; • • • Arrays of the same type can be declared on the same line separated by commas. Declarations can also be a mixed of ordinary variables and arrays of the same type. Below are examples of valid array element references are based on the declarations above: n 1[0] real[4] doub[500] • The following however are examples of invalid array element references: n 2[-1] real[2. 5] doub[1000]

Example #include <iostream> for (index = 0; index < 5; index++) real[index] = 2.

Example #include <iostream> for (index = 0; index < 5; index++) real[index] = 2. 54; using namespace std; cout << string[0] << "n"; void main(void) { char string[10]; int n 1[5] = {2, 4, 6, 8, 10}; int index, n 2[100], n 3[200]; float real[5]; double doub[1000]; string[0] = 'J'; n 2[0] = 5; n 3[0] = 10; doub[2] = 3. 1416; for (index = 0; index < 5; index++) cout << real[index] << "n"; n 3[0] = n 1[0] + n 2[0]; doub[55] = doub[2]/2. 5 * n 1[0]; cout << n 3[0] << endl; cout << doub[55] << endl; }

Two-Dimensional Array • • • Also referred to as a table A two dimensional

Two-Dimensional Array • • • Also referred to as a table A two dimensional array has two indexes. The first index refers to the row, while the second refers to column. The syntax in declaring two-dimensional arrays in C++ is: <data type> <array name> [ <row size> ] [<column size>] • In a two-dimensional array, the array elements are arranged in row-major order. Consider the sample declaration: int Two. DArray[2][3]; • The graphical representation of the two-dimensional array Two. DArray is shown below.

Two-Dimensional Array (cont. ) • Upon declaration, Two. DArray will occupy a portion of

Two-Dimensional Array (cont. ) • Upon declaration, Two. DArray will occupy a portion of the RAM in row-major order. The figure below simulates this behavior.

Two-Dimensional Array (cont. ) • To reference an element in a two-dimensional array the

Two-Dimensional Array (cont. ) • To reference an element in a two-dimensional array the syntax is: <array name> [<row index>][<column index>] • The following are some key points regarding two-dimensional arrays: 1. 2. 3. 4. The range of row index is from 0 to <row size> - 1 The range of column index is from 0 to <column size> - 1 The first element therefore is <array name>[0][0] The last element is <array name>[<row size> - 1][<column size> - 1] • Examples of valid array access are: cout << Two. DArray[0][2] << “n”; // assumed initialized Two. DArray[1][2] = 25; Two. DArray[0][0] = Two. DArray[1][2] + 3; • Examples of invalid array access are: cout << Two. DArray[1, 2] << “n”; Two. DArray[-1][1] = 1. 25; Two. DArray[5][0] = 3. 1416;

Two-Dimensional Array (cont. ) #include <iostream> using namespace std; void main(void) { int Two.

Two-Dimensional Array (cont. ) #include <iostream> using namespace std; void main(void) { int Two. DArray[2][3] = int row, col; {{1, 2, 3}, {4, 5, 6}}; for (row = 0; row < 2; row++) for (col = 0; col < 3; col++) cout << "Row[" << row << "] Column[" << col << "] = " << Table[row][col] << "n"; }