• 2D Arrays

    What is a 2D array?:

    • same as a regular array (1-dimensional) but in 2 dimensions

    Why are 2D arrays important?:

    • same benefits as 1D arrays
    • 1D arrays are useful to represent lists
    • 2D arrays are useful to represent tables
  • Implementing a Table using a 2D Array

    Implement this Table:

      Table of Values
      1 2
      3 4
      5 6

    Build the Table with a 2D Array:

    In Summary:

      Things to note:
      • the subscript rule is:
      • the last (rightmost) subscript increases most rapidly, e.g. columns
      • the first (leftmost) subscript increases least rapidly, e.g. rows
    • to use 3D arrays (sets of tables), use 3 subscripts, e.g. set_of_tables_of_values[TABLE][ROW][COLUMN]
    • to use n-dimensional arrays, use n subscripts, e.g. set_of_tables_of_values[sub1][sub2]..[subn]
      • if you need to do this, consider using another programming language or tool like Python or Matlab
  • Comparing Usage of Multi-Dimensional Array

    Comparing Usage of Multi-Dimensional Array
    Programming Operation (In English)Scalar Variables1D Array2D Array3D Array
    Declaration/Initialization (Save exactly this much space for me to use)int scalar = 0;int list[MAX_COL] = {0};int table[MAX_ROW][MAX_COL] = {0};int book[MAX_PAGE][MAX_ROW][MAX_COL] = {0};
    Pass to Function (This is what you need to use; and here is where it begins)function(scalar);function(list);function(table);function(book);
    Function Prototype/Definition (I'll be using a list, but I'm not sure how long the list is; I'll be using a table of X columns, but I'm not sure how long the table is; I'll be using a book of tables with X columns and Y rows, but I'm not sure how many pages the book has)void function(int scalar);
    void function(int scalar){...}
    void function(int list[]);
    void function(int list[]){...}
    void function(int table[][MAX_COL]);
    void function(int table[][MAX_COL]){...}
    void function(int book[][MAX_ROW][MAX_COL]);
    void function(int book[][MAX_ROW][MAX_COL]){...}
      Things to note:
    • An example program implementing the above code snippets: scalar_array_2darray_3darray.c
    • Imagine stacking rows of wire frame cube shelves (like this one); the structure will be unstable if you stack a row of 3 cubes onto rows of 2 cubes; that is why C needs to know exactly how many you plan to stack in each row
  • 3D Arrays

    Modify 2d_arrays.c to be a 3D array

  • Assignment

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

    • a file named UHusername_31_1.c
      • example:
      • zhaol_31_1.c
    • write a program that:

      • allows a user to specify the size of a table s/he wants to populate
      • the user is then allowed to input values into the m x n table (m rows and n columns)
      • the program displays the table of values back to the user
        • it is sufficient to output the table in the same manner as 2d_arrays.c, i.e. you don't need to display borders
    • Test Case 1:
      How many rows are needed?
      [user enters 3]
      How many columns are needed?
      [user enters 5]
      Please enter value for [0][0]:
      [user enters 1]
      Please enter value for [0][1]:
      [user enters 2]
      Please enter value for [0][2]:
      [user enters 3]
      Please enter value for [0][3]:
      [user enters 4]
      Please enter value for [0][4]:
      [user enters 5]
      Please enter value for [1][0]:
      [user enters 6]
      Please enter value for [1][1]:
      [user enters 7]
      Please enter value for [1][2]:
      [user enters 8]
      Please enter value for [1][3]:
      [user enters 9]
      Please enter value for [1][4]:
      [user enters 10]
      Please enter value for [2][0]:
      [user enters 11]
      Please enter value for [2][1]:
      [user enters 12]
      Please enter value for [2][2]:
      [user enters 13]
      Please enter value for [2][3]:
      [user enters 14]
      Please enter value for [2][4]:
      [user enters 15]
      [0][0]: 1, [0][1]: 2, [0][2]: 3, [0][3]: 4, [0][4]: 5
      [1][0]: 6, [1][1]: 7, [1][2]: 8, [1][3]: 9, [1][4]: 10
      [2][0]: 11, [2][1]: 12, [2][2]: 13, [2][3]: 14, [2][4]: 15  
      

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

    • a file named UHusername_31_2.c
    • a file named UHusername_31_2_functions.h
    • a file named UHusername_31_2_functions.c
    • a file named 31_2.input
      • example:
      • zhaol_31_2.c
      • zhaol_31_2_functions.h
      • zhaol_31_2_functions.c
      • 31_2.input
    • Write a program that reads in a file (named 31_2.input) into a matrix of characters. The file has the following content:

      The program then "grows" the x's (i.e. for each x in the input, the output will have x's in it's adjacent positions) and prints out the new matrix of characters to the screen. The matrix of characters should now look like this:

      You can assume the arrangement (i.e. dimensions) of the input characters will not change

      Extra tidbit: Every digital picture, whether taken with a professional single lens reflex camera or a consumer mobile phone camera, at the very core is represented as a matrix of values. Each element of the matrix represents a pixel. The program you just made essentially boldened the edges of a picture. For more information about this, please read the problem statement for the picture problem in the "Optional Assignments" Section

  • 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

    Update 31.1 to output the averages

    • Modify the program to also output the averages for:
      • each column
      • each row

    Picture 31.2

    • Modify the program to generate a data file that will later be converted to an actual picture
      • The data file of a greyscale picture (i.e. a black and white photo with shades of grey) consists of integer values ranging from 0 to 255. This integer specifies how dark/light a pixel is. So 0 is black and 255 is white.
      • Convert the matrix of characters to a matrix of integers using the following transformation rules:
        • 'X' -> 0
        • 'O' -> 255
      • Output the matrix of integers into a data file named pic.data
      • For the input file specified in 31.2, pic.data will look like the following:
        255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 
        255 255 0 0 0 255 255 255 255 255 0 0 0 255 255 
        255 255 0 0 0 255 255 255 255 255 0 0 0 255 255 
        255 255 0 0 0 255 255 255 255 255 0 0 0 255 255 
        255 255 255 255 255 255 0 0 0 255 255 255 255 255 255 
        255 255 255 255 255 255 0 0 0 255 255 255 255 255 255 
        255 255 255 255 255 255 0 0 0 255 255 255 255 255 255 
        255 0 0 0 255 255 255 255 255 255 255 0 0 0 255 
        255 0 0 0 0 0 0 0 0 0 0 0 0 0 255 
        255 0 0 0 0 0 0 0 0 0 0 0 0 0 255 
        255 255 0 0 0 0 0 0 0 0 0 0 0 255 255 
        255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
      • Include and use this function to convert your image data file into a .png file:

        picture_helpers.c:

      • After your program creates your .png file, you should be able to double click it in the Cloud9 file tree and view it within Cloud9
      • You shoud see something like:
    • Let's think about what we just did:

      Using an integer to represent the intensity of gray, our single matrix of integers stores the information that can be translated to a grayscale image. If we had three matrices of integers, each matrix representing a different color (such as red, green, and blue (RGB)), we can then store the information necessary to represent a color image.

      Taking it one step further, if we had a sequence of singular matices, we can then represent a black and white video. One step after that, a sequence of three matrices, can be used to represent a color video. Going backwards in technology, sound can be represented as an array of integers. Looking to the future, holograms and our current pseudo 3D technologies, more matrices can be added to take into account spatial data.

      The hardest part with all these technologies is being able to compress the data so the data can be processed fast enough to be used, but the loss in data content cannot be noticeable to the human eye. For example, most pictures have many pixels that are the same color or close to the same color within the same region. Videos must be processed fast enough so it doesn't look choppy to the user.

      Summarizing
      TechnologiesSimplistic Matrix Configuration
      Sound1xM array
      Greyscale ImageMxN matrix
      Color Image3 MxN matrices
      Black and White VideoSequence of MxN matrices
      Color VideoSequence of 3 MxN matrices
      Hologram/3DMore Matrices