Arrays - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Arrays

Description:

A array of five elements (type double) can be declared as: double s[5] ... There are five categories of hurricanes. Category 1 74 wind speed 96 mph ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 37
Provided by: adria9
Category:
Tags: arrays | elements | five

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
2
Arrays
  • Suppose we have a set of 100 temperature
    measurements that we want to analyze. It would be
    tedious to write a program that has 100 different
    variables corresponding to each temperature
    measurement!
  • double temperature1, temperature2, temperature3

3
Arrays
  • Instead, we use a method that enables us to work
    with a group of values with a single identifier
    that is arrays.

4
Arrays
  • A array of five elements (type double) can be
    declared as
  • double s5
  • This is a one dimensional array, which you can
    visualize as a vector (row of values).
  • s0 0.5
  • s1 0.3
  • s2 -1.1
  • s3 4.2
  • s4 9.4

The array index is the offset from the first
element in the array
s0
s1
s2
s3
s4
s
Each element is a type double
s holds the address of the first element in the
array
5
Initialization
  • Initialization uses braces. The following two
    array declarations and initializations are
    equivalent.
  • double s5 0.5, 0.0, -0.1, 0.2, 0.15
  • double s 0.5, 0.0, -0.1, 0.2, 0.15
  • The following two array declarations and
    initializations are equivalent.
  • double s5 0.0, 0.0, 0.0, 0.0, 0.0
  • double s5 0.0

6
Initialization and Display
  • Write a program that first creates an array that
    has the values between 0 to 10 in steps of 0.5,
    and second displays the contents of the array,
    along with its offset location.

7
  • include ltiostreamgt
  • include ltiomanipgt
  • using namespace std
  • int main()
  • double x21
  • int i // this variable is used as the
    current offset
  • // initialize the array with the values
  • for (i 0 i lt 21 i)
  • xi i0.5
  • cout ltlt setw(10) ltlt "Offset"
  • ltlt setw(10) ltlt "Value" ltlt endl
  • cout ltlt setw(10) ltlt "------"
  • ltlt setw(10) ltlt "-----" ltlt endl

8
Reverse Array
  • Write a program that first creates an array that
    has the values between 0 to 10 in steps of 0.5,
    and second displays the contents of the array,
    along with its offset location, in reverse order.

9
  • include ltiostreamgt
  • include ltiomanipgt
  • using namespace std
  • int main()
  • double x21
  • int i // this variable is used as the
    current offset
  • // initialize the array with the values
  • for (i 0 i lt 21 i)
  • xi i0.5
  • cout ltlt setw(10) ltlt "Offset"
  • ltlt setw(10) ltlt "Value" ltlt endl
  • cout ltlt setw(10) ltlt "------"
  • ltlt setw(10) ltlt "-----" ltlt endl

10
Display array
  • include ltiostreamgt
  • using namespace std
  • const int N 5
  • int main()
  • int xN 1,2,3,4,5
  • int i // this variable is used as the
    current offset
  • for (i 0 i lt N i)
  • cout ltlt xi ltlt " "
  • cout ltlt endl
  • system("pause")
  • return 0

11
Falling off the end of the array
  • include ltiostreamgt
  • using namespace std
  • const int N 5
  • int main()
  • int xN 1,2,3,4,5
  • int i // this variable is used as the
    current offset
  • for (i 0 i lt N i)
  • cout ltlt xi ltlt " "
  • cout ltlt endl
  • system("pause")
  • return 0

This should be i lt N. This means the loop will
execute for i 5. What will happen when it tries
to display x5?
What would happen instead of displaying x5, we
assigned a value to x5! What would happen then?
12
Static versus Dynamic arrays
  • In this course, we will only be considering
    static arrays that is, the size of the array
    will be constant.
  • There are dynamic arrays, where the size changes
    when we allocate/deallocate memory to the array.
    We will not be considering dynamic arrays in this
    course.

13
Partially filled array
  • Write a program that accepts user inputs of
    integers, which are placed in an array. The
    program continues to accept user inputs until 10
    inputs are received, or until the user enters -1.
    When the program is finished receiving inputs,
    the program should then display the array
    contents.

14
  • include ltiostreamgt
  • using namespace std
  • const int NMAX 10
  • int main()
  • int xNMAX
  • int n 0, i
  • while (n lt NMAX)
  • cout ltlt "Enter an input (-1 to quit) "
  • cin gtgt xn
  • if (xn -1)
  • break

15
Functions and Arrays
  • double s5
  • Recall that s0 is the value of the first
    element in the array and the s is the memory
    address of the array.
  • Arrays used in function are inherently pass by
    reference because we are passing the address of
    the array into a function.

16
Reverse and Display
  • Write a function that displays the contents of an
    array.
  • Write a function that reverses the contents of an
    array.
  • Write a program that has an array of elements 1
    to 20, displays the array, reverses the array,
    and displays the array again.

17
  • include ltiostreamgt
  • using namespace std
  • const int N 19
  • void display_array(int y, int n)
  • void reverse_array(int y, int n)
  • int main()
  • int xN, i
  • for (i 0 i lt N i)
  • xi i 1
  • display_array(x, N)
  • reverse_array(x, N)

18
  • void display_array(int y, int n)
  • int i
  • for (i 0 i lt n i)
  • cout ltlt yi ltlt " "
  • cout ltlt endl
  • return
  • void reverse_array(int y, int n)
  • int tempN, i
  • for (i 0 i lt n i)
  • tempi yi

19
Functions and Arrays
  • In the many cases, functions that include arrays
    will often have two input parameters the array
    and the number of valid inputs.

20
Max value in array
  • Write a function that accepts user inputs of
    integers, which are placed in an array. The
    function continues to accept user inputs until
    100 inputs are received, or until the user enters
    -1. The function should return the number of user
    inputs.
  • When a function that returns the maximum value in
    an array.
  • Write a program that accepts user inputs until a
    -1 is entered or until 100 inputs are received.
    The program should then display the user inputs
    and the maximum value.

21
  • include ltiostreamgt
  • using namespace std
  • const int NMAX 100
  • void display_array(int x, int n)
  • int fill_array(int x, int nmax)
  • int max_in_array(int x, int n)
  • int main()
  • int xNMAX, n
  • n fill_array(x, nmax)
  • display_array(x,n)
  • cout ltlt "The maximum value is "
  • ltlt max_in_array(x,n) ltlt endl

22
  • void display_array(int x, int n)
  • int i
  • for (i 0 i lt n i)
  • cout ltlt xi ltlt " "
  • cout ltlt endl
  • return
  • int fill_array(int x, int nmax)
  • int i 0
  • while (i lt nmax)
  • cout ltlt "Enter an integer -1 to stop) "
  • cin gtgt xi

23
  • int max_in_array(int x, int n)
  • int i, maxval x0
  • for (i 1 i lt n i)
  • if (xi gt maxval)
  • maxval xi
  • return maxval

24
Hurricane
  • There are five categories of hurricanes
  • Category 1 74 ? wind speed lt 96 mph
  • Category 2 96 ? wind speed lt 111 mph
  • Category 3 111 ? wind speed lt 131 mph
  • Category 4 131 ? wind speed lt 155 mph
  • Category 5 155 mph ? wind speed
  • Suppose we have a data file storm.txt that
    contains a numerical identification for each
    storm within a year and the peak wind speed.
    Write a program that outputs a table that
    contains the identification number, peak wind,
    and category of each hurricane. Storms that are
    not a hurricane are simply ignored. Note that
    there are at most 100 hurricanes a year.

25
  • include ltiostreamgt
  • include ltfstreamgt
  • include ltiomanipgt
  • using namespace std
  • const int NMAX 100
  • int category(double wind_speed)
  • int main()
  • int idNMAX, storm_categoryNMAX, n 0, i
  • double peak_windNMAX
  • ifstream fin
  • fin.open("storm.txt")
  • while (!fin.eof() n lt NMAX)
  • fin gtgt idn gtgt peak_windn

26
  • cout ltlt setw(20) ltlt "Identification"
  • ltlt setw(20) ltlt "Peak Wind"
  • ltlt setw(20) ltlt "Category" ltlt endl
  • cout ltlt setw(20) ltlt "--------------"
  • ltlt setw(20) ltlt "---------"
  • ltlt setw(20) ltlt "--------" ltlt endl
  • for (i 0 i lt n i)
  • cout ltlt setw(20) ltlt idi
  • ltlt setw(20) ltlt peak_windi
  • ltlt setw(20) ltlt storm_categoryi ltlt
    endl
  • cout ltlt endl ltlt "There were " ltlt n
  • ltlt " storms that were hurricanes" ltlt
    endl
  • system("pause")
  • return 0

27
  • /------------------------------------------------
    -------------------------
  • int category(double wind_speed)
  • Author Adrian Chan
  • Student ID 100123456
  • This function determines the hurriance category
    of a storm.
  • Category 1 74 lt wind speed lt 96 mph
  • Category 2 96 lt wind speed lt 111 mph
  • Category 3 111 lt wind speed lt 131 mph
  • Category 4 131 lt wind speed lt 155 mph
  • Category 5 155 mph lt wind speed
  • Inputs
  • wind_speed the peak wind speed of the storm
  • Outputs
  • double category number

28
  • int category(double wind_speed)
  • if (wind_speed gt 155.0)
  • return 5
  • else if (wind_speed gt 131.0)
  • return 4
  • else if (wind_speed gt 111.0)
  • return 3
  • else if (wind_speed gt 96.0)
  • return 2
  • else if (wind_speed gt 74.0)
  • return 1
  • return 0

29
Hurricane
  • Did you need to use an array for this
    question?!?!?!?!

30
  • include ltiostreamgt
  • include ltfstreamgt
  • include ltiomanipgt
  • using namespace std
  • int category(double wind_speed)
  • int main()
  • int id, storm_category, n 0, i
  • double peak_wind
  • ifstream fin
  • cout ltlt setw(20) ltlt "Identification"
  • ltlt setw(20) ltlt "Peak Wind"
  • ltlt setw(20) ltlt "Category" ltlt endl
  • cout ltlt setw(20) ltlt "--------------"
  • ltlt setw(20) ltlt "---------"

31
  • /------------------------------------------------
    -------------------------
  • int category(double wind_speed)
  • Author Adrian Chan
  • Student ID 100123456
  • This function determines the hurriance category
    of a storm.
  • Category 1 74 lt wind speed lt 96 mph
  • Category 2 96 lt wind speed lt 111 mph
  • Category 3 111 lt wind speed lt 131 mph
  • Category 4 131 lt wind speed lt 155 mph
  • Category 5 155 mph lt wind speed
  • Inputs
  • wind_speed the peak wind speed of the storm
  • Outputs
  • double category number

32
Hurricane
  • It would be better to use arrays if
  • You needed to display the number of storms that
    were hurricanes first
  • You were going to put an asterisk next to the row
    that had the highest wind speed

33
  • include ltiostreamgt
  • include ltfstreamgt
  • include ltiomanipgt
  • using namespace std
  • const int NMAX 100
  • int category(double wind_speed)
  • int main()
  • int idNMAX, storm_categoryNMAX, n 0, i,
    index_max 0
  • double peak_windNMAX
  • ifstream fin
  • fin.open("storm.txt")
  • while (!fin.eof() n lt NMAX)
  • fin gtgt idn gtgt peak_windn

This is not the maximum peak wind but rather than
offset index where the maximum peak wind is
located in the array. Initialize it to the first
element in the array
See how the index_max is updated
34
  • cout ltlt endl ltlt "There were " ltlt n
  • ltlt " storms that were hurricanes" ltlt
    endl
  • cout ltlt setw(20) ltlt "Identification"
  • ltlt setw(20) ltlt "Peak Wind"
  • ltlt setw(20) ltlt "Category" ltlt endl
  • cout ltlt setw(20) ltlt "--------------"
  • ltlt setw(20) ltlt "---------"
  • ltlt setw(20) ltlt "--------" ltlt endl
  • for (i 0 i lt n i)
  • cout ltlt setw(20) ltlt idi
  • ltlt setw(20) ltlt peak_windi
  • ltlt setw(20) ltlt storm_categoryi
  • if (i index_max)
  • cout ltlt "" ltlt endl
  • else
  • cout ltlt endl

Output an when we encounter the index_max
locations
35
  • /------------------------------------------------
    -------------------------
  • int category(double wind_speed)
  • Author Adrian Chan
  • Student ID 100123456
  • This function determines the hurriance category
    of a storm.
  • Category 1 74 lt wind speed lt 96 mph
  • Category 2 96 lt wind speed lt 111 mph
  • Category 3 111 lt wind speed lt 131 mph
  • Category 4 131 lt wind speed lt 155 mph
  • Category 5 155 mph lt wind speed
  • Inputs
  • wind_speed the peak wind speed of the storm
  • Outputs
  • double category number

36
  • int category(double wind_speed)
  • if (wind_speed gt 155.0)
  • return 5
  • else if (wind_speed gt 131.0)
  • return 4
  • else if (wind_speed gt 111.0)
  • return 3
  • else if (wind_speed gt 96.0)
  • return 2
  • else if (wind_speed gt 74.0)
  • return 1
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com