One Dimensional Arrays - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

One Dimensional Arrays

Description:

Read (then re-read) from a file. The real answer ... Possible Solutions. Use arrays! ... Most such tasks (assignment, read, write) can be performed some other way ... – PowerPoint PPT presentation

Number of Views:358
Avg rating:3.0/5.0
Slides: 26
Provided by: SArms7
Category:

less

Transcript and Presenter's Notes

Title: One Dimensional Arrays


1
One Dimensional Arrays
  • Chapter 11

2
"All students to receive arrays!" reports Dr.
Austin.
scores 85 79 92 57 68 80 . . .
0 1 2 3 4 5 98 99
3
Design Problem
  • Consider a program to calculate class average

?
4
Add to Design Problem
  • Now your client says, I need to ALSO calculate
    and display deviations from the average

Describe why this will or will NOT work
5
Possible Solutions
?
  • Enter in the scores again
  • Use 100 separate variables
  • and cout and cin commands
  • Read (then re-read) from a file
  • The real answer

?
?
Use arrays!!
6
Simple vs Structured Data Types
  • Simple data type gt data element contains a
    single value
  • Structured data type gt a data element contains a
    collection of data values

7
Arrays
  • Arrays are Structured Data Types
  • They have a means of accessing individual
    components
  • Values can be retrieved from and stored in the
    structure

scores 85 79 92 57 68 80
0 1 2 3 4 5
8
One Dimensional Array
  • Structured collection of components
  • All of the same type
  • Structure given a single name
  • Individual elements accessed by index indicating
    relative position in collection
  • Type of elements stored in an array can be just
    about anything
  • Index of an array must be an integer

9
Use of Array for Our Problem
  • Store elements in array as read in
  • Go back and access for deviations

10
Declaring Arrays
  • Syntax Data_type Array_name constant
  • Note declaration from our example

11
Declaring Arrays
  • Example specifies an array
  • each element is an integer
  • there is space for 100 elements
  • the are numbered 0 through 99

scores 85 79 92 57 68 80 . . .
0 1 2 3 4 5 98 99
12
Accessing Individual Components
  • Use the name of the array
  • Followed by an integer expression inside the
    square brackets

scores 85 79 92 57 68 80 . . .
0 1 2 3 4 5 98 99
Index can be- constant- variable-
expressionMUST be an integer
max scores0for (x 0 x lt 100 x) if
(scoresx gt max) max scoresx
13
Out of Bounds Index
  • What happens if
  • C does NOT check for index out of range
  • Possible to walk off into far reaches of memory
    -- clobbers ...
  • other variable locations
  • .exe code
  • the operating system (??)

float f_list 50 f_list 100 123.456
14
Initializing Arrays in Declarations
  • Possible to declare the size initialize
  • Possible to omit size at declaration
  • Compiler figures out size of array

int results 5 14, 6, 23, 8, 12
float prices 2.41, 85.06, 19.95, 3.91
15
Aggregate Operations
  • Defn gt an operation on the data structure as a
    whole
  • as opposed to operation on a SINGLE element
    within the structure
  • Example
  • would be nice to read in a WHOLE array

16
Lack of Aggregate Operations
  • Would be nice but . . . C does NOT have . .
    .
  • Assignment operator for whole array
  • Arithmetic operations for whole array (think
    matrix)
  • Comparisons for arrays (not even )
  • Return of an array type by a function

17
How to Accomplish Aggregate Operations?
  • Most such tasks (assignment, read, write) can be
    performed some other way
  • CS II course will write classes to provide
    these functions
  • Otherwise
  • these operations must be performed by the
    programmer
  • element by element in a loop

18
Arrays as Parameters
  • This is one task that CAN be done to the WHOLE
    array
  • C always passes arrays by reference

19
Arrays as Parameters
  • The name of the array is a pointer constant
  • The address of the array is passed to the
    function
  • Size of thearray alsopassed tocontrol loop

20
Arrays as Parameters
  • Note the empty brackets in parameter list
  • A number can be placed here but it will beignored

21
Sub-array Processing
  • Note we specified an array size of 100
  • but we dont anticipate that many scores
  • Array always declared larger than needed
  • Must keep track of how many have been used
  • this is our limit when doing other things to the
    array

22
Design Problem
  • Consider the task of keeping track of data about
    parts for manufacture
  • part number, description, qty needed, unit price

23
Design Problem
  • Use Parallel arrays
  • One array each for part num, descrip, qty, price
  • nth item in any one of the arrays associated with
    same nth item of all the arrays

24
Testing and Debugging Hints
  • Range of legal index values is 0 to array_size -
    1
  • Individual elements of the array are of the
    component type
  • No aggregate operations in arrays
  • you must write the code to do this
  • If array parameter is incoming, specify formal
    parameter as const
  • prevents function from modifying

25
Testing and Debugging Hints
  • Omitting array size in declaration
  • when array declared formal parameter
  • when array initialized at declaration
  • Dont pass component when function expects entire
    array
  • Declare array size as max ever needed
  • process only part of array which is used
  • Pass array name and length to functions which
    process array or sub array
Write a Comment
User Comments (0)
About PowerShow.com