ESc 014

Lab 3 -- Arrays and Structs
Thursday, April 18, 2002



A multi-dimensional array can hold a lot of data, but that data must still 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 sophisticated structures to hold data.

Lab summary

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 containing a list of students' names and lab scores called "students.txt". Open it up. 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
... etc.
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).

This program 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 following information:

Be sure to create your struct using the exact names as listed above. Otherwise, the code I wrote for you in readData won't work.

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:

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 your program code, and good use of the C++ language. It's important to comment your code where appropriate and to do little things like space things properly, use readable indentation, and also to make sure the overall design and logic of the 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