# Matrices by nikeborome

VIEWS: 14 PAGES: 24

• pg 1
```									Two-Dimensional Arrays

ELEC 206
Computer Applications for
Electrical Engineers
Dr. Ron Hayne
Two-Dimensional Arrays
 Row offset
row 0    2     3     -1
 Column offset                   row 1     0    -3     5
   x[2][1] = 6                row 2     2     6     3
 All values must have            row 3    -2    10     4
same data type                          col 0 col 1 col 2

   int x[4][3] = {{2, 3, -1}, {0, -3, 5},
{2, 6, 3}, {-2, 10, 4}};

206_C7                               2
2-D Arrays from Data Files
#include <fstream>
...
const int NROWS = 10;
const int NCOLS = 5;
...
double data[NROWS][NCOLS]
...
for (int i=0; i<=NROWS-1; i++)
{
for (int j=0; j<=NCOLS-1; j++)
{
data_file >> data[i][j];
}
}
206_C7         3
Computations with 2-D Arrays
double power[NROWS][NCOLS];
...
// Compute and print daily averages
for (int j=0; j<=NCOLS-1; j++)
{
col_sum = 0;
for (int i=0; i<=NROWS-1; i++)
{
col_sum += power[i][j];
}
cout << "Day " << j+1 << ": Avg = "
<< col_sum/NROWS << endl;
}

206_C7              4
Function Arguments
 Passing array information to a function
   Always call by reference
   Two-dimensional array
 Function also needs info about the declared column
size of the array
 int sum(int x[][NCOLS]);

206_C7                           5
Problem Solving Applied
   Problem Statement
   Determine and print the number of peaks and their
locations in an elevation grid.
   Input/Output Description

Peak locations

grid1.dat

206_C7                            6
Problem Solving Applied
   Hand Example (grid1.dat)
6 7
5039      5127    5238       5259   5248   5310   5299
5150      5392    5410       5401   5320   5820   5321
5290      5560    5490       5421   5530   5831   5210
5110      5429    5430       5411   5459   5630   5319
4920      5129    4921       5821   4722   4921   5129
5023      5129    4822       4872   4794   4862   4245
   Peak Locations
   [2][1], [2][5], [4][3]

206_C7                           7
Problem Solving Applied
 Algorithm Development
   main
   read nrows and ncols from data file
   read terrain data into array
   for i=1 to nrows-2
 for j=1 to ncols-2
 if (ispeak(grid, i, j))
print peak location

206_C7          8
Problem Solving Applied
 Algorithm Development
   ispeak
   if ((grid[i-1][j]<grid[i][j]) &&
(grid[i+1][j]<grid[i][j]) &&
(grid[i][j-1]<grid[i][j]) &&
(grid[i][j+1]<grid[i][j]))
return true;
   else
return false;

206_C7      9
/*   Program chapter7_2                         */
/*                                              */
/*   This program determines the locations of   */
/*   peaks in an elevation grid of data.        */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int const N = 25;

// Function prototypes.
bool is_peak(double grid[][N], int r, int c);

int main()
{
// Declare objects.
int nrows, ncols;
double elevation[N][N];
string filename;
ifstream file1;

// Prompt user for file name and open file for input.
cout << "Enter the name of the input file.\n";
cin >> filename;

file1.open(filename.c_str());
if(file1.fail())
{
cerr << "Error opening input file\n";
return 1;
}
file1 >> nrows >> ncols;
if(nrows > N || ncols > N)
{
cerr << "Grid is too large, adjust program.";
return 1;
}
// Read information from data file into array.
for (int i=0; i<=nrows-1; i++)
{
for (int j=0; j<=ncols-1; j++)
{
file1 >> elevation[i][j];
}
}
// Determine and print peak locations.
cout << "Top left point defined as row 0, column 0\n";
for (int i=1; i<=nrows-2; i++)
{
for (int j=1; j<=ncols-2; j++)
{
if(is_peak(elevation, i, j))
{
cout << "Peak at row: " << i
<< " column: " << j << endl;
}
}
}
// Exit program.
system("PAUSE");
return 0;
}
// is peak function
bool is_peak(double grid[][N], int i, int j)
{
if ((grid[i-1][j]<grid[i][j]) &&
(grid[i+1][j]<grid[i][j]) &&
(grid[i][j-1]<grid[i][j]) &&
(grid[i][j+1]<grid[i][j]))
return true;
else
return false;
}
Testing

206_C7   15
Matrices
 Matrix
   Set of numbers arranged in a rectangular grid
   Row and column numbers begin with 1
   Can use two-dimensional array to store matrix
   Must be careful translating equations in matrix notation into C++
statements because of difference in subscripting
 Example
   2x3                 1 0     2
B
  1 4  2


206_C7                                    16
Matrices
 Determinant
 a1,1 a1, 2 
|A| = a1,1a2,2 - a2,1a1,2            A
a2,1 a2, 2 


            

 Transpose

 1  1
 1 0     2
B                               B T  0    4
  1 4  2
                                
 2  2
      

206_C7                      17
Transpose Function
void transpose(int b[][NCOLS], int bt[][NROWS])
{
for (int i=0; i<=NROWS-1; i++)
{
for (int j=0; j<=NCOLS-1; j++)
{
bt[j][i] = b[i][j];
}
}
return;
}

206_C7                      18
 Add (subtract) elements in corresponding positions
in the matrices
   Matrices must be same size

2 5 1                      1 0     2
A                         B
 0 3  1
                    1 4  2


 3 5     3
A B  
  1 7  3


206_C7                    19
Matrix Multiplication
 The value in position ci,j is the product of row i of
the first matrix and column j of the second matrix
   Inner dimensions must match                     n

   e.g. 2 x 3 3 x 2                      ci , j   ai ,k bk , j
k 1

 1  1
2 5 1            B T  0    4
A
 0 3  1

      
 2  2
      

 4 16
C  AB  
T

  2 14


206_C7                                     20
N x N Multiplication Function
void matrix_mult(int a[][N], int b[][N], int c[][N])
{
for (int i=0; i<=N-1; i++)
{
for (int j=0; j<=N-1; j++)
{
c[i][j] = 0
for (int k=0; k<=N-1; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
return;
}                     206_C7                      21
Summary
 Two-Dimensional Arrays
 Problem Solving Applied
 Matrices
   Use MATLAB
 End of Chapter Summary
   C++ Statements
   Style Notes
   Debugging Notes

206_C7   22
Test #2 Review
 Modular Programming
   Modularity
   Programmer Defined Functions
   Parameter Passing
   Storage Class and Scope
 Arrays
   One-Dimensional Arrays
   Statistical Measurements
   Functions Revisited

206_C7     23
Test #2 Review
 Two-Dimensional Arrays
   Matrices
 Applications
   Random Numbers
   Sorting

206_C7   24

```
To top