hw09

cpp

School

Dartmouth College *

*We aren’t endorsed by this school

Course

189

Subject

Computer Science

Date

Nov 24, 2024

Type

cpp

Pages

5

Uploaded by ProfWildcatMaster683

Report
/*********************************************** Author: Shreya Mishra Date: November 29th, 2023 Purpose: Sources of Help: Dr. Lin's lecture slides Time Spent: 4-5 hours ***********************************************/ #include <iostream> #include <string> using namespace std; void testPFArrayD(void); //Objects of this class are partially filled arrays of doubles. class PFArrayD { public: PFArrayD(); //default constructor //Initializes with a capacity of 50. PFArrayD(int capacityValue); //value constructor PFArrayD(const PFArrayD& pfaObject); //copy constructor ~PFArrayD(); //Destructor void addElement(double element); //Precondition: The array is not full. //Postcondition: The element has been added. bool full() const { return (capacity == used); } //Returns true if the array is full, false otherwise. int getCapacity() const { return capacity; } int getNumberUsed() const { return used; } void emptyArray() { used = 0; } //Empties the array. double& operator[](int index); //Read and change access to elements 0 through numberUsed - 1. PFArrayD& operator=(const PFArrayD& rightSide); // copy assignment operator private: double* a; //for an array of doubles. int capacity; //for the size of the array.
int used; //for the number of array positions currently in use. }; class OutOfRange { public: OutOfRange(int index, string msg) :idx(index), err_msg(msg) {}; int index(void) { return idx; }; string err(void) { return err_msg; }; private: int idx; string err_msg; }; void testException() { int max = 5; PFArrayD obj1(max); try { for (int i = 0; i < max; i++) obj1.addElement(i); //try access invalid index double d1 = obj1[max + 1]; double d2 = obj1[-max]; obj1.addElement(max); //ex adding beyond capacity } catch ( OutOfRange &ex){ cout << "Index:" << ex.index() << " " << ex.err() << endl; } catch (...) { cout << "Some other exception" << endl; } } int main(int argc, char* argv[]) { cout << "This program tests the class PFArrayD.\n"; char ans; do { testPFArrayD(); cout << "Test again? (y/n) ";
cin >> ans; } while ((ans == 'y') || (ans == 'Y')); testException(); return 0; } void testPFArrayD() { int cap; cout << "Enter capacity of this super array: "; cin >> cap; PFArrayD temp(cap); cout << "Enter up to " << cap << " nonnegative numbers.\n"; cout << "Place a negative number at the end.\n"; double next; cin >> next; while ((next >= 0) && (!temp.full())) { temp.addElement(next); cin >> next; } cout << "You entered the following " << temp.getNumberUsed() << " numbers:\n"; int index; int count = temp.getNumberUsed(); for (index = 0; index < count; index++) cout << temp[index] << " "; cout << endl; cout << "(plus a sentinel value.)\n"; } PFArrayD::PFArrayD() : capacity(50), used(0) { a = new double[capacity]; } PFArrayD::PFArrayD(int cap) : capacity(cap), used(0) { a = new double[capacity]; }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
PFArrayD::PFArrayD(const PFArrayD& pfaObj) : capacity(pfaObj.capacity), used(pfaObj.used) { a = new double[capacity]; for (int i = 0; i < used; i++) a[i] = pfaObj.a[i]; } PFArrayD::~PFArrayD() { delete[] a; } void PFArrayD::addElement(double element) { if (used >= capacity) { throw OutOfRange(used, "Array capacity exceeded."); } a[used++] = element; } double& PFArrayD::operator[](int index) { if (index < 0 || index >= used) { throw OutOfRange(index, "Array index out of range."); } return a[index]; } PFArrayD& PFArrayD::operator=(const PFArrayD& rOp) { if (this != &rOp) { delete[] a; capacity = rOp.capacity; used = rOp.used; a = new double[capacity]; for (int i = 0; i < used; i++) a[i] = rOp.a[i]; } return *this; }
/* Computing III -- COMP.2010 Honor Statement The practice of good ethical behavior is essential for maintaining good order in the classroom, providing an enriching learning experience for students, and as training as a practicing computing professional upon graduation. This practice is manifested in the Universitys Academic Integrity policy. Students are expected to strictly avoid academic dishonesty and adhere to the Academic Integrity policy as outlined in the course catalog. Violations will be dealt with as outlined therein. All programming assignments in this class are to be done by the student alone. No outside help is permitted except the instructor and approved tutors. I certify that the work submitted with this assignment is mine and was generated in a manner consistent with this document, the course academic policy on the course website on Blackboard, and the UMass Lowell academic code. Date: Nov. 29th, 2023 Name: Shreya Mishra */