(N) Pointers in a vectorΒΆ
Goal:
I will learn how to use pointers and nullptr value for a missing data.
I will also understand what is the difference between two pointers
pointing to the same target vs two pointers pointing to two similar targets.
Instructions:
Retrieve the code template: templates/05/pointers/ ->
student/05/pointers/.
The main function in the template is enclosed with directives:
// Don't remove! Needed for tests.
#ifndef POINTERS_TEST
#define POINTERS_TEST
// Don't remove! Needed for tests.
// Main function (an example test case)
int main()
{
// main-funktion koodi
}
// Don't remove! Needed for tests.
#endif //POINTERS_TEST
// Don't remove! Needed for tests.
These directives are related to automated tests.
You need not care about them, and you must not remove or change them.
Directives must be kept surrounding the main function, and thus,
write the new functions above the lines shown above.
In this exercise, you will implement a program to handle students and courses. For simplicity reason, a student is described as a struct, but pointers would act in the same way, if we, instead of a struct, had a class. A course is described as a vector, the elements of which are pointers to student structs. In other words, a course vector contains pointers to those students that have enrolled in the course.
Naturally, a student can have enrolled in several courses. The program would also allow a student to be several times in the same course vector. This does not happen in the given main function, but it would be possible. You need not care about such possible, and you need not handle nor check such.
Implement the following functions in the program:
find_first_occurrence: Returns the first student occurrence (with the given name and birthday) from the given vector. If no occurrences found, returnsnullptr.find_same: Prints those students that can be found from both the given courses. (see print format from the example execution below).find_identical: Prints those students from the given course that have the same name and birthday but are still different students (see print format from the example execution below).
The function print has already been implemented.
After implementing the functions, the given main function works as follows:
Seaching for same students from two different courses
* Same student in two courses: Mortti --- 2001-01-01
* Same student in two courses: Vertti --- 2002-02-02
Searching for identical students from a course
* Identical students in a course: Mortti --- 2001-01-01
Searching for a certain student from a course
* Mortti --- 2001-01-01
* None
Test your program also with other cases by changing the main function in an appropriate way.
Tips for completing the assignment:
Studentstruct has the fieldsname_andbirth_day_. As you have a pointer to this struct:Student* student;
you can refer to the fields like this:
student->name_
In this kind of code, be careful that the pointer really points to something. Namely, if its value
nullptr(i.e. it points to nothing), your program will crash in the above kind of cases.
A+ presents the exercise submission form here.