Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

Similar presentations


Presentation on theme: "Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne."— Presentation transcript:

1 Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

2 206_C72 Two-Dimensional Arrays  Row offset  Column offset x[2][1] = 6  All values must have same data type int x[4][3] = {{2, 3, -1}, {0, -3, 5}, {2, 6, 3}, {-2, 10, 4}}; row 023 row 10-35 row 2263 row 3-2104 col 0col 1col 2

3 206_C73 2-D Arrays from Data Files #include... 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]; }

4 206_C74 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; }

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

6 206_C76 Problem Solving Applied  Terrain Navigation Problem Statement Determine and print the number of peaks and their locations in an elevation grid. Input/Output Description grid1.dat Peak locations

7 206_C77 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]

8 206_C78 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

9 206_C79 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;

10 /* Program chapter7_2 */ /* */ /* This program determines the locations of */ /* peaks in an elevation grid of data. */ #include using namespace std; int const N = 25; // Function prototypes. bool is_peak(double grid[][N], int r, int c); int main() {

11 // 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; }

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

13 // 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; }

14 // 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; }

15 206_C715 Testing

16 206_C716 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 2 x 3

17 206_C717 Matrices  Determinant |A| = a 1,1 a 2,2 - a 2,1 a 1,2  Transpose

18 206_C718 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; }

19 206_C719 Matrix Addition (Subtraction)  Add (subtract) elements in corresponding positions in the matrices Matrices must be same size

20 206_C720 Matrix Multiplication  The value in position c i,j is the product of row i of the first matrix and column j of the second matrix Inner dimensions must match e.g. 2 x 3 3 x 2

21 206_C721 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; }

22 206_C722 Summary  Two-Dimensional Arrays  Problem Solving Applied Terrain Navigation  Matrices Use MATLAB  End of Chapter Summary C++ Statements Style Notes Debugging Notes

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

24 206_C724 Test #2 Review  Two-Dimensional Arrays Matrices  Applications Random Numbers Sorting


Download ppt "Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne."

Similar presentations


Ads by Google