2D Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

2D Arrays

Description:

2D Arrays. Alina Solovyova-Vincent. Department of Computer Science & Engineering. University of Nevada, Reno. Spring 2006. Declarations ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 20
Provided by: ali75
Category:
Tags: arrays | nevada | reno

less

Transcript and Presenter's Notes

Title: 2D Arrays


1
2D Arrays
Alina Solovyova-Vincent Department of Computer
Science Engineering University of Nevada,
Reno Spring 2006
2
Declarations
  • datatype array_name num_of_rows num_of_col
  • int array_11010 // indexed from (0,0) to
    (9,9)
  • char array_234 //indexed from (0,0) to
    (2,3)
  • float array_3210 //indexed from (0,0) to
    (1,9)

The first index always indicates the row, the
second the column.
3
Declarations
  • char letters34
  • or
  • const int row 3const int col 4char
    lettersrowcol //preferred way

4
1-D vs. 2-D Arrays
0,0 0,1 0,2 0,3 0,4
1,0 1,1 1,2 1,3 1,4
2,0 2,1 2,2 2,3 2,4
3,0 3,1 3,2 3,3 3,4
4,0 4,1 4,2 4,3 4,4
0
1
2
3
4
5
int table43
  • 72 45 1562 10 019 99 2738 24 89

table22
6
Examples
  • float array_A122
  • char array_B73
  • int array_C410

7
Row-Major Order
  • 72 45 1562 10 019 99 2738 24 89
  • 72451562100199927382489 ? ?

8
Initialization
  • int nums34 2,4,6,8,1,3,5,7,5,2,7,10
  • int nums34 2,4,6,8,
  • 1,3,5,7,
  • 5,2,7,10
  • int nums34 2,4,6,8,
  • 1,3,5,7,
  • 5,2,7,10

9
Accessing All Elements of an Array
  • for ( i 0 i lt row i )
  • for( j0 jltcolumn j ) cout ltlt
    bunch_of_numsij ltlt \t
  • cout ltlt endl

10
Accessing Individual Elements
  • int numint bunch_of_nums1010bunch_of_n
    ums00 25num bunch_of_nums35

11
Using Subscript Expressions
  • result result value i 2 i num
  • cout ltlt num i row col-6

12
Worksheet
  • Declare an array to store 5x5 matrix of integers.
  • List all of the elements on the major diagonal.
  • List all of the elements on the minor diagonal.
  • Write a code segment that would display all of
    the elements on the major diagonal.
  • Write a code segment that would display all of
    the elements on the minor diagonal.

13
  • const int size 5
  • int squaresizesize
  • // display major diagonal in one loop
  • for (int i 0 i lt size i)
  • cout ltlt squareii ltlt endl
  • // display minor diagonal in one loop
  • for (int i 0 i lt size i)
  • cout ltlt squareisize-1-i ltlt endl

14
  • const int size 5
  • int squaresizesize
  • // display major diagonal in two loops
  • for (int i 0 i lt size i)
  • for (int j 0 j lt size j)
  • if (i j)
  • cout ltlt squareij ltlt endl
  • // display minor diagonal in two loops
  • for (int i 0 i lt size i)
  • for (int j 0 j lt size j)
  • if (i j size-1)
  • cout ltlt squareij ltlt endl

15
Worksheet
  • Write a code segment that declares
  • 45 x 33 array of integers and initializes
    all elements to 0. The program should then prompt
    the user for a location in that array ( row
    number, column number) and tell the user whether
    its a valid location ( i.e. within bounds). For
    example (4, 16) is a valid, (-3, 5) is invalid,
    (13, 56) is invalid. If location is valid,
    display the contents of that location.

16
  • const int nrow 45
  • const int ncol 33
  • int arrnrowncol 0
  • do
  • cout ltlt "Enter row and column "
  • cout ltlt r ltlt c
  • while (r lt 0 r gt nrow
  • c lt 0 c gt ncol)
  • cout ltlt "The element in ("
  • ltlt r ltlt ", " ltlt c ltlt ") is "
  • ltlt arrrc

17
2-D Arrays in Functions
  • void ProcessValues (int 5, int, int)
  • //works with ALL 2-d arrays that have
  • // exactly 5 columns i.e. 10x5, 2x5 .

18
  • int max(int 2, int, int)
  • void main()
  •    const int nrow 4, ncol 2    int
    numsnrowncol 3,2,5,13,1,7,9,4
  •  cout ltlt "The max value of the array is "
  • ltlt max(nums, nrow , ncol)
  • int max(int vals 2, int nr, int nc)
  •    int i, j, max vals00
  • for (i 0 i lt nr i)       for (j 0
    j lt nc j)          if (max lt valsij)
                 max valsij
  • return max

19
Worksheet
  • Write a function that receives an array of
    floating point values, number of rows and number
    of columns (known to be 7) in that array, and a
    special number between 0 and 6. The function
    should return the sum of all elements in the
    column that corresponds to the special number.
Write a Comment
User Comments (0)
About PowerShow.com