Two Dimensional Arrays - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Two Dimensional Arrays

Description:

i, j, k; Gibbons | GNG1101-10: 15. Output. for(i=0; i 4; i ) for(j=0; j 3; j ) for(k=0; k 2; k ... %dn',i,j,k,m[i][j][k]); Gibbons | GNG1101-10: 16. Out of ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 27
Provided by: davidg84
Category:

less

Transcript and Presenter's Notes

Title: Two Dimensional Arrays


1
Two Dimensional Arrays
  • float p210
  • 20 floats being associated as 10 pairs.
  • p0i can be taken as equivalent to xi
  • p1i can be taken as equivalent to yi

2
Weather Balloon
  • As a weather balloon is used to measure
    temperature as it rises.
  • Measurements are taken at fixed time intervals.
  • We need to keep track of the height as well as
    the temperature.

3
Height vs Time
height
time
4
Measure Height Temperature
  • float fBalloon210, fSumOfHT0.0
  • int i,n
  • printf("How many points? ") scanf("d",n)
  • for(i0 iltn i)
  • printf(height and temperature values at d
    ",i)
  • scanf("ff", fBalloon0i,
    fBalloon1i)
  • for(i0 iltn i)
  • fSumOfHTfSumOfHT
  • fBalloon0i fBalloon1i

5
Input
  • float fBalloon210, fSumOfHT0.0
  • int i,n
  • printf("How many points? ")
  • scanf("d",n)
  • for(i0 iltn i)
  • printf(height and temperature
  • values at d ",i)
  • scanf("ff", fBalloon0i,
    fBalloon1i)

6
Calculation
  • for(i0 iltn i)
  • fSumOfHTfSumOfHT
  • fBalloon0i fBalloon1i

7
Initialization
  • include ltstdio.hgt
  • void main()
  • int i, j, k2500,01,02,03,04,
  • 10,11,12,13,14
  • float f520.0, 0.1,
  • 1.0,1.1,
  • 2.0,2.1,
  • 3.0,3.1,
  • 4.0,4.1
  • for(i0 ilt5 i)
  • for(j0 jlt2 j)
  • printf("fdd is .1f",i, j,
    fij)
  • printf(" kdd is 02d\n",j, i,
    kji)

8
Initialization
  • include ltstdio.hgt
  • void main()
  • int i, j, k2500,01,02,03,04,
  • 10,11,12,13,14
  • float f52 0.0, 0.1,
  • 1.0,1.1,
  • 2.0,2.1,
  • 3.0,3.1,
  • 4.0,4.1

9
Output
  • for(i0 ilt5 i)
  • for(j0 jlt2 j)
  • printf("fdd is .1f",i, j,
  • fij)
  • printf(" kdd is 02d\n",j, i,
  • kji)

10
Default Initialization
  • include ltstdio.hgt
  • void main()
  • int i, j, k2599
  • for(i0 ilt5 i)
  • for(j0 jlt2 j)
  • printf(" kdd is 02d\n",j,
  • i, kji)

11
Surface
  • void main()
  • float z2121
  • int x, y
  • for(x0 xlt21 x)
  • for(y0 ylt21 y)
  • zxy((x-15)(x15)(y-5)(y-5))

12

13
3D Array
  • include ltstdio.hgt
  • void main()
  • int m432000,001,010,011,020,021
    ,
  • 100,101,110,111,120,121
    ,
  • 200,201,210,211,220,221
    ,
  • 300,301,310,311,320,321
    , i, j, k
  • for(i0 ilt4 i)
  • for(j0 jlt3 j)
  • for(k0 klt2 k)
  • printf("d d d value is
    d\n",i,j,k,mijk)

14
Initialization
  • include ltstdio.hgt
  • void main()
  • int m432000,001,010,011,020,021,
  • 100,101,110,111,120,121,
  • 200,201,210,211,220,221,
  • 300,301,310,311,320,321,
  • i, j, k

15
Output
  • for(i0 ilt4 i)
  • for(j0 jlt3 j)
  • for(k0 klt2 k)
  • printf("d d d value is
  • d\n",i,j,k,mijk)

16
Out of Bounds
  • What happens if
  • int a10
  • a1123
  • We can get an error that is not easily detected.
  • Some systems will check this, Borland on Windows
    does not.

17
Array Passing to Functions
  • include ltstdio.hgt
  • define SIZE 4
  • void PrintArray(int ) //Prototype
  • void main()
  • int aSIZE0,1,2,3
  • PrintArray(a) //Function call
  • void PrintArray(int x) //Declaration
  • int i
  • for(i0 iltSIZE i)
  • printf("d d\n",i,xi)

18
main and Prototype
  • include ltstdio.hgt
  • define SIZE 4
  • void PrintArray(int ) //Prototype
  • void main()
  • int aSIZE0,1,2,3
  • PrintArray(a) //Function call

19
Function
  • void PrintArray(int x) //Declaration
  • int i
  • for(i0 iltSIZE i)
  • printf("d d\n",i,xi)

20
Variable Number of Elements
  • include ltstdio.hgt
  • define SIZE 4
  • void PrintArray(int , int)
  • void main()
  • int aSIZE0,1,2,3
  • PrintArray(a, 3)
  • void PrintArray(int x, int n)
  • int i
  • for(i0 iltn i)
  • printf("d d\n",i,xi)

21
main and Prototype
  • include ltstdio.hgt
  • define SIZE 4
  • void PrintArray(int , int)
  • void main()
  • int aSIZE0,1,2,3
  • PrintArray(a, 3)

22
Function
  • void PrintArray(int x, int n)
  • int i
  • for(i0 iltn i)
  • printf("d d\n",i,xi)

23
Passing Multidimensions
  • include ltstdio.hgt
  • void PrintArray(int 3) //Prototype
  • void main()
  • int a230,1,2,3,4,5
  • PrintArray(a) //Function call
  • void PrintArray(int x3) //Declaration
  • int i,j
  • for(i0 ilt2 i)
  • for(j0 jlt3 j)
  • printf("d d d\n",i,j,xij)

24
main and Prototype
  • include ltstdio.hgt
  • void PrintArray(int 3) //Prototype
  • void main()
  • int a230,1,2,3,4,5
  • PrintArray(a) //Function call

25
Function
  • void PrintArray(int x3) //Declaration
  • int i,j
  • for(i0 ilt2 i)
  • for(j0 jlt3 j)
  • printf("d d d\n",i,j,xij)

26
Notes
  • In one dimensional arrays, we do not have to be
    explicit about the number of elements
  • void PrintArray(int )
  • In multi-dimensional arrays, we do have to be
    explicit about the number of elements in every
    level except the first
  • void PrintArray(int 3)
  • These define the shape.
Write a Comment
User Comments (0)
About PowerShow.com