Lab 3 -- Arrays and Structs
Thursday, April 18, 2002
- Practice using structs and arrays, where one can contain the other
A multi-dimensional array can hold a lot of data, but that data must
be all the same type (all ints, all strings, etc.) A struct is
useful since it can "package up" sets of data that are of varied
types. By combining both arrays and structs, you can create
structures to hold data.
A class such as ESc 014 has many labs throughout the term.
You will complete a program that will access the lab scores for
a set of students and compute the average lab score for each student.
Grab the input files
On blackduck in the "esc014pub" directory is a directory named
"lab 3". Transfer this directory to the DesktopAnnex folder
on your desktop. Go back to lab 1 if you
need a refresher on the instructions.
Inside the "lab 3" folder are two files. One is the input file
a list of students' names and lab scores called "students.txt". Open it
The file is organized like this:
Sydney Stone <-- name of 1st student
10 <-- Sydney's 3 lab scores
Sharon Kelso <-- name of 2nd student
8 <-- Sharon's 3 lab scores
The second file is the starter code you should
begin with named "yourNameHere.cpp".
Change the filename to your
own name (keep the .cpp extension).
It contains two constants: the total number of labs per
student (MAX_LABS) and the total number of students (MAX_STUDENTS).
consists of two functions. I've already written
the first one for you, called readData. This function
will read in the data from the data file for you. Your task
will be to write the second function, called printAverages.
Create the struct
The data structure used in this lab is an array of structs. Your
first task is to define the struct itself. Remember, it needs to be
placed before the function prototypes at the top of your source
code. Create a struct called student that contains the
- a string named firstName for the student's
first name (make sure the capitalization is correct!)
- a string named lastName for the student's last name
- an array of ints named labs. This array
of length MAX_LABS will hold the student's individual lab scores.
Be sure to create your struct using the exact names as listed
above. Otherwise, the code I wrote for you in readData
Take a look at the main function. You'll see the
declaration of a variable named classroom that is an array
of student structs (the struct you just wrote).
This is the data structure that
will hold all the data from the input file.
Each slot in the classroom array
is a struct containing the first name, last name, and all
the lab info for a single student. The
picture below shows what this classroom array looks like
after the data has been read into it.
Write a function
Finally, you need to write the printAverages function that
will use the array of structs to display the following
information for each student:
- the student's first and last name
- the student's individual lab scores
- the student's average lab score
Be sure to label your output appropriately.
Your code for printAverages should NOT access the
data file at all. It should instead use the array
of structs (called someArray in the parameter)
to obtain the necessary information.
You should study the readData function to learn
how it works and how
it is accessing this array of structs. That will help
you to write the printAverages function.
Debug your code until it works.
Make sure you paste your output into your source code!
How to turn in this lab
Remember, you will be graded on the
correctness of your implementation, neatness, presentation, style of
program code, and good use of the C++ language. It's important to
code where appropriate and to do little things like space things
readable indentation, and also to make sure the overall design and logic
program are coherent.
Turn in a hard copy of your source code along with the output appended
at the bottom as a comment. Then turn in the source code
electronically on BlackBoard.
Return to Lab Index