Arrays Introduction - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Arrays Introduction

Description:

Can use a single name to refer to all these similar numbers ... To reference, need to specify the array name and the element desired. Array Illustration ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 16
Provided by: beth74
Category:

less

Transcript and Presenter's Notes

Title: Arrays Introduction


1
Arrays - Introduction
  • Data structure of a grouping of related data
    items in memory
  • Used to store a collection of data items that are
    all the same type
  • Exam scores
  • Population
  • Temperature
  • Can use a single name to refer to all these
    similar numbers
  • Collection of 2 or more adjacent memory cells,
    called elements, that are associated with a
    particular symbolic name
  • To reference, need to specify the array name and
    the element desired

2
Array Illustration
3
Declaring arrays
  • Examples
  • real frequency(12), tempC(366)
  • integer daysInMonth(12)
  • Can use a variable for the array size
  • parameter(ipollenCnt31)
  • real pollenSize(ipollenCnt)
  • Array indexes MUST be integers
  • Index (or element) size can be a range
  • real annTemp(19482002), tempFreq(-20140)

4
Tips on Arrays
  • Declare the size of array to allow for maximum
    expected amount. You do not have to fill every
    element
  • Parallel arrays
  • integer totAnnFires(19502000),
    AnnFireCnt(1950200)

5
Initializing an Array
  • Always initialize array before trying to access
    particular elements
  • Do loops
  • Assignment
  • Input
  • DATA statement

6
Initializing an Array with a DO loop and
Assignements
  • do 100 icnt 1950, 2000
  • annFireCnt(icnt) 0
  • 100 end do
  • do 200 imonth 1, 12
  • monTempC(imonth) -99.0
  • 200 end do

7
Initializing with an Implied DO Loop from Input
  • read , (temp(mon),mon1,12)
  • read , (tempFreq(tbins), tbins -20, 140)
  • read , (lon112Temp(ilats),ilat90,-90,-1)
  • Note the parenthesis surrounding expressions and
    commas separating array from array range

8
DATA statement
  • Used to assign data to an array
  • integer daysInMonth(12), annFireCnt(12)
  • parameter(dummy-9999.0)
  • data daysInMonth /31,28,31,30,31,30,31,31,30,31,30
    ,31/
  • data annFireCnt /3,12,24,49,112,152,196,82,10,8,2,
    5/
  • data annFireCnt /12dummy/
  • Note
  • slash marks
  • no spaces between variable name and list
  • and list is separated by commas

9
Passing arrays between subprograms
  • If passing entire array, dont need to specify
    size
  • real tempC(365)
  • minNum min(tempC)
  • rangeNum rangeFunc(tempC)
  • call C2KandF(tempC, tempF, tempK)
  • function rangeFunc(tempC)
  • subroutine C2KandF(tempC, tempF, tempK)

10
More passing of Arrays
  • Can pass a single element
  • utmX(I) dec2utm(latdec(I))
  • function dec2utm(degree)
  • Can pass a range of elements
  • real temp(365)
  • runAve5 ave5func(temp(II4)
  • function ave5func(num5)
  • real num5(5)

11
We Try It!
  • Find the cube and square of numbers 1 through 10
    and store into separate arrays

12
We Try It!
  • Read 10 numbers into an array and find the
    largest and smallest numbers. Compute the sum of
    the numbers

13
We try it!
  • Convert your program that finds the Julian day
    from the month, day and year by replacing the Ifs
    with an array of how many days are in each month

14
You Try It!
  • Write a program that computes the average and
    standard deviation of an inputted list of unknown
    size (max of 100). The user will say how many
    items are in the list and the user will then
    enter the values. Modularize your program and
    use ample comments.
  • Standard deviation sqrt(sum (difference between
    value and average squared) divided by the total
    number of values minus 1)
  • Hints
  • Initialize list to something you know the user
    will likely not ever enter

15
You Try It
  • Write a program that sorts of list of numbers
    (size determined by user). Use the bubble sort
    method. Compute the median.
Write a Comment
User Comments (0)
About PowerShow.com