int i, sum = 0;
int a[100];
for (i = 0; i < 100; i++) {
sum = sum + a[i];
#if 0 // #ifdef DEBUG
cout << i << " " << a[i] << " " << sum
#endif
}
cout << sum;
Arrays allow items of the same type to be grouped in the same structure.
Structures in C/C++ allow items of different types to be grouped together.
An element of an array is accessed using an index into an array.
An element of a structure is accessed using a field name for the element.
To access a field of the structure use the ``structname.fieldname''. Often
we have an address to a structure (a pointer) in which case we could use
``(*pstructname).fieldname'' or more commonly
``pstructname->fieldname''.
Example:
/*
* /cs/cs2005/pub/example/structexample.C -- structure example in C++
*/
#include <iostream.h>
#include <iomanip.h> // needed for formatting (manipulating) output
#define MAXNAME 12
struct datestruct {
int day; /* 1-31 */
int month; /* 1-12 */
int year; /* 1995 */
};
enum levels {mainfloor, balcony};
struct ticketstruct {
char sbFirst[MAXNAME]; /* first name */
char sbLast[MAXNAME]; /* last name */
struct datestruct date;
char chAisle; /* A to Z */
int wRow;
int wSeat;
enum levels level;
float cost;
};
void GetTicket(struct ticketstruct &tkt); // inclusion of param name optional
void PrintTicket(struct ticketstruct tkt); // inclusion of param name optional
void main(void)
{
struct ticketstruct tkt;
GetTicket(tkt);
PrintTicket(tkt);
}
/*
* GetTicket - get ticket values
*/
void GetTicket(struct ticketstruct &tkt) // call-by-reference parameter
{
strcpy(tkt.sbFirst, "Joe");
strcpy(tkt.sbLast, "Rocket");
tkt.date.day = 6;
tkt.date.month = 7;
tkt.date.year = 1996;
tkt.chAisle = 'Q';
tkt.wRow = 3;
tkt.wSeat = 12;
tkt.level = mainfloor;
tkt.cost = 25.00;
}
/*
* PrintTicket - print ticket values
*/
void PrintTicket(struct ticketstruct tkt)
{
cout << tkt.sbFirst << " " << tkt.sbLast << "\n";
cout << "on " << tkt.date.month << "/"
<< tkt.date.day << "/" << tkt.date.year << "\n";
cout << "aisle: " << tkt.chAisle << " row: " << tkt.wRow << " seat: "
<< tkt.wSeat;
switch (tkt.level) {
case mainfloor:
cout << " on the main floor\n";
break;
case balcony:
cout << " in the balcony\n";
break;
}
cout << "cost = $" << tkt.cost << "\n";
// ugly C++ output formatting to get the cents to print
// see pg. 214 of Shiflet text
cout << setiosflags(ios::showpoint|ios::fixed) << setprecision(2)
<< "cost = $" << tkt.cost << "\n";
}
Sample execution:
> structexample Joe Rocket on 7/6/1996 aisle: Q row: 3 seat: 12 on the main floor cost = $25 cost = $25.00
We could have an entire array of structures (versus having a parallel array for each field.
#define MAXTICKETS 15000
struct ticketstruct rgTicket[MAXTICKETS];
rgTicket[i].date.month; /* access the month field */
for (i = 0; i < MAXTICKETS; i++) {
GetTicket(rgTicket[i]);
PrintTicket(rgTicket[i]);
}
Could enclose all information about the polynomial in a single structure (have an array within the structure).
struct Polynomial {
int wDegree;
float rgCoeff[MAXCOEFF];
};
Polynomial poly; // in C++ can just use the name of the structure
/*
* /cs/cs2005/pub/example/polystruct.C -- read and print a polynomial in C++
*
* programmer -- Craig Wills
*
* date -- November 2, 1996
*
* modification history
*/
#define MAXCOEFF 10 /* maximum number of coefficients */
#include <iostream.h>
#define FALSE 0
#define TRUE 1
typedef short Boolean;
struct Polynomial {
int wDegree;
float rgCoeff[MAXCOEFF];
};
/* function prototypes */
void ReadPoly(Polynomial &);
void PrintPoly(Polynomial);
void main()
{
Polynomial poly; // in C++ can just use the name of the structure
/* read in and print a polynomial */
ReadPoly(poly);
PrintPoly(poly);
}
/*
* ReadPoly -- read and store the contents of a polynomial along with its
* degree.
*/
void ReadPoly(Polynomial &poly)
{
Boolean bOk; /* is the degree ok? */
int i;
/* obtain the degree of the polynomial */
bOk = FALSE;
while (!bOk) {
cout << "What is the degree of your polynomial? \n";
cin >> poly.wDegree;
if ((poly.wDegree < 0) || (poly.wDegree >= MAXCOEFF))
cout << poly.wDegree << "is not in the allowed degree range of 0-"
<< MAXCOEFF-1 << "\n";
else
bOk = TRUE;
}
/* obtain the coefficients of the polynomial */
for (i = poly.wDegree; i >= 0; i--) {
cout << "Enter the coefficient for the degree " << i << " term: ";
cin >> poly.rgCoeff[i];
}
}
/*
* PrintPoly -- print the contents of a polynomial given the coefficient
* list and the polynomial degree (in a rather ugly manner).
*/
void PrintPoly(Polynomial poly)
{
int i;
cout << "f(x) =";
for (i = poly.wDegree; i >= 0; i--) {
cout << " + " << poly.rgCoeff[i] << "x^" << i;
}
cout << "\n";
}