COSC 1437 TCC Monthlybudget Structure Designed Program Project

COSC 1437 Programming Fundamentals II
By FirstName LastName
11
Structures and
Abstract Data Types
PURPOSE
1.
2.
3.
4.
5.
PROCEDURE
1. Students should read the Pre-lab Reading Assignment before coming to lab.
2. Students should complete the Pre-lab Writing Assignment before coming to lab.
3. In the lab, students should complete labs assigned to them by the instructor.
To introduce the concept of an abstract data type
To introduce the concept of a structure
To develop and manipulate an array of structures
To use structures as parameters
To use hierarchical (nested) structures
Contents
Pre-requisites
Pre-lab Reading Assignment
Pre-lab Writing Assignment
Approximate
completion
time
Page
number
20 min.
196
Pre-lab reading
10 min.
205
Knowledge of previous
chapters
15 min.
205
Basic understanding of
structures
15 min.
206
Basic understanding of
arrays and structures
20 min.
208
Basic understanding of
20 min.
functions and nested logic
209
Check
when
done
LESSON 11 A
Lab 11.1
Working with Basic Structures
Lab 11.2
Initializing Structures
Lab 11.3
Arrays of Structures
LESSON 11 B
Lab 11.4
Nested Structures
195
196
LESSON SET 11 Structures and Abstract Data Types
P R E – LA B R E A D I N G A S S I G N M E N T
So far we have learned of data types such as float, int, char, etc. In some
applications the programmer needs to create their own data type. A user defined
data type is often an abstract data type (ADT). The programmer must decide
which values are valid for the data type and which operations may be performed
on the data type. It may even be necessary for the programmer to design new
operations to be applied to the data. We will study this style of programming extensively when we introduce object-oriented programming in the lesson set from
Chapter 13.
As an example, suppose you want to create a program to simulate a calendar. The program may contain the following ADTs: year, month, and day. Note
that month could take on values January, February, . . . , December or even 1,2,
. . . ,12 depending on the wishes of the programmer. Likewise, the range of values for day could be Monday, Tuesday, . . . , Sunday or even 1,2, . . . ,7. There
is much more flexibility in the choice of allowable values for year. If the programmer is thinking short term they may wish to restrict year to the range 1990–
2010. Of course there are many other possibilities.
In this lab we study the structure. Like arrays, structures allow the programmer to group data together. However, unlike an array, structures allow you
to group together items of different data types. To see how this could be useful
in practice, consider what a student must do to register for a college course.
Typically, one obtains the current list of available courses and then selects the
desired course or courses. The following is an example of a course you may
choose:
CHEM 310
Physical Chemistry
4 Credits
Note that there are four items related to this course: the course discipline (CHEM),
the course number (310), the course title (Physical Chemistry), and the number
of credit hours (4). We could define variables as follows:
Variable Definition
string discipline
int courseNumber
string courseTitle
short credits
Information Held
4-letter abbreviation for discipline
Integer valued course number
First 20 characters of course title
Number of credit hours
All of these variables are related because they can hold information about the
same course. We can package these together by creating a structure. Here is
the declaration:
struct course
{
string discipline;
int
courseNumber;
string courseTitle;
};
short credits;
//note the semi-colon here
The tag is the name of the structure, course in this case. The tag is used like a
data type name. Inside the braces we have the variable declarations that are
the members of the structure. So the code above declares a structure named
course which has four members: discipline, courseNumber, courseTitle, and
credits.
Pre-lab Reading Assignment 197
The programmer needs to realize that the structure declaration does not
define a variable. Rather it lets the compiler know what a course structure is
composed of. That is, the declaration creates a new data type called course. We
can now define variables of type course as follows:
course pChem;
course colonialHist;
Both pChem and colonialHist will contain the four members previously listed.
We could have also defined these two structure variables on a single line:
course pChem, colonialHist;
Both pChem and colonialHist are called instances of the course structure. In
other words, they are both user defined variables that exist in computer memory.
Each structure variable contains the four structure members.
Access to Structure Members
Certainly the programmer will need to assign the members values and also keep
track of what values the members have. C++ allows you to access structure
members using the dot operator. Consider the following syntax:
colonialHist.credits = 3;
In this statement the integer 3 is assigned to the credits member of colonialHist.
The dot operator is used to connect the member name to the structure variable
it belongs to.
Now let us put all of these ideas together into a program. Sample Program 11.1
below uses the course structure just described. This interactive program allows a
student to add requested courses and keeps track of the number of credit hours
for which they have enrolled. The execution is controlled by a do-while loop.
Sample Program 11.1:
#include
#include
#include
using namespace std;
// This program demonstrates the use of structures
// structure declaration
struct course
{
string discipline;
int courseNumber;
string courseTitle;
short credits;
};
continues
198
LESSON SET 11 Structures and Abstract Data Types
int main()
{
course nextClass; // next class is a course structure
int numCredits = 0;
char addClass;
do
{
cout > nextClass.discipline;
cout nextClass.courseNumber;
cout

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER