Grouping Data and Image Processing with the Scribbler 2: An Overview
So far in this course, variables have identified individual pieces of data (e.g., an int, float, double, char) or arrays of these individual data elements. Although these variables have been quite useful, many applications require groupings of different types of data. Several examples follow.
-
A single note, played by a Scribbler 2 robot, involves both a frequency (an int) and a duration (a double); a tune requires a sequence of frequency/duration pairs.
-
A Scribbler motion requires a speed for the left motor, a speed for the right motor, and a duration; a dance requires a sequence of these left/right/duration triples.
-
A digital picture, such as is taken by the Fluke's camera, is organized into a two-dimensional grid of dots, where each dot (called a pixel) has a red, blue, and green component. Also, different pictures might have different sized grids. An entire picture, therefore, must include the size of the grid (e.g., a height and width) together with the 2-dimensional grid of pixels.
-
A Registrar's Office maintains information about students, where each student has a first name, last name, home address, telephone number, list of courses taken, number of credits, grade point average, etc.
This module explores two basic ways of grouping data, beyond the 1-dimensional array we have already encountered.
-
A struct provides a mechanism to group together several elements of data (either of the same type or with different types) .
-
A multi-dimensional array provides a mechanism to access data through several subscripts, just as a 1-dimensional array accesses data through a single subscript. For example, a 2-dimensional array allows organization of data into a grid or table.
Beyond these basic approaches, arrays and structs can be combined in flexible ways. For example, the red, green, and blue components of a pixel can be combined into a struct. Then a grid of pixels can be represented by a 2-dimensional array. Finally, a picture can be defined as a struct specifying a height, a width, and the 2-dimensional array of pixels.
Beyond these basic approaches for storage, many applications require data to be ordered (e.g., alphabetically by last name in the Registrar's Office or in decreasing light intensity in a picture).
This module explores the grouping of data, providing considerable experience with structs, 2-dimensional arrays, and even 1-dimensional arrays of structs. The module also introduces a relatively simple algorithm, called an insertion sort, for sorting arrays of data.
Later computing courses likely will investigate additional mechanisms for structuring and ordering data, building upon the basic ideas introduced here.
created 10 April 2016 by Henry M. Walker revised 10 August 2016 by Henry M. Walker |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |