University of Washington Computer Programming I: Nested Data Structures
University of Washington Computer Programming I: Nested Data Structures
Computer Programming I
Lecture 20:
Nested Data Structures
Arrays of structs
U-2
Data Representation in C
Simple data types
int, double, char
Atomic chunks of data - cannot be
pulled apart into components
Composite data
Arrays
Structs
U-3
Composite Data
U-4
Composite Data
Arrays
Sequence of variables all of the same type
structs
Collection of fields of possibly different
types
rectangle r;
Field Selection
Use the . operator to r
select a field.
If the field it itself a width
struct, use . again to size height
select its
components
x
lower_left
y
line_color
fill_color
U-8
Field Selection
Use the . operator to r
select a field.
If the field it itself a width
struct, use . again to size height
select its
components
x
r lower_left
y
line_color
fill_color
U-9
Field Selection
Use the . operator to r
select a field.
If the field it itself a width
struct, use . again to size height
select its
components
x
r lower_left
y
r.lower_left
line_color
fill_color
U-10
Field Selection
Use the . operator to r
select a field.
If the field it itself a width
struct, use . again to size height
select its
components
x
r lower_left
y
r.lower_left
r.lower_left.x line_color
fill_color
U-11
Structures and Arrays
U-15
Structures and Arrays
A struct represents a single record
Typically, computer applications have to deal
with collections of such records
Examples: student records, employee
records, customer records, parts records
In each case we will have multiple instances
of one record (struct) type
x
y
x
y
x
y
x
y
#define MAX_NAME 40
typedef struct {
char name [MAX_NAME+1] ;
int id ;
double score ;
} student_record ;
U-20
Review: structs as Parameters
A single struct is passed by value
all of its components are copied from the argument
(actual parameter) to initialize the (formal)
parameter, even if they are arrays (unless you use
pointers explicitly)
point midpoint (point a, point b) {...}
int main (void) {
point p1, p2, m; /* declare 3 points */
...
m = midpoint ( p1, p2); U-23
}
Passing Arrays of structs
An array of structs is an array.
When any array is an argument (actual
parameter), it is passed by reference (not copied)
U-25
Sorting Arrays of structs
Bill Will Gill Phil Jill
920915 901028 900317 920914 910607
2.9
4.0 3.9 2.8 3.6
U-26
Sorting Arrays of structs
Bill Will Gill Phil Jill
920915 901028 900317 920914 910607
2.9
4.0 3.9 2.8 3.6
} StudentRecord ;
Review: Selection Sort
/* Sort b[0..n-1] in non-decreasing order
(rearrange elements in b so that
b[0]<=b[1]<=…<=b[n-1] ) */
U-29
Helper for Selection Sort
/* Find location of smallest element in b[k..n-1] */
/* Returns index of smallest, does not return the
smallest value itself */
U-30
Helper for Selection Sort
/* Find location of smallest element in b[k..n-1] */
/* Returns index of smallest, does not return the
smallest value itself */
int min_loc (int b[ ], int k, int n) {
int j, pos; /* b[pos] is smallest element */
pos = k; /* found so far */
for ( j = k + 1; j < n; j = j + 1)
if (b[j] < b[pos])
pos = j;
return pos;
}
U-31
Helper for Selection Sort
/* Find location of smallest element in b[k..n-1] */
/* Returns index of smallest, does not return the
smallest value itself */
int min_loc (int b[ ], int k, int n) {
int j, pos; /* b[pos] is smallest element */
pos = k; /* found so far */
for ( j = k + 1; j < n; j = j + 1)
if (b[j] < b[pos])
pos = j;
return pos;
}
/* Interchange values */
U-32
void swap (int * x, int * y);
Modifying for Array of StudentRecord
U-33
Modifying for Array of StudentRecord
U-34
Selection Sort Helper Modified
/* Sort b[0..n-1] in non-decreasing order
(rearrange elements in b so that
b[0]<=b[1]<=…<=b[n-1] ) */
U-36
Selection Sort Modified
/* Find location of smallest element in b[k..n-1] */
/* Returns index of smallest, does not return the
smallest value itself */
U-37
Selection Sort Modified
/* Find location of smallest element in b[k..n-1] */
/* Returns index of smallest, does not return the
smallest value itself */
int min_loc (StudentRecord b[ ], int k, int n) {
int j, pos; /* b[pos] is smallest element */
pos = k; /* found so far */
for ( j = k + 1; j < n; j = j + 1)
if (b[j].score < b[pos].score)
pos = j;
return pos;
}
U-38
Selection Sort Modified
/* Find location of smallest element in b[k..n-1] */
/* Returns index of smallest, does not return the
smallest value itself */
int min_loc (StudentRecord b[ ], int k, int n) {
int j, pos; /* b[pos] is smallest element */
pos = k; /* found so far */
for ( j = k + 1; j < n; j = j + 1)
if (b[j].score < b[pos].score)
pos = j;
return pos;
}
U-39
/* Interchange values */
void swap (StudentRecord * x, StudentRecord * y);
Alphabetical Order
Phil Harry Susan David Sarah
920914 910607 901028 920915 900317
2.8 3.6 4.0 2.9 3.9
U-40
Alphabetical Order
Phil Harry Susan David Sarah
920914 910607 901028 920915 900317
2.8 3.6 4.0 2.9 3.9
typedef struct {
char name[MAX_NAME + 1];
int id;
double score;
} student_record;
U-41
#include <string.h>
int strcmp (char str1[ ], char str2[ ])
U-44
Modified to Sort by Name
The only change from sorting by score is in the
function min_loc
U-45
Modified to Sort by Name
The only change from sorting by score is in the
function min_loc
U-48
Type Quiz
typedef struct {
char name [MAX_NAME+1];
int id ;
double score ;
} StudentRecord ;
StudentRecord a [MAX_STUDENTS];
U-49
Type Quiz
typedef struct {
char name [MAX_NAME+1];
int id ;
double score ;
} StudentRecord ;
StudentRecord a [MAX_STUDENTS];
/*What is the type of each?*/
a a[0] a[5].name
a[4].id &a[6].score a[2].name[1]
U-50
a.score[0] StudentRecord[1]
Data Structures: What If...
...you wanted to keep information about one
song on the computer.
What pieces of data would you want?
How would you organize them?
How would it look in C?
And then…
What if you wanted information about an
entire CD of songs?
And then… how about a whole collection of
CD's?
U-51
Summary
Arrays and structs can be combined and nested to
any level