One Dimensional Arrays - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

One Dimensional Arrays

Description:

3) Show code to print all the numbers in the array. for (int i=0; i SIZE; i ) ... bool flags[8]; char grades[100] int scores[10] Type of cells # of memory ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 20
Provided by: brenda54
Category:
Tags: arrays | dimensional | flags | one | print | to

less

Transcript and Presenter's Notes

Title: One Dimensional Arrays


1
Chapter 12
  • One Dimensional Arrays

2
C Data Types

3
Structured Data Types
  • Struct
  • Class
  • Union
  • array

4
Structured Data Typearray
  • Array linearly-ordered collection of
    homogeneous data elements (collection of elements
    of the same type)
  • Linearly-ordered there is a first, second,
    third, etc. element

5
Array Properties
  • All elements in the array are of the same type.
  • Elements are accessed by their index, starting at
    0
  • Size of the array is fixed (cannot be increased
    or decreased once an array has been created)

6
Why use arrays?
  • Gives use the ability to store a large number of
    elements using one variable name.
  • Examples
  • Suppose we want to store 3 test grades int t1,
    t2, t3
  • How could we store 100 test grades?

7
Using C array
  • Declare the array
  • Initialize the array
  • Access the array
  • Passing an array as an argument

8
How to declare an array
  • Examples
  • int numbers10//stores 10 integers
  • float averages30 //stores 30 floats
  • --------------------------------------------
  • //Better to use const
  • const int CLASS_SIZE 25
  • string namesCLASS_SIZE

9
You may want to initialize an array
  • You can use a loop
  • int numbers10
  • for (int i0 ilt10 i)
  • numbersi0
  • 2) string names5 Sam, Joe, Carol,
    Bill, Jane

10
How to Access array elements
  • Must use an index (subscript)
  • Index starts with 0
  • Examples
  • cout ltlt numbers4 ltlt endl
  • //prints the 5th element in the array

11
How to access array elements (continued)
  • int grades10
  • cout ltltEnter 10 test grades\n
  • for (int i0 ilt10 i)
  • cin gtgtgradesi

12
Array Assignments
  • Examples
  • grades5 89
  • grades5 grades4
  • --------------------------------------------
  • int a3
  • int b3
  • ab //illegal

13
Questions!
  • Assume that we have an array of integers called
    numbers of size, SIZE.
  • Show code to declare the array
  • int numbersSIZE
  • Show code to initialize the array to 0s.
  • for (int i0 iltSIZE i)
  • numbersi0

14
QUESTIONS
  • 3) Show code to print all the numbers in the
    array
  • for (int i0 iltSIZE i)
  • cout ltltnumbersi ltlt endl
  • 4) Show code to print the 5th number in the array
  • cout ltltnumber4 ltlt endl

15
QUESTIONS
  • How many memory cells are reserved for data and
    what type of data can be stored there?

16
BE CAREFULL!
  • C does not check for out-of-bounds array
    indexes. For example, suppose
  • float alpha100
  • The valid range of alpha is 0 through 99.
  • Assume
  • alphai62.4
  • This is out-of-bounds if i is less than 0 or i is
    greater than 99!

17
Passing an array as a parameter
  • Assume
  • const int TOTAL 10
  • float finalGrade
  • int gradesTOTAL
  • Function prototype
  • float average(int , int)
  • Function call
  • finalGrade average(grades, TOTAL)
  • Function heading
  • float average (int numbers, int size)

18
Array Notes
  • Arrays are always passed by reference!
  • The address of the beginning of the array is
    passed as an argument to the function

19
HomeworkQuiz Grade
  • Write a function called Smallest() which
    receives as arguments an integer array called A
    and an integer called Count where A has been
    given values and Count is the total number of
    integers found in the array. The function should
    return the smallest integer found in the array.
Write a Comment
User Comments (0)
About PowerShow.com