CS107 Study Guide

Unit 3: Working with Simple Data Structures

3a. Use arrays

  • What is an array?
  • How are loops useful when processing arrays?
  • What is a multidimensional array?

An array is a collection of elements of the same data type and those elements can be referenced using an index. Just like all variables in C++, arrays must be declared. For example,

int a[5];

declares an array that will contain 5 integers. Arrays can be simultaneously declared and initialized using notation such as:

int a[ ] = {2,3,4,-4,-7};

Elements within an array are referenced using an index starting with the integer 0. Loops are often useful tools for referencing elements where the loop variable can be used to form an index. For instance, code such as:

void main() {
float a[ ]={7,5,6,-7,-8,};
float s=0;
for (int i=0; i<5; i++)
s+=a[i];
}

will sum the elements contained in the array. Additionally, invoking the compiler directive:

#include <array>

will allow use of the array library that contains several life-simplifying methods such as size, empty, and swap.

Multidimensional arrays (also referred to as multi-subscript arrays) refer to array data that is distributed across several dimensions. The simplest extension is from the single subscript (one dimensional) array to a double subscript (two dimensional) array which you can imagine as having rows and columns. Each row can be thought of and treated syntactically as a one dimensional array. Therefore, two subscripts are required to access the array elements: the first index refers to the row and the second index refers to the column. For example:

int a[3][4] = { {1,2,3,4}, {3,4,5,6}, {4,5,6,7}}

initializes a two dimensional array with 3 rows and 4 columns resulting in a total of 12 elements contained within the array. Multidimensional arrays are often used with nested loops. For example, if you wanted to sum all the elements of the above array, you could run something like:

int s=0;
int i,j;
for (i=0; i<3; i++)
for (j=0; j<4; j++)
s += a[i][j];

To review, see Arrays and Multidimensional Arrays.


3b. Use struct, unions, and enumerations

  • What is a structure?
  • How are unions used?
  • When are enumerations useful?

Structures (or struct) originated in the C programming language and are the intermediate step on the way to class definition syntax for C++ object oriented programming. Whereas arrays can hold exactly one data type, structures allow you to include members of any data type. Therefore, it is important to review the use of the keyword struct in order to define a structure and understand how to declare a structure variable. Once a structure variable is declared, its members are referred to using dot notation. For example, a given structure variable v with member t would be referred to as v.t (assuming t is a single variable). Syntax for creating a structure generally looks like:

struct name {
variable declarations
};

where name is the name of the structure.

Unions are similar to structures in terms of their definition, syntax, and variable references; however, only one member of the union can be accessed at any time. This is because only one block of memory is allocated to the union as the size of its largest member. Unions are useful for organizing related data members into the same family where exactly one member will be assigned a value at any given time. Syntax for creating a union generally looks like:

union name {
    variable declarations
};

where name is the name of the union.

Enumerations are useful when one knows a priori a spectrum of values that can be referenced (i.e. the 'enumerators'). The keyword enum should be reviewed in order to understand the syntax. Enumerators are often used in tandem with switch statements where the case statements correspond to the enumerator values. Syntax for creating an enumeration generally looks like:

enum name {
enumerator list
};

where name is the name of the enumeration.

For a review see Structs, Unions, and Enumerations.


Unit 3 Vocabulary

This vocabulary list includes terms that students need to know to successfully complete the final exam for the course.

  • array
  • element
  • enumerations
  • index
  • multidimensional arrays
  • structures
  • unions
  • enum
  • struct
  • union