1 / 24

Two-Dimensional Arrays

Two-Dimensional Arrays. ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne. 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}};.

beauregard
Download Presentation

Two-Dimensional Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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}}; 206_C7

  3. 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

  4. 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

  5. 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]); 206_C7

  6. Problem Solving Applied • Terrain Navigation • 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

  7. 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

  8. 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

  9. 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

  10. /* 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() {

  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. Testing 206_C7

  16. 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 206_C7

  17. Matrices • Determinant • |A| = a1,1a2,2 - a2,1a1,2 • Transpose 206_C7

  18. 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

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

  20. 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 • e.g. 2 x 3 3 x 2 206_C7

  21. 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

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

  23. 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

  24. Test #2 Review • Two-Dimensional Arrays • Matrices • Applications • Random Numbers • Sorting 206_C7

More Related