• Arrays

    What is an array?:

    • collection of data items with that represent the same thing (e.g. test scores of a class, data measurements from an experiment, balances of bank accounts, etc.)
    • it could be a collection of int's, float's, char's, etc.
    • you can think of arrays like PO Boxes

    Why are arrays important?:

    • arrays allow you to group data items that are related
    • arrays allow you to add new data items to the group easily
    • using arrays allow your code to be flexible when treating variables that represent similar things

    How to picture an array?:

    • Honolulu Post Office honolulu_po_box

      PO Box 1 at Honolulu Post Office
      honolulu_po_box[0]
      PO Box 2 at Honolulu Post Office
      honolulu_po_box[1]
      PO Box 3 at Honolulu Post Office
      honolulu_po_box[2]
      Array Analogy
      Real WorldProgramming World
      Honolulu Post Office honolulu_po_box
      PO Box 1 at Honolulu Post Office honolulu_po_box[0]
      PO Box 2 at Honolulu Post Office honolulu_po_box[1]
      PO Box 3 at Honolulu Post Office honolulu_po_box[2]
      Array Statement Analogy
      Real WorldProgramming World
      My package is somewhere inside the Honolulu Post Office My data is stored in the honolulu_po_box array
      My parckage is in PO Box 1 at Honolulu Post Office My data is stored in the first array element of the honolulu_po_box array, honolulu_po_box[0]
      My parckage is in PO Box 2 at Honolulu Post Office My data is stored in the second array element of the honolulu_po_box array, honolulu_po_box[1]
      My parckage is in PO Box 3 at Honolulu Post Office My data is stored in the third array element of the honolulu_po_box array, honolulu_po_box[2]
      All the po boxes at the Honolulu Post Office can only hold 4 inch x 4 inch x 4 inch boxes The honolulu_po_box array can only hold int's, int honolulu_po_box[3]
      The Honolulu Post Office has 3 PO Boxes available The honolulu_po_box array has 3 array elements, int honolulu_po_box[3]
  • Implementing Arrays

    How to use an array?:

    • same as variables: declare it, initialize it, and use (set, change, or display) it
    • special rules for arrays:
      • all data items must be of the same type (e.g. all int's, all char's)
      • individual data items in the array are accessed by a subscript or index (e.g. list_of_values[0], list_of_values[5]
      • array subscript starts at 0 (e.g. to get to the first data item of an array, you need to use list_of_values[0], instead of list_of_values[1]
      • the size of the array need to be specified when declaring the array int list_of_values[3], there are exceptions to this rule

    A simple example using arrays:

    simple_arrays.c:

    Be aware of the null character \0 when working with strings

    Let's compare how you would do things with and without arrays

    • with arrays:

      with_array.c:

    • without arrays:

      without_array.c:

    • The power of arrays shines through when it is time to update the code to make the program handle a longer list.
  • Arrays are Handled Almost like Pointers

  • Write Your First Arrays

      Write a program that:
    • initializes a 3 element array of floats
      • set the elements to 1.1, 2.2, and 3.3
      • print out the 3 elements of the float array
    • initializes a 3 element array of chars
      • set the elements to a, b, and c
      • print out the 3 elements of the char array
  • Assignment

    Homework Problem 19.1, at the end of this problem you should submit to Laulima:

    • a file named UHusername_19_1.c
      • example:
      • zhaol_19_1.c
    • write a program that:

      • reads in 10 decimals into an array using a while loop
      • prints out the 10 decimal numbers from the array using a separate while loop
    • Test Case 1:
      Please enter your number:
      [user enters 3]
      [user enters 4]
      [user enters 5]
      [user enters 6]
      [user enters 7]
      [user enters 8]
      [user enters 9]
      [user enters 0]
      [user enters 1]
      [user enters 2]
      The numbers you've entered are:
      3.000000
      4.000000
      5.000000
      6.000000
      7.000000
      8.000000
      9.000000
      0.000000
      1.000000
      2.000000
      

    Homework Problem 19.2, at the end of this problem you should submit to Laulima:

    • a file named UHusername_19_2.c
    • a file named UHusername_19_2_functions.c
    • a file named UHusername_19_2_functions.h
    • a file named UHusername_19_2_macros.h
      • example:
      • zhaol_19_2.c
      • zhaol_19_2_functions.c
      • zhaol_19_2_functions.h
      • zhaol_19_2_macros.h
    • update homework problem 19.1 to use a function when storing into the array and when reading from the array:

  • Optional Assignments

    Optional assignments are not graded and does not need to be turned in. It is intended to give you more practice (if you want it) using the programming concepts discussed in lecture. Some optional assignments are intended to introduce related concepts (if you care about it) not discussed in this course to broaden your knowledge about programming in general.

    If the optional assignment is modifying a required assignment, be sure to leave your previous assignment work untouched by copying them to a new folder for the optional assignment and modify only the files in the new folder

    Finish this Program with an Array:

    • Complete finish_array.c so the program prints out the following:

      • every element in the correct order:
        Correct Order: 1
        Correct Order: 3
        Correct Order: 4
        Correct Order: 5
        Correct Order: 6
        Correct Order: 8
        Correct Order: 9
        Correct Order: 12
        Correct Order: 16
        Correct Order: 19
      • every element in the reverse order:
        Reverse Order: 19
        Reverse Order: 16
        Reverse Order: 12
        Reverse Order: 9
        Reverse Order: 8
        Reverse Order: 6
        Reverse Order: 5
        Reverse Order: 4
        Reverse Order: 3
        Reverse Order: 1
      • print out every third element:
        Every third element: 1
        Every third element: 5
        Every third element: 9
        Every third element: 19
      • print out every value that is divisible by 3:
        All values that is divisible by 3: 3
        All values that is divisible by 3: 6
        All values that is divisible by 3: 9
        All values that is divisible by 3: 12

    Click here for the answer if you get stuck:

    Modifying Sound:

    Sound can be represented as a list of signal amplitudes:

    Computer programs are then able to store and manipulate audio information as an array of numbers. Write a program that reduces the volume of an audio signal and write another program that plays the audio signal backwards.

      Instructions:
    • Download the audio data:
      • wget https://raw.github.com/zhaol/ee160/gh-pages/code/array/sound_modifier/original.wav
      • wget https://raw.github.com/zhaol/ee160/gh-pages/code/array/sound_modifier/original.data
    • Include and use this function to convert your audio data file into a .wav file:

    Click here for the answer if you get stuck: