Given the following declaration: const int MAX_CHAR = 100; struct Student { char
ID: 3695067 • Letter: G
Question
Given the following declaration:
const int MAX_CHAR = 100;
struct Student
{
char id[MAX_CHAR];
float gpa;
};
const int MAX_CAP = 10;
class StudentList
{
public:
int search(const char id[ ], Student& match) const;
bool get(int index, Student& aStudent) const;
bool remove(const char id[ ], Student& aStudent);
…
private:
Student list[MAX_CAP];
int size;
};
Implement the function: bool StudentList::remove(const char id[ ], Student& aStudent);
The function removes a student object with “id” from the list and copies the deleted student into “aStudent.” It returns true if the operation is successful and false if no matching id could be found.
Explanation / Answer
bool StudentList::remove(const char id[ ], Student& aStudent)
{
bool flag = false;
for(int i=0;i<size;i++)
{
if(strcmp(id,list[i].id)==0)
{
flag = true;
size--;
break;
}
}
for(;i<size;i++)
{
list[i]=list[i+1];
}
return flag;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.