course.wilkes.edu/CS125Labs
Welcome to the site for CS 125 labs!

 

DoubleVectorOps code

Prev | Next | Lab 9

// DoubleVectorOps code has some code for DoubleVectorOps.cpp and grades.cpp

// code for FillVectors { string name; // input name into here double score; // input student's test score while (true) { infile >> name; if (infile.eof()) break; infile >> score; if (infile.eof()) break;

nameVec.push_back(name); scoreVec.push_back(score); } infile.close(); return; }

// code for Average { int numValues; // number of elements in vector double sum; // store total of the vector elements here numValues = theVec.size(); if (numValues > 0) { // use the line from web page for 'accumulate' into sum return sum / numValues; } else { cerr << "In Average, vector has no elements." << endl; exit(1); } }

// code for StandardDev { int numValues; // number of elements in vector numValues = theVec.size(); if (numValues > 0) { double avg; // the average of the values in numVec double sumSqrTerms = 0.0; // the sum of squared terms double term; // the sum of squared terms avg = Average(theVec); for (int i = 0; i < numValues; i++) { term = theVec[i] - avg; sumSqrTerms = sumSqrTerms + term * term; } return sqrt(sumSqrTerms / numValues); } else { cerr << "In StandardDev, vector has no elements." << endl; exit(1); } }

// code for ComputeLetterGrades { int numValues; // number of scores numValues = scoreVec.size(); vector<char> gradeVec(numValues); // store the grades here

if (numValues > 0) { double avg = Average(scoreVec); double stndrdDev = StandardDev(scoreVec); double F_CUT_OFF = avg - 1.5 * stndrdDev; double D_CUT_OFF = avg - 0.5 * stndrdDev; double C_CUT_OFF = avg + 0.5 * stndrdDev; double B_CUT_OFF = avg + 1.5 * stndrdDev; for (int i = 0; i < numValues; i++) { if (scoreVec[i] < F_CUT_OFF) gradeVec[i] = 'F'; else if (scoreVec[i] < D_CUT_OFF) gradeVec[i] = 'D'; else if (scoreVec[i] < C_CUT_OFF) gradeVec[i] = 'C'; else if (scoreVec[i] < B_CUT_OFF) gradeVec[i] = 'B'; else gradeVec[i] = 'A'; } } // end true part else { cerr << "Error in ComputeLetterGrades, no scores sent." << endl; exit(1); } // end else return gradeVec; }

// code for DisplayVectors { const int width = 17; // width between Name and Score int numValues = nameVec.size(); // number of lines to print int numSpaces; // number of spaces needed to get things to line up // print header out << "Name Score Grade" << endl; out << "==== ===== =====" << endl << endl; for (int i = 0; i < numValues; i++) { out << nameVec[i]; numSpaces = width - nameVec[i].size(); out << setw(numSpaces) << " "; // adjust spaces to get to score out << setiosflags(ios::fixed) << setprecision(1) << setw(5); out << scoreVec[i] << setw(4) << " " << gradeVec[i] << endl; } return; }

Prev | Next | Lab 9

 
  Home

Help

Lab 0

Lab 1

Lab 2

Lab 3a

Lab 3b

Lab 4

Lab 5

Lab 6

Lab 7

Lab 8

Lab 9

Lab 10

Lab 11

Lab 12

Lab 13



Membership

Login




Last update: Thursday, November 7, 2002 at 12:26:33 PM.
visitors to this page.