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
What is a 2D array?:
Why are 2D arrays important?:
Implement this Table:
1 | 2 |
3 | 4 |
5 | 6 |
Build the Table with a 2D Array:
In Summary:
set_of_tables_of_values[TABLE][ROW][COLUMN]
set_of_tables_of_values[sub1][sub2]..[subn]
Programming Operation (In English) | Scalar Variables | 1D Array | 2D Array | 3D 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]){...} |
Modify 2d_arrays.c to be a 3D array
write a program that:
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
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 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
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
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.
Technologies | Simplistic Matrix Configuration |
---|---|
Sound | 1xM array |
Greyscale Image | MxN matrix |
Color Image | 3 MxN matrices |
Black and White Video | Sequence of MxN matrices |
Color Video | Sequence of 3 MxN matrices |
Hologram/3D | More Matrices |