Arrays - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Arrays

Description:

Arrays save time in that you don't have to manually declare a 100 floating point variables. ... Forgetting to declare the array (either altogether or forgetting the ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 23
Provided by: Satya6
Category:
Tags: arrays | declare | handin

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
2
Arrays
  • So far the variables we have made store one value
    at a time. If you need to store and use 10
    integers, then 10 int variables must be declared.
  • These variables are called atomic or scalar,
    which means they cannot be further divided into
    smaller pieces.

3
Arrays
  • Often it is the case that we have a group of
    values of the same data type that form a logical
    group, for example a list of grades for a class.
  • Such a list is called a 1-dimensional array.

4
One-Dimensional Arrays
  • To declare an array, we use the following syntax
  • data-type array-namenumber-of-items
  • For example
  • char grades50
  • This creates an array of 50 characters that could
    be used to hold letter grades.

5
Other array declarations
  • double amount38 //holds 38 values of type
    double.
  • int volts6 //holds 6 integer values.
  • const int BUFFER_SIZE 100
  • float bufferBUFFER_SIZE //100 floats.

6
Whats the Point?
  • Arrays save time in that you dont have to
    manually declare a 100 floating point variables.
  • Arrays allow for easy updates of a given value.
    To do this we make use of the array index. Each
    element in an array is accessed by supplying the
    appropriate index value. We see this in the next
    slide.

7
Array Indices
  • The grades array from our first example holds 50
    characters.
  • grades0 refers to the first element in the
    array.
  • grades1 is the second element
  • grades2 is the third element
  • grades49 is the fiftieth element

8
Using Array Variables
  • Just like atomic variables
  • grades0 a / assigns a to the first
    element in grades /
  • grades1 grades0
  • grades2 grades1 3 //assigns d to
    grades2

9
More array indices
  • The array index can be specified using variables
    or expressions, but you must be careful of two
    things
  • The variable or expression evaluates to an
    integer
  • The value is in the range of the array. In the
    grades example, the index range is something
    between 0 and 49.

10
Using a loop with arrays
  • Arrays and for loops go hand-in-hand.
  • Consider the following code
  • int volts5
  • //Note i 0 and i lt 5
  • for (int i 0 i lt 5 i)
  • voltsi 70
  • //What does this do?

11
Example Program
  • / ask user for 5 temperature values, and then
    output their sum /
  • include ltiostreamgt
  • using namespace std
  • int main()
  • const int MAXTEMPS 5
  • int i, tempMAXTEMPS, total 0
  • for (i 0 i lt MAXTEMPS i)
  • cout ltlt Enter a temperature
  • cin gtgt tempi
  • (cont)

12
Example Program (cont)
  • cout ltlt \nThe total of the temperatures
  • for (i 0 i lt MAXTEMPS i)
  • cout ltlt ltlt tempi
  • total total tempi
  • cout ltlt is ltlt total ltlt endl
  • return 0

13
Array Initialization
  • Arrays are initialized like scalar variables,
    except the list of values are placed in braces.
    For example
  • int volts5 2, 9, 100, 34, 27
  • char grades3 a, c, b
  • Initialization of large arrays can be split into
    rows
  • double states10 5.4, 3.8, 3.4, 10.6, 9.2,
  • 1.8, 10.3, 14.0, 2.3, 1.4

14
Array Initialization (cont)
  • You do not have to initialize every element of an
    array
  • / initializes hits0, hits1, and hits2 to
    the values indicated all the rest are set to 0
    /
  • int hits6 133, 25, 10001
  • The size of the array may be omitted if you
    initialized all of its values at declaration
  • / grades has three elements /
  • int grades 79, 90, 81
  • / same as above /
  • int grades3 79, 90, 81

15
Two-dimensional Arrays
  • A two-dimensional array consists of both rows and
    columns of elements. It is essentially a matrix.
  • To declare a two-dimensional array, we merely use
    two sets of square brackets.
  • The first contains the number of rows
  • The second contains the number of columns
  • //Creates a 2D array with 3 rows and 4 columns
  • int vals34

16
Indices in 2D arrays
  • Assume that the two dimensional array called val
    is declared and looks like the following
  • To access the cell containing 6, we reference
    val13, that is, row 1, column 3.

17
Using 2D arrays
  • Just like 1D arrays, once you have specified the
    index, you are just working with a single
    variable of the given data type.
  • Assignments and usage is still the same
  • sumRow0 val00 val01 val02
  • val03
  • //assigns 72 to cell at row 2, column 3
  • val23 72

18
Initializing 2D arrays
  • You can use additional braces to indicate when
    rows start and end, but you dont have to do
    that.
  • int val34 8,16,9,52,
  • 3,15,27,6,
  • 14,25,2,10
  • Or
  • int val34 8,16,9,52,
  • 3,15,27,6,
  • 14,25,2,10
  • Or (correct, but not as clear as the first two)
  • int val34 8,16,9,52,3,15,27,6,14,25,2,10

19
More on 2D arrays
  • Initialization of 2D arrays is done in row order.
  • 2D arrays work well with (for) loops like 1D
    arrays. However, to access all elements,
    typically you will need nested loops for 2D
    arrays. Can you see why?

20
Example Program
  • include ltiostreamgt
  • include ltiomanipgt
  • using namespace std
  • int main()
  • int vals34 8,16,9,52, 3,15,27,6,
    14,25,2,10
  • for (int i 0 i lt 3 i)
  • cout ltlt endl
  • for (int j 0 j lt 4 j)
  • cout ltlt valij ltlt
  • return 0

21
Multi-Dimensional Arrays
  • Arrays can have higher dimensions.
  • Everything works about the same as indicated
    here.
  • Note multidimensional arrays can have quite a few
    elements so you must be mindful of your memory
    capacity
  • int big100010001000 //a billion ints

22
Common Programming Errors
  • Addressing indices that are out of bounds of the
    array range. This will run-time crash or at the
    very least a logical error. Be careful especially
    when using expressions to compute the indices.
  • Remember, indexing starts with 0, not 1!!!
  • Forgetting to declare the array (either
    altogether or forgetting the )
  • Forgetting to initialize an array. Some compilers
    set everything to zero, some do not.
Write a Comment
User Comments (0)
About PowerShow.com