C++ arrays


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C++ arrays

  1. 1. C++ ArraysMELJUN CORTES
  2. 2. OutlineAt the end of the lesson, students should be able to: 1. Describe arrays 2. Explain why arrays are necessary 3. Identify a one-dimensional array and how they are declared and used 4. Create programs using one-dimensional arrays 5. Identify operations that can be performed on one-dimensional arrays 6. Identify a two-dimensional array and how they are declared and used 7. Create programs using two-dimensional arrays
  3. 3. IntroductionConsider 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 variableBetter Solution:• Use LOOPS. (One variable will be enough for the 10 input values)Issue:• Impossible to access or recall let say the 5th or even the 9th 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.
  4. 4. Introduction (Arrays)• An array in C++ is a group of elements with the same data type referenced by a single name.• Characterized by: 1. data type 2. name 3. size 4. dimension• The elements in the array can then be referenced using a valid index.
  5. 5. 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.
  6. 6. One-Dimensional Array (cont.)• Below are some more examples of one-dimensional array declarations. int n1[5], n2[100], n3[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: n1[0] real[4] doub[500]• The following however are examples of invalid array element references: n2[-1] real[2.5] doub[1000]
  7. 7. Example#include <iostream> for (index = 0; index < 5; index++) real[index] = 2.54;using namespace std; cout << string[0] << "n";void main(void){ for (index = 0; index < 5; index++) char string[10]; cout << real[index] << "n"; int n1[5] = {2, 4, 6, 8, 10}; int index, n2[100], n3[200]; n3[0] = n1[0] + n2[0]; float real[5]; doub[55] = doub[2]/2.5 * n1[0]; double doub[1000]; cout << n3[0] << endl; string[0] = J; cout << doub[55] << endl; n2[0] = 5; } n3[0] = 10; doub[2] = 3.1416;
  8. 8. 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 TwoDArray[2][3];• The graphical representation of the two-dimensional array TwoDArray is shown below.
  9. 9. Two-Dimensional Array (cont.)• Upon declaration, TwoDArray will occupy a portion of the RAM in row-major order. The figure below simulates this behavior.
  10. 10. 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. The range of row index is from 0 to <row size> - 1 2. The range of column index is from 0 to <column size> - 1 3. The first element therefore is <array name>[0][0] 4. The last element is <array name>[<row size> - 1][<column size> - 1]• Examples of valid array access are: cout << TwoDArray[0][2] << “n”; // assumed initialized TwoDArray[1][2] = 25; TwoDArray[0][0] = TwoDArray[1][2] + 3;• Examples of invalid array access are: cout << TwoDArray[1,2] << “n”; TwoDArray[-1][1] = 1.25; TwoDArray[5][0] = 3.1416;
  11. 11. Two-Dimensional Array (cont.)#include <iostream>using namespace std;void main(void){ int TwoDArray[2][3] = {{1, 2, 3}, {4, 5, 6}}; int row, col; for (row = 0; row < 2; row++) for (col = 0; col < 3; col++) cout << "Row[" << row << "] Column[" << col << "] = " << Table[row][col] << "n";}