Chapter 8 - Arrays
Chapter 8 - Arrays
How would I
name the
variable??
INTRODUCING ARRAY
int tests[5];
1 --
-- -- --
0 1 2 3 4 5 6 7 8 9
A -- -- -- 1 -- -- -- -- -- --
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
ARRAY INITIALIZATION (cont..)
Using Loops for Sequential Access
Looping statement
can be used in array
for sequential
process
Array Square
Using the loop
counter as an array [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
to each array
element in turn
Example:
int square[SIZE], I;
The for loop
int square[SIZE], i;
for (i=0;i<SIZE; ++i)
square [i] = i * I;
EXAMPLE: Average Value
/*This program is to read 10 numbers and get an
arrays*/
#include<iostream>
using namespace std;
int main()
{
int x[10],total = 0,i;
int table[5][4];
1st Dimension
2nd Dimension
Allocate
Row:0-4
Allocate
Column:0-3
TWO DIMENSIONAL ARRAY
INITIALIZATION
The right index of an array is
X[0][0] 1
increased first before the left index X[0][1] 2
Example: X[0][2] 3
X[2][3] 12
int x[3]
[4]={1,2,3,4,5,6,7,8,9,10,11,12}
EXAMPLE
#include <iostream>
using namespace std;
main()
{
float b[3][4]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.10,11.1,12.12};
int i,j;
for (i=0;i<3;i++)
for(j=0;j<4;j++)
{
cout << "[" << i << "] [" << j << "] = " << b[i][j] << endl;
}
system("pause"); [0] [0] = 1.1
return (0); [0] [1] = 2.2
}
Output [0] [2] = 3.3
[0] [3] = 4.4
[1] [0] = 5.5
[1] [1] = 6.6
[1] [2] = 7.7
[1] [3] = 8.8
[2] [0] = 9.9
[2] [1] = 10.1
[2] [2] = 11.1
[2] [3] = 12.12