arrays

14
C ARRAYS One-dimensional Array

Upload: notre-dame-of-midsayap-college

Post on 12-Feb-2017

165 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Arrays

CARRAYSOne-dimensional Array

Page 2: Arrays

One-dimensional arrayA one-dimensional array is a list related values. (Lab. Manual, Page 92)

You are knowledgeable & experience of the following:1. Declare one-dimensional array2. Initialize one-dimensional array3. Understand the initialization and declaration of a one-dimensional array4. Perform fundamental operations of a one-dimensional array

An array is a series of elements of the same type. It means that, we can store 10 values of type INT in an array without having to declare 10 different values of the same type INT.

Example:

int a1;int a2;int a3;and so on…

Instead this way int a[10];

Page 3: Arrays

Declaration of an arrayLike regular variable, an array must be declared before it is used.

Type name[elements]

Where type is a valid type (int, float,)Where name is a valid identifierWhere elements specifies the size of an array

Example:

int testArray[10];

An individual element of an array is accessed through index.An index describes the position of an element within an array.Arrays have zero as the index of the first element.

In the example.How many elements?What is the first element?What is the last element?

Page 4: Arrays

Initializing arrayTo assign initial values to each one of its elements by enclosing the values in curly braces { }.

Example:

int ArraySample[5] = {10,20,30,40,50};

The compiler will identify or assume the size of an array that will matches the number of values that Is included between the curly braces.

After declaration the above example, array sample would be 5 int long, because we initialized 5 values.

Page 5: Arrays

Accessing the values of an arrayWe can access the value of any elements individually like a normal variable.

Format/Syntax:

Name[index];

Example:ArraySample[2];

To store a valueArraySample[2]=70;

To pass a valuea = ArraySample[2];

Previous example:

int ArraySample[5] = {10,20,30,40,50};

What is the last element?What is the value of second element?

If we write/access ArraySample[5]. You are accessing out-of-range elements.

Where you experience the error? compilation or runtime?

Page 6: Arrays

Example program (Problem)Create a program that will allow a user to enter four scores of the quizzes and display the average.

Output:Display the average of the four quizzes on the screen using for loop statement.

See lab manual page 95

Page 7: Arrays

Two dimensional Arrays

•A multi-dimensional arrays described as “arrays of arrays”

Page 8: Arrays

At the end of this topic, you should be able to:

• Declare a two-dimensional array• Perform fundamentals operations on a two-dimensional array• Use parameters for a two-dimensional arrays

Page 9: Arrays

The simplest form of the multidimensional array is a two –dimensional array.

A two-dimensional array is a list of one-dimensional arrays.

A two-dimensional array can be understood as a table containing rows and columns.

Format/Syntax:type name[rows][columns];

Example: to declare an arrayint arraySample[a][b];

Where type can be any valid c++ data typeWhere arraySample can be a valid identifierWhere [a] and [b] is the rows and columns

Two-dimensional arrays are declared by specifying the number of rows and columns.

Page 10: Arrays

A two-dimensional array can be think as a table which will have an X number of rows and Y number of columns.

Example below is a two-dimensional array which contains three rows and four columns. myArray[rows][columns];

  Column 0 Column 1 Column 2 Column 3Row 0 myArray [0]

[0]myArray [0][1]

myArray [0][2]

myArray [0][3]

Row 1 myArray [1][0]

myArray [1][1]

myArray [1][2]

myArray [1][3]

Row 2 myArray [2][0]

myArray [2][1]

myArray [2][2]

myArray [2][3]

Every element in array myArray is identified by an element name of the form myArray[rows][columns]

Where myArray is the name of the array.Where [rows] and [columns] are the subscript that uniquely identified each element in myArray.

Page 11: Arrays

Initializing two-dimensional arraysYou can specify initial values by enclosing each row in curly braces { }.

Example 1:

char board[3][3] = {{‘x’, ‘x’, ‘o’},{‘o’, ‘o’, ‘x’},{‘x’, ‘o’, ‘ ‘}};

If some elements are omitted in the initialization list, they are set to zero.

Example 2:

int a[3][4]= {{0,1,2,3}, \\initialize for row indexed by 0 {4,5,6,7}, \\initialize for row indexed by 1 {8,9,10,11}}; \\initialize for row indexed by 2

The nested braces, which indicate the ntended row are optional.

You can write as:int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Page 12: Arrays

Subscripting two-dimensional array elementsNote: Passing over all elements of a two-dimensional array is usually done with nested loops.

Write subscript as x[row][col]. (Lab Manual page 100)

Accessing two-dimensional array elementsAn element in two-dimensional array is accessed by using the subscript,ROW Index and COLUMN index of the array.

EXAMPLE:

int val = a[2][3] // this will take 4th element from the 3rd row of the array. To verify page 99.

Page 13: Arrays

#include <iostream>using namespace std;

int main(){

int a[5][2] = {{0,0},{1,2},{2,4},{3,6},{4,8}};

for(int n=0;n<5;n++)for(int m=0;m<2;m++){

cout<<"a["<<n<<"]["<<m<<"]";

cout<<a[n][m]<<endl;

}system("pause");return 0;

}

Example program (Problem) Page 101OUTPUT

|name| rows|columns|values|a[0][0]0a[0][1]0a[1][0]1a[1][1]2a[2][0]2a[2][1]4a[3][0]3a[3][1]6a[4][0]4a[4][1]8

R|C{0,0}{1,2}{2,4}{3,6}{4,8}

Page 14: Arrays

Assignment Lab Manual page 103