Word Version

advertisement
Note 3: One, Two, and Three Dimensions Array Data Manipulation (Compile Time)
One Dimension Array:
Data stored in consecutive address in the memory.
Syntax:
DataType ArrayName[Constant Integer Expression] ;
.
short array1d[24]
0
1 2 3
4 5
6 7
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
array1d+20
array1d+3
Address syntax
(array1d+n) element address
Value syntax
array1d[n] or *(array1d+n)
Example of data manipulation:
Write the function to compute any power of 2 numbers by using one dimension array.
How many digits store in each element is depend on what type of array, such as unsigned
long array will store a maximum of 5 digits per element of the array.
4 ways to compute the power of 2 numbers:
1. Adding the value to itself (adding 2 each time)
2. Multiplying the value by 2
3. Shifting the value to the left one bit
4. Shifting the value to the left 15 bits (using for compute greater than 15 power of
power of 2 numbers)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Source Code:
//Function for compute power of 2 number by using adding the value itself
usigned long pow2[32767];
//Array for stored power of 2 numbers
long p_num;
//The power number
short num = 32767;
//Array size
:
short adding(unsigned long pow2[], long p_num, short num)
{
short begin = num-1,
end = num-1,
curry, i;
unsigned long carry;
C++ Programming Note 3
13
pow2[begin] = 1;
//Use for loop to control how many time to do the power
for(i = 0; i < p_num; i++)
{
current = end;
carry = 0;
//Uses while loop to control compute the number
while(current >= begin)
{
pow2[current] + = (pow2[current]+carry);
//Uses if statement to check carry or not
if(pow2[current] >= 99999)
{
carry = pow2[current]/100000;
pow2[current] – = (carry*100000);
}
else //If pow2[current] less than 99999 do the statement
{
carry = 0;
}
current – –;
}
if(carry > 0) //If carry greater than 0 do the statements
{
begin – –;
pow2[begin] = carry;
}
}
return begin;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Two Dimension Array:
Data stored by rows in consecutive addresses in the memory.
Syntax:
DataType ArrayName[ConstIntExpression][ ConstIntExpression] ;
↑
↑
Rows
Columns
C++ Programming Note 3
short array2d[6][4];
Row 0
Row 1
0
1 2 3
array2d[0]
4 5
6 7
array2d[1]
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Row 2
Row 4
Row 5
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
array2d[2]
Address syntax
array2d[r] or *(array2d+r) row address
(array2d[r]+c) or (*(array2d+r)+c) element
address
Value syntax
array2d[r][c], *(array2d[r]+c), or
*(*(array2d+r)+c)
Row 3
14
array2d[3]
array2d[4]
array2d[5]
array2d[0]
0
1
2
3
Row 0
array2d[1]
4
5
6
7
Row 1
array2d[2]
8
9
10
11
Row 2
array2d[3]
12
13
14
15
Row 3
array2d[4]
16
17
18
19
Row 4
array2d[5]
20
21
22
23
Row 5
array2d[5]+1
Matrix Transposition:
1. The function definition for transposing an array declared to have two dimensions with
the same size.
void matrix_transpose1(int fnum[][size], int side)
{
int i, j, temp;
for(i = 0; i < side – 1; i++)
for(j = i + 1; j < side; j++)
{
temp = fnum[i][j];
fnum[i][j] = fnum[j][i];
fnum[j][i] = temp;
}
}
C++ Programming Note 3
15
Before transposition:
After transposition:
0
1
2
0
1
2
0
1
2
3
0
1
4
7
1
4
5
6
1
2
5
8
2
7
8
9
2
3
6
9
2. The function definition for transposing an array declared to have two dimensions that
have a different number of rows and columns of data.
void matrix_transpose2(int fnum[][size], int &rows, int &cols)
{
int i, j, temp, size;
size = rows;
if (rows < cols)
//check for data without the same number of rows & cols
{
if (rows < cols)
size = cols; //size will contain the larger of rows or cols
temp = rows;
rows = cols;
cols = temp;
}
for(i = 0; i < size – 1; i++)
//transpose square data
for(j = i + 1; j < size; j++)
{
temp = fnum[i][j];
fnum[i][j] = fnum[j][i];
fnum[j][i] = temp;
}
}
C++ Programming Note 3
After transposition:
Before transposition:
0
1
2
0
1
2
3
1
4
5
6
2
16
3
0
1
0
1
4
1
2
5
2
3
6
2
3
3. The function definition for transposing an array declared to have two dimensions that
have a different number of rows and columns of data using linear addressing.
int matrix_transpose3(int *fnum, int &rows, int &cols, int row_size, int col_size)
{
int i, j, temp, size, result = 1;
if(rows <= col_size && cols <= row_size)
{
size = rows;
if(rows != cols) //check for data without the same number of rows & cols
{
if(rows < cols)
size = cols;
//size will contain the larger of row or cols
temp = rows;
//swap the values in rows and cols
rows = cols;
cols = temp;
}
for(i = 0; i < size – 1; i++)
//transpose square data
for(j = i + 1; j < size; j++)
{
temp = *(fnum + i * col_size + j);
*(fnum + i * col_size + j) = *(fnum + j * col_size + i);
*(fnum + j * col_size + i) = temp;
}
}
else
result = 0; //a result of zero means the matrix could not be transposed
return result;
}
C++ Programming Note 3
17
Matrix Multiplication:
Requirement for doing the matrix multiplication
1. ArrayA’s columns size must be the same as ArrayB’s rows size
2. Product Array’s rows size is same as ArrayA’s rows size; Product Array’s
columns size is same as ArrayB’s columns size
Array A
X
Array B
=
Product Array
0
1
2
3
4
5
0
1
2
3
4
0
175
190
205
220
1
6
7
8
9
10
1
5
6
7
8
1
400
440
480
520
2
11
12
13
14
15
2
9
10
11
12
2
625
690
755
820
3
16
17
18
19
20
3
13
14
15
16
3
850
940 1030 1120
0
1
2
3
4
4
17
18
19
20
0
1
2
3
0
1
2
Multiplication Algorithm:
short ArrayA[4][5], ArrayB[5][4], ProductArray[4][4];
short i, j, k, row_A = 4, cols_B = 4, cols_A = 5sum;
:
:
for ( i = 0; i < row_A; i++)
{
for (j = 0; j < cols_B; j++)
{
sum = 0;
// sum’s type must be able to hold product value
for (k = 0; k < cols_A; k++)
{
sum += (ArrayA[ i ][ k ] * ArrayB[ k ][ j ]);
}
ProductArray[ i ][ j ] = sum;
}
}
3
C++ Programming Note 3
18
Three Dimension Array:
Data stored by rows by pages in consecutive addresses in the memory.
Syntax:
DataType ArrayName[ConstIntExpression][ConstIntExpression][ConstIntExpression] ;
↑
↑
↑
Pages
Rows
Columns
Graphical Representation: short array3d[3][4][6];
24
25
26
27
28
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
29
array3d[2]
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
array3d[1]
Page 2
array3d[0]
24
25
26
27
28
29
0
1
2
3
4
5
30
31
32
33
34
35
6
7
8
9
10
11
36
37
38
39
40
41
12
13
14
15
16
17
42
43
44
45
46
47
18
19
20
21
22
23
Page 1
Page 0
Memory Representation:
array3d[0]
Row 0
Page 0
array3d[0][3]
Row 3
array3d[1][2]+2
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
array3d[2]
Page 2
array3d[0][2]
Row 2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
array3d[1]
Page 1
array3d[0][1]
Row 1
array3d[2][3]+4
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
C++ Programming Note 3
19
Address syntax
array3d[p] or *(array3d+p) page address
array3d[p][r], *(array3d[p]+r) or *(*(array3d+p)+r) row address
(array3d[p][r]+c), (*(array3d[p]+r)+c) or (*(*(array3d+p)+r)+c) element address
Value syntax
array3d[p][r][c], *(array3d[p][r]+c), *(*(array3d[p]+r)+c) or *(*(*(array3d+p)+r)+c)
Row Major Storage Model:
Rows are stored in consecutive addresses in the memory
Graphical Representation:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Memory Representation:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Column Major Storage Model:
Columns are stored in consecutive addresses in the memory
Graphical Representation:
0
4
8
12
16
0
4
8
12
16
1
5
9
13
17
1
5
9
13
17
2
6
10
14
18
2
6
10
14
18
3
7
11
15
19
3
7
11
15
19
19
C++ Programming Note 3
20
Memory Representation:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
As memory representation pictures show that row major storage and column major
storage are the same in the memory. The different is graphical representation which is
how data is being print out for different storage model.
3 Dimension Array in Fortran:
Language of the Fortran is column major; it is different in memory storage from C/C++.
In syntax for array representation, the array dimension size greater than 2 are different
from C/C++.
Syntax for the Fortran:
DataType ArrayName[ConstIntExpression][ConstIntExpression][ConstIntExpression] ;
↑
↑
↑
Rows
Columns
Pages
Download