Вы находитесь на странице: 1из 13

INTRODUCTION TO ARRAY

C++

03/16/15

Outline
Introduction
Arrays
Declaring Arrays
Examples Using Arrays

Using looping statement


Characters

Assign

DEFINITION ARRAY
03/16/15

A collection of objects of the same type stored


contiguously in memory under one name
May be type of any kind of variable
May even be collection of arrays!

For ease of access to any member of array


For passing to functions as a group

Array

03/16/15

Group of consecutive memory


locations
Same name and type

To refer to an element, specify


Array name
Position number

Format:

arrayname[position number]

First element at position 0


n element array named c:
c[0], c[1]...c[n1]

03/16/15

Array elements are like normal variables


c[0] = 3;
cout<< c[0];
Perform

operations in subscript. If x equals 3

c[5-2] == c[3] == c[x]

03/16/15

Array
# of elements Size of
Array
Definition
Element
Size
-------------------------------------------------------------------------char letters[25]; 251 byte25 bytes
short rings[100];1002 bytes200 bytes
int miles[84]; 844 bytes336 bytes
float temp[12]; 12 4 bytes48 bytes
double distance
1000 8 bytes8000 bytes
[1000];

DECLARING ARRAYS
03/16/15

When declaring arrays, specify

Name
Type of array
Number of elements

arrayType arrayName[numberOfElements];

Examples:

int c[10];
float myArray[3284];
Declaring multiple arrays of same type

Format similar to regular variables


Example:

int b[ 100 ], x[ 27 ];

EXAMPLES USING ARRAYS


03/16/15

Initializers
int n[5] = { 1, 2, 3, 4, 5 };

If not enough initializers, rightmost elements become


0
int n[5] = { 0 }

All elements 0

If too many a syntax error is produced syntax error


C/C++ arrays have no bounds checking

If size omitted, initializers determine it


int n[ ] = { 1, 2, 3, 4, 5 };

5 initializers, therefore 5 element array

SAMPLE PROGRAM USING ARRAY

using namespace std;


int main()

03/16/15

#include <iostream>

Note: The program has


5 elements . Positioning
of array distance (counts
starts) is from 0 to 4.

{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28};
cout << "2nd member = " << distance[1] << endl;
cout << "5th member = " << distance[4] << endl;
system (pause);
return 0;
}

This would produce:


2nd member = 720.52
5th member = 6.28

using namespace std;

03/16/15

CREATING AN ARRAY
USING LOOPING
STATEMENT
# include <iostream>
int main ()
{
int arrayVar [9];
for (int Var = 0; Var<=8; Var+=2)
{
arrayVar [Var] = 90;
cout << Var<< " ---- " <<
arrayVar[Var]<<endl;
}
system ("pause");
return 0;
}

10

ARRAY USING CHARACTERS


using namespace std;
int main()

03/16/15

#include <iostream>

{
char Color[] = { 'B', 'l', 'a', 'c', 'k' };
char Country[] = "Philippines";
cout << "Color = " << Color << endl;
cout << "Country = " << Country;
system ("pause");
return 0;
}

11

TRY THIS!
03/16/15

Create a program that will ask the user to input


value of length and width of a rectangle and
display (compute) the area of a rectangle using
array.
Array type = float
Array name = rectangle
Array element = 3

rectangle[0]

= length
rectangle[1] = width
rectangle [2] = area

12

ASSIGN (FINALS)

Create a program that ask three input numbers


from the user and will determine the highest
number using array.

03/16/15

How will you determine the highest? ( thats


part of your assignment) try using if else.

13

Вам также может понравиться