1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include<iostream>
#include<vector>
#include<string>
using namespace std;
#include "student.h"
//this function does NOT have bugs
void initialize(vector<Student> &group);
/*
original function given
it contains two bugs according to my professor
void copy_to_array( vector<Student> &group ){
int size = group.size();
Student * ptr = new Student[size];
for(int i = 0; i < size; i++){
ptr[i] = group[i];
ptr++;
}//for
return ptr;
}//copy_to_array
*/
//Contains bug(s)
void copy_to_array( vector<Student> &group ){
int size = group.size();
Student *ptr = new Student[size];
for(int i = 0; i < size; i++){
ptr[i] = group[i];
}//for
}//copy_to_array
//Contains bug(s)
void print_array(Student *ptr_to_array, int size){
for(int i = 0; i < size; i++){
ptr_to_array[i].print();//member function "print" of class Student is called
}//for
}//print_array
//Contains bug(s)
void init_array_of_pointers(Student *array_of_ptrs[], int &array_size, vector<Student> &group){
int group_size = group.size();
int min_size = 0;
for(int i = 0; i < array_size && i < group_size; i++){
array_of_ptrs[i] = new Student ( group[i].get_name(), group[i].get_gpa() );
min_size++;
}//for
array_size = min_size;
}//init_array_of_pointers
//Contains bug(s)
void print_via_pointers(Student *array_of_ptrs[], int size){
for(int i = 0; i < size; i++){
(*array_of_ptrs)->print();
array_of_ptrs++;
}
cout << "The first student is:" << endl;
array_of_ptrs[0]->print();
}//print_via_pointers
//Contains bug(s)
void release_space(Student *array_of_ptrs[], int size){
for(int i = 0; i < size; i++){
delete [] array_of_ptrs[i];
}//for
}//release_space
//DO NOT CHANGE code inside the main() function
//function main() DOES NOT have bugs
int main(){
vector<Student> group;
initialize(group); // reads in students from input, stores Student objects in the vector "group"
int size = group.size();
//declare a pointer to Student
Student *ptr_to_array = NULL;
//create an array of Students using "new" in the heap and copy content from the vector group into the array,
//ptr_to_array must point to the beginning of this array
ptr_to_array = copy_to_array( group );
//print students' information
print_array(ptr_to_array, size);
const int SIZE = 5;
Student *array_of_ptrs[SIZE];//SIZE has to be constant int
int updated_size = SIZE;//number of actually copied students from "group" into array of pointers
//initialize array of pointers with pointers to Student objects with the same names and gpas as those in the vector "group"
init_array_of_pointers(array_of_ptrs, updated_size, group);
cout << "Total copied students into array of pointers is " << updated_size << endl;
//print students information
print_via_pointers(array_of_ptrs, updated_size);
release_space(array_of_ptrs, updated_size);
delete [] ptr_to_array;
return 0;
}
//this function does NOT have bugs
void initialize(vector<Student> &group){
string aname;
double agpa;
while( cin >> aname ){
cin >> agpa;
Student current(aname, agpa);//an object of type Student is created, and a constructor of the class initializes name with aname and gpa with agpa
group.push_back(current);
}
}//initialize does not have bugs
|