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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <ctime>
#include "Student.h"
using namespace std;
/*
* Read in a file containing student records and create an array of Student
* objects
*
* @param file The filename
* @param num The number of students in the file
* @return An array of Student objects
*/
Student* readStudentsFromFile(string filename, int num) {
ifstream studentsStream;
studentsStream.open(filename.c_str());
string line, name, school, sid;
int id;
static Student* readArray = new Student [num]; //An array of student object that will contain the read objects
if (!studentsStream.is_open()) {
cerr << "Couldn't open the file " << filename << endl;
return NULL;
}
while (!studentsStream.eof()) {
for(int i=0;i<num;i++) {
getline(studentsStream, line);
istringstream iss(line);
getline(iss, name, ',');
getline(iss, sid, ',');
getline(iss, school, ',');
istringstream idConv(sid);
idConv >> id;
readArray[i] = Student(id,name,school); //An array of Student objects
}//End of for loop
}//End of while
studentsStream.close();
return readArray;
}//End of readStudentsFromFile
/*
* Write an array of Student objects to a file
*
* @param students The array of Student objects to write out
* @param filename The output filename
*/
void writeStudentsToFile(Student students[], int num, string filename) {
ofstream output(filename.c_str());
output.open(filename);
for(int i=0;i<num;i++) {
string str=students[i].toString();
output << str<<"\n"<<endl;
}//End of for loop
output.close();
}//End of writeStudentsToFile
/*
* Find students belonging to both groups.
*
* This function checks each student in group1 for membership in group2 by
* comparing it with every student in group2.
*
* @param group1 A group of Students
* @param group2 A group of Students
* @param numCommon number of students belonging to both groups
* @return An array of Students which belong to both group1 and group2
*/
Student* findCommonStudents1(Student group1[], int len1, Student group2[],
int len2, int numCommon) {
Student *commonArray1 = new Student[numCommon];//An array that will hold the common students
int counter=0;
for(int i=0;i<len1;i++) {
for(int a=0;a<len2;a++) {
if(group1[i]==group2[a]) {
commonArray1[counter]=group1[i];
counter++;
}//enf of if statment
}//end of inner for loop
}//end of outer for loop
return commonArray1;
}//end of findCommonStudents1
/*
* Find students belonging to both groups.
*
* This function checks each student in group1 for membership in group2 by
* doing a binary search on group2.
*
* @param group1 A group of Students
* @param group2 A group of Students
* @param numCommon number of students belonging to both groups
* @return An array of Students which belong to both group1 and group2
*/
Student* findCommonStudents2(Student group1[], int len1, Student group2[],
int len2, int numCommon) {
int counter=0;
sort(group1, group1 + len1);
sort(group2, group2 + len2);
Student *commonArray2 = new Student[numCommon];
/*
* library function to binary search an array for the given value
* Note that values must be comparable with the < operator
*/
for(int i=0;i<len1;i++) {
bool found = binary_search(group2, group2 + len2, group1[i]);
if(found==true){
commonArray2[counter]=group1[i];
counter++;
}//End of if loop
}//End of for loop
return commonArray2;
}//End of findCommonStudents2
int main() {
/*
* DO NOT CHANGE ANY OF THE CODE BELOW
* Here just to help you with testing and timing your functions
*/
/***** These files provided to help you with initial testing *****/
const int UC_SIZE = 10;
const int SMC_SIZE = 5;
const int SMC_UC_GRADS_SIZE = 2;
Student* uc = readStudentsFromFile("sample_uc_students.txt", UC_SIZE);
Student* smc = readStudentsFromFile("sample_smc_grads.txt", SMC_SIZE);
/********************************** Use these files for the output you submit *************************/
//const int UC_SIZE = 350000;
//const int SMC_SIZE = 75000;
//const int SMC_UC_GRADS_SIZE = 25000;
//Student* uc = readStudentsFromFile("uc_students.txt", UC_SIZE);
//Student* smc = readStudentsFromFile("smc_grads.txt", SMC_SIZE);
// Rough timing
time_t start, end;
time(&start);
Student* common1 = findCommonStudents1(uc, UC_SIZE, smc, SMC_SIZE,
SMC_UC_GRADS_SIZE);
time(&end);
cout << "Using linear search it took " << difftime(end, start) << " seconds."
<< endl;
/*
* library sort function to sort an array: sort(arr, arr+size)
* Note that values must be comparable with the < operator
*/
sort(common1, common1 + SMC_UC_GRADS_SIZE);
writeStudentsToFile(common1, SMC_UC_GRADS_SIZE, "smc_grads_at_uc_1.txt");
time(&start);
Student* common2 = findCommonStudents2(uc, UC_SIZE, smc, SMC_SIZE,
SMC_UC_GRADS_SIZE);
time(&end);
cout << "Using binary search it took " << difftime(end, start)
<< " seconds." << endl;
sort(common2, common2 + SMC_UC_GRADS_SIZE);
writeStudentsToFile(common2, SMC_UC_GRADS_SIZE, "smc_grads_at_uc_2.txt");
delete[] smc;
delete[] uc;
delete[] common1;
delete[] common2;
return 0;
}
| |