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

https://www.cs.ucsb.edu/~aduncan/cs32/s17/labs/lab02/Roster.cpp = Roster.cpp. Fi

ID: 3826198 • Letter: H

Question

https://www.cs.ucsb.edu/~aduncan/cs32/s17/labs/lab02/Roster.cpp = Roster.cpp. Finish this Roster.cpp file using the testRosters (provided) making sure it follows the correct output. Using basic c++. Thank you!!!
test Roster include Student -h #include Roster. #include Kiost ream> finclude csstream- #include tddFuncs.h using namespace std; int main() cout "Testing class Roster endl. std: :string testRostersource std, string perm, lname,fnameln" "1234567, smith,Nalory Logan n" 5555555, Perez, Juanaln "2222222, Conrad, Phillip Todd n" 8888888, Preble Ethan Awesome In "1111111 Laux,Hunter n Roster ASSERT E Allows me to test reading from a stream without relying anything external to the test code i.e. another file on the file system. That is one of the principles of unit testing" std istringstream iss(testRostersource): r.addstudentsFromStream(iss); ASSERT E (5,r.getNumStudents()) ASSERT EQUALS(1234567 r.getStudentat(0) -getPer m)) ASSERT EQUALS( Perez ,r.getstudentat(1) .getLas tName ASSERT EQUALS Phillip Todd rr.getStudentAt (2).getFirstAndMiddleNames std :string expectedToStringResult std: :string "[1234567, Smith Malory Logan],In" "[5555555, Perez, Juana), Vn" "12222222 Conrad, Phillip Toddi, In 8888888, Preble,Ethan Awesome] [1111111 Laux, Hunter JIn" ASSERT EQUALS(expectedTostringResul t, r., toStrin g()) return 0

Explanation / Answer

//In this each student contains its data in string format in thisLine string object

//This code contains all methods implementations to pass cases in testRoaster1 and testRoaster3

//All related methods are implemented

//Read comments for descriptions

#include "Roster.h"

#include <cstdlib>
#include <fstream>
#include <cassert>
using namespace std;

Roster::Roster() {
// initialize to empty array

this->numStudents = 0;
for (int i=0; i<ROSTER_MAX; i++) {
this->students[i] = NULL;
}

}

void Roster::resetRoster() {
// To avoid memory leaks:
// Recycle memory for all allocated students on roster
  
while (this->numStudents > 0) {
delete this->students[this->numStudents - 1];
this->numStudents --;
}

}

void Roster::addStudentsFromFile(std::string filename) {
std::ifstream ifs; // the stream we will use for the input file
ifs.open(filename);
  
if (ifs.fail()) {
std::cerr << "Could not open input file: "
   << filename << std::endl;
exit(2);
}

// ifs is an instance of ifstream
// ifstream inherits from istream,
// i.e. ifs is-a istream

this->addStudentsFromStream(ifs);

}

void Roster::addStudentsFromStream(std::istream &is) {

this->resetRoster();

std::string thisLine;
// Try to read past the header line.
getline(is,thisLine);
if ( is.eof() || is.fail() ) {
std::cerr << "Unable to read first line of input stream" << std::endl;
exit(3);
}

getline(is,thisLine);
while ( !is.eof() && !is.fail() ) {
// If we get here, it means the most recent attempt to read succeeded!
// So do something with thisLine
  
Student *sPtr = new Student(thisLine);
this->students[this->numStudents] = sPtr;
this->numStudents++;
  
// try to read another line
getline(is,thisLine);
} // end while

}

//Returns total number of students records
int Roster::getNumStudents() const {
return this->numStudents;
//return -999; // stub
}
//Returns the student object at given index
Student Roster::getStudentAt(int index) const {
return this->students[index];
//return Student(-999,"Stubbi","Stubsdottir");
}
//Returns the First number or parameter in thisLine string
std::string getPerm()
{
string arr[]=this->thisLine.split(",");
return arr[0];
}
//returns the last name of student
std::string getLastName()
{
string arr[]=this->thisLine.split(",");
return arr[1];
}
//Returns first name of student
std::string getFirstName()
{
string arr[]=this->thisLine.split(",");
return arr[2].split(" ")[0];
}
//Returns first and middle name of the student
std::string getFirstAndMiddleName()
{
string arr[]=this->thisLine.split(",");
return arr[2];
}
//REturns string of all student records data
std::string Roster::toString() const {
std::string result = "{ ";
for(int i=0;i<=this->numStudents;i++)
{
result+=students[i]->thisLine;
}
// result += "STUB!!!!"; // @@@ RESTORE THIS

result += "} ";
return result;

}

/* DEFERRED TO LAB03

void Roster::sortByName() {
// stub does nothing
}
*/

void Roster::sortByPerm() {
// SELECTION SORT
// stub does nothing
}

int Roster::indexOfMaxPermAmongFirstKStudents(int k) const {
return 0; // STUB
}

void Roster::sortByPermHelper(int k) {
// swaps max perm from [0..k-1] with elem [k-1]


int im = indexOfMaxPermAmongFirstKStudents(k);

// now swap the pointers between index im and index k-1

// THIS IS STILL A STUB !!!
  
}