Structs/Classes

Handout 5

 

1.       Given the following struct:

struct bankCD

      {

            double amount;

            double interestRate;

            int years;

      };

 

a.    What is the name of the struct?

b.    What are the members of the struct?

c.    Has any memory been allocated?

d.    Declare two variables of type bankCD, and give all members values.

 

2.    What does the following code do?

bankCD deposits[10];

 

       int i;

       for (i = 0; i < 10; i++)

       {

              deposits[i].amount = 0;

              deposits[i].interestRate = .05;

              deposits[i].years = 1;

       }

3.    Suppose the following struct:

const int ARRAY_SIZE = 100;

 

       struct listType

       {

              string listElem[ARRAY_SIZE];

              int listLength;

       };

 

       struct sectionInfo

       {

              listType studentList;

              string sectionNum;

       };

 

 

       What is printed out by the following code?

sectionInfo CSC211;

 

       CSC211.studentList.listElem[0] = "Joe";

       CSC211.studentList.listElem[1] = "Mel";

       CSC211.studentList.listElem[2] = "Sal";

       CSC211.sectionNum = "9056";

       CSC211.studentList.listLength = 3;

 

       for ( int i = 0; i < CSC211.studentList.listLength; i++)

              cout << "Student # " << i << " of class " << CSC211.sectionNum << ":"

                     << CSC211.studentList.listElem[i] << endl;