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]

 

 

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;                       

 

            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

 

 

short array2d[6][4];

 

 

 

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;

                  }

}

 

 

 

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;

                        }

}

 

 

 

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;

}

 

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

 

 

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;

                       

            }

}

 

 

 

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];

 

Memory Representation:

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:

Memory Representation:

 

 

Column Major Storage Model:

Columns are stored in consecutive addresses in the memory

Graphical Representation:

Memory Representation:

 

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