C++ code that I have so far.............. struct studentType { string name; int
ID: 3684826 • Letter: C
Question
C++ code that I have so far..............
struct studentType
{
string name;
int ID;
};
struct courseType
{
string name;
string code;
int noOfStudents;
studentType *studentList;
};
int main()
{
courseType theCourse;
theCourse.name = "Intro to Programming";
theCourse.code = "BCS 120";
cout << "Enter the number of students in the class: ";
cin >> theCourse.noOfStudents;
theCourse.studentList =
system("pause");
return 0;
}
Question:
In int main:
Answer the following question:
a. write C++ code so that theCourse has theCourse.noOfStudents number of students in its studentList.
b. write C++ code to input name and ID of each student from the keyboard into the studentList of theCourse.
c. write C++ code to output name and ID of each student in the studentList of theCourse.
d. write a C++ statement to de-allocate memory to the studentList pointer of theCourse.
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
struct studentType
{
string name;
int ID;
};
struct courseType
{
string name;
string code;
int noOfStudents;
studentType *studentList;
};
int main()
{
courseType theCourse;
theCourse.name = "Intro to Programming";
theCourse.code = "BCS 120";
cout << "Enter the number of students in the class: ";
cin >> theCourse.noOfStudents;
// Q a.
theCourse.studentList = new studentType[theCourse.noOfStudents];
//Q b
for(int i=0; i<theCourse.noOfStudents; i++){
cout<<"Enter name of "<<(i+1)<<" student: ";
cin>>theCourse.studentList[i].name;
cout<<"Enter ID of "<<(i+1)<<" student: ";
cin>>theCourse.studentList[i].ID;
}
//Q c
for(int i=0; i<theCourse.noOfStudents; i++){
cout<<"Name: "<<theCourse.studentList[i].name<<", ID: "<<theCourse.studentList[i].ID<<endl;
}
// Q d
delete[] theCourse.studentList;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.