Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ 1.Given the following struct: struct bankCD { double amount; double interest

ID: 3750987 • Letter: C

Question

c++

1.Given the following struct:

struct bankCD

      {

            double amount;

            double interestRate;

            int years;

      };

What is the name of the struct?

What are the members of the struct?

Has any memory been allocated?

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;

       };

4. 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;

Explanation / Answer

Answer 1:

struct bankCD
{
   double amount;
   double interestRate;
   int years;
};

a) The name of this struct is "bankCD" as indicated in the declaration.

b) The members of this struct are amount, interestRate, years.

c) This is just the definition of a structure so it does not use any memory.

Answer 2:

bankCD deposits[10];                       //creates an array of size 10 of the bankCD struct
int i;
for (i = 0; i< 10; i++)           //loop runs 10 times each time i takes values ranging from 1-10
{
   deposits[i].amount = 0;              //amount of ith element is set to 0
   deposits[i].interestRate = .05; //interestRate of ith element is set to .05
   deposits[i].years = 1;                  //years of ith element is set to 1
}

3:

const int ARRAY_SIZE = 100;
struct listType
{
   string listElem[ARRAY_SIZE];          //string array of size 100
   int listLength;                                       //int to store length of array used
};
struct sectionInfo
{
   listType studentList;                             //listType variable
   string sectionNum;                              //string to store section number
};

Answer 4:

sectionInfo CSC211;                                              //creates a variable CSC211 of type sectionInfo
CSC211.studentList.listElem[0] = "Joe";           //first name in studentList is Joe
CSC211.studentList.listElem[1] = "Mel";           //first name in studentList is Mel
CSC211.studentList.listElem[2] = "Sal";           //first name in studentList is Sal
CSC211.sectionNum = "9056";                          //sectionNum is 9056
CSC211.studentList.listLength = 3;                  // number of students is 3
for ( int i = 0; i <CSC211.studentList.listLength; i++) //loop runs 3 times with values of i as 0, 1, 2
   cout << "Student # " << i << " of class " << CSC211.sectionNum<< ":"
             <<CSC211.studentList.listElem[i] << endl;

Final Output:

Student # 0 of class 9056:Joe
Student # 1 of class 9056:Mel
Student # 2 of class 9056:Sal

For Q2, what happens in each line is mention in the comments.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote