$50

SENECA 244200 WS06 NUMBERS CLASS C++

0 ratings
Pay

SENECA 244200 WS06 NUMBERS CLASS C++

$50
0 ratings

Part 1 - LAB (50%) The Numbers Module

Your task for this lab is to complete the implementation of the Numbers class. This class reads several double values from a file (one number per line) and holds them dynamically in memory. Then the caller application has the option of adding numbers to the collection. This Module can provide information about the numbers and display them on the screen:

  • The Module can display the values in ascending order (sort).
  • The Module can find the largest value
  • The Module can find the smallest value
  • The Module can calculate the average of the values in the collection

Also, The Numbers class has the following capabilities:

  • A Numbers object can safely be copied or assigned to another Numbers module
  • A Numbers object can be displayed or inserted into an istream object.
  • A Numbers object can be read or extracted from an istream object.

When the Numbers object goes out of the scope the values overwrite the old values in the file, ONLY IF the object is the original object and not a copy.

The Numbers class

The Numbers class has four attributes:

    double* m_numbers; // holds the address of the dynamic array of doubles holding the number collection
    char* m_filename;  // holds the name of the file associated with the class (if it is the original object and not a copy)
    bool m_isOriginal; // true if this object is the original (and not a copy)
    int m_numCount; // holds the number of values in the m_number dynamic array

Constructors, Destructor and Copy Assignment

The Numbers class can be instantiated in three different ways:

One argument constructor (implemented)

Creates an original instance of the Numbers class by receiving the data file name as a constant Cstring

  • Sets the object to safe empty state (setEmpty()).
  • Sets the m_isOriginal flag to true.
  • Sets the filename of the class (setFilename())
  • Sets the number of the double values to be read from the file (sets m_numCount using numberCount())

If the number of double values in the file is greater than zero it tries to read the values from the file using the load() function as follows:

  • If not successful, it will free the memory pointed by m_numbers and m_filename
  • sets the object back to the safe empty state and sets the class NOT TO BE original anymore.

Default constructor (implemented)

  • Sets the object to safe empty state (setEmpty()).
  • Sets the m_isOriginal flag to false.

Destructor (implemented)

  • Saves the values in the file
  • deletes the memory pointed by m_numbers and m_filename

Copy Constructor (to be implemented by students)

  • Sets the object to the safe empty state
  • Sets the object NOT to be original (this will be done in next step)
  • Assigns the current object to the Numbers object that is being copied. (calls the Copy Assignment Operator)

Copy Assignment Operator (to be implemented by students)

If this is not a self-copy assignment

  • deletes the current collection of the double values
  • sets the object to the safe empty state.
  • sets the object not to be original
  • Allocates new memory pointed by m_numbers to the number of the values in the object that is being copied.
  • Copies all the double values in the object that is being copied into the newly allocated memory.

In any case the reference of the current object is returned

Member Functions and Member operator+= overload

deallocate() (implemented)

Deallocates the dynamic memory.

isEmpty() (implemented)

returns true if m_numbers is null

setEmpty() (implemented)

sets m_numbers and m_filename to nullptr. sets m_numCount to zero.

setFilename() (implemented)

  • Receives a constant Cstring for the data file name.
  • Frees the memory pointed by m_filename pointer
  • Allocates memory to hold the data file name as a Cstring.
  • Copies the data file name to the newly allocated memory.

max() (implemented)

Returns the largest double number in the list

min() (implemented)

Returns the smallest double number in the list

average() (implemented)

Returns the average of the double numbers in the list.

Numbers& sort(bool ascending) (to be implemented by students)

Based on the value of the ascending argument, This private method will sort the numbers in ascending or descending order and then returns the reference of the current Numbers object.

Use one of the sort algorithms you learned from IPC144 to sort the numbers:
IPC144 Sort Algorithm

Unary negation operator ( - ) (to be implemented by students)

Numbers operator-()const

This unary operator (that is incapable of modifying the current object) will return a descending sorted copy of the Numbers object.

Unary plus operator ( + ) (to be implemented by students)

Numbers operator+()const

This unary operator (that is incapable of modifying the current object) will return an ascending sorted copy of the Numbers object.

numberCount() (to be implemented by students)

This function (that is incapable of modifying the current object), will return the number of lines in the data file (hence returning the number of double values in the file).

  • Create an instance of ifstream to open the data file name.
  • While the ifstream object has not failed keep reading single characters from the file
  • Count the number of newline characters in an integer variable
  • Return the number of counted newlines when all characters in the file are scanned.

load() (to be implemented by students)

This function returns true if all the double values are read from the data file and stored in a dynamically allocated memory pointed by m_numbers, otherwise returns false.

  • If the number of double values in the file is greater than zero (m_numCount > 0)
  • Allocate memory to hold the double values in an array pointed by m_numbers.
  • Create an instance of ifstream for the data file
  • While the ifstream object has not failed, keep reading double values from the file into the elements of m_numbers dynamic array (as you do with cin)

make sure to add to the index of the loop only if the istream object has not failed in its last reading

  • return true if the number of read doubles is equal to the m_numCount member variable.

save() (to be implemented by students)

  • If the current object is the original and it is not in a safe empty state
  • Create an instance of ofstream to overwrite the data file
  • Write all the elements of the m_numbers array using the ofstream object.
  • Write a newline after each double value.

Operatro += (to be implemented by students)

Overload the += operator to add a single double value to the list of numbers in the array and then return the reference of the current object.

You need to increase the size of the allocated memory by one (add one double to the array), to be able to do this.

Here is the sequence of the actions to be taken to resize memory:

  • Create a temporary local pointer and allocate memory with the increased size
  • Copy all the current values to the newly allocated memory.
  • Update the size of the data to the new size
  • Now that all the values are copied to the new memory, delete the original allocated memory
  • Set the original data pointer to point to newly allocated memory
  • You are done!

View the Slides

Using the above guidelines; in our case, do the following only if the object is not in an empty state:

  • Create a temp double-pointer and allocate memory to the size: m_numCount + 1
  • In a loop, copy all the double m_numbers elements to the double temp number elements (up to m_numCount)
  • Add one to m_numCount (to update the number of double values)
  • copy the value of the double argument to the last element of the double temp array (and therefore add the double value to the list)
  • delete m_numbers to get rid of the original allocated memory
  • Set m_numbers to temp pointer; by doing this m_numbers will point to the newly allocated memory.

Regardless of whether the object is empty or not return the reference of the current object.

display function. (to be implemented)

   ostream& display(ostream& ostr) const

Displays the Numbers object on the screen. Note that all the double numbers are printed with two digits after the decimal point.

  • if the object is empty, write "Empty list".
  • if the object is not empty start by writing: "=========================" on ostr and then go to newline
  • if the object is flagged as original, then write the file name otherwise write "*** COPY ***" and then go to a new line
  • write all the double numbers separated by ", " (A comma and a space) and then go to a new line
  • write "-------------------------" and go to a new line
  • Write "Total of " << m_numCount << " number(s)" and go to new line
  • write "Largest number: " and the largest number in the list and go to a new line
  • write "Smallest number: " and the smallest number in the list and go to a new line
  • write "Average: " and the average of all the numbers in the list and go to a new line
  • write "=========================" and DO NOT GO TO NEWLINE

At the end return the ostr object.

Execution sample

A Numbers object that has the double values 34.1, 3, 6, 12.2 and 34.56 in a file called numbers.txt
should be displayed as follows:

=========================
numbers.txt
3, 6, 12.2, 34.1, 34.56
-------------------------
Total of 5 number(s)
Largest number:  34.56
Smallest number: 3
Average :        17.972
=========================

If a Number object is empty, it will be displayed as follows:

Empty list

Helper function overloads (to be implemented)

ostream& operator<<(ostream& os, const Numbers& N);

Overload the insertion operator to display the Numbers object using istream.

  • call the display function of N and return it
istream& operator>>(istream& istr, Numbers& N);

Overload the extraction operator to read the Numbers object using istream.

  • read a double value using istr
  • if the successful add the double value to N using the += operator
  • return istr;

the tester program

/***********************************************************************
// OOP244 Workshop 6 p1: tester program
//
// File  main.cpp
// Version 1.0
// Date  2022/10/1
// Author  Fardad Soleimanloo
//
//
// Revision History
// -----------------------------------------------------------
// Name            Date            Reason
/////////////////////////////////////////////////////////////////
***********************************************************************/
#include <iostream>
#include <fstream>
#include "Numbers.h"
using namespace sdds;
using namespace std;
void startTest(int n) {
   cout << "Test";
   for (int i = 0; i < 30; i++) cout << " " << n;
   cout << endl;
}
void endTest(int n) {
   cout << "End Test";
   for (int i = 0; i < 28; i++) cout << " " << n;
   cout << endl;
}

void resetFile(const char* fname) {
   ofstream f(fname);
   f.setf(ios::fixed);
   f.precision(2);
   f << 6.00 << endl << 12.20 << endl << 3.00 << endl << 34.10 << endl;
}
void displayFile(const char* fname) {
   ifstream f(fname);
   char ch;
   cout << endl << "Content of \"" << fname << "\"" << endl
      << "*****************************************************************" << endl;
   while (f.get(ch)) {
      cout << ch;
   }
   cout << "*****************************************************************" << endl;
}
void displayList(Numbers N) {
   cout << "Add a number to the list: ";
   cin >> N;
   cout << N << endl;
}
void test1() {
   startTest(1);
   Numbers N("numbers.txt");
   N += 34.56;
   cout << N << endl;
   displayList(N);
   endTest(1);
}
void test2() {
   startTest(2);
   Numbers N("numbers.txt");
   Numbers M(N);
   Numbers L;
   Numbers BAD("BadFileName.txt");
   cout << N << endl << M << endl << L << endl << BAD <<endl;
   cout << "Ascending:" << endl << +N << endl << "Descending:" << endl << -N << endl;
   M += 1000;
   L = M;
   N = L;
   cout << N << endl << M << endl << L << endl;
   cout << "Ascending:" << endl;
   cout << +N << endl << +M << endl << +L << endl;
   cout << "Descending: "<< endl;
   cout << -N << endl << -M << endl << -L << endl;
   endTest(2);
}
void test3() {
   startTest(3);
   cout << endl << "In this test we use your insertion and extraction operators to do" << endl
      << "File IO. This makes sure your implementation is done correctly" << endl 
      << "using the reference of istream and ostream and NOT cin and cout" << endl << endl;
   ifstream addnum("add.txt");
   ofstream output("output.txt");
   Numbers N("numbers.txt");
   addnum >> N;
   addnum >> N;
   addnum >> N;
   output << N << endl << +N << endl << -N << endl;
   endTest(3);
}
int main() {
   resetFile("numbers.txt");
   test1();
   displayFile("numbers.txt");
   test2();
   displayFile("numbers.txt");
   test3();
   displayFile("numbers.txt");
   displayFile("add.txt");
   displayFile("output.txt");
   return 0;
}

execution sample

The tester program's execution should generate the following output:


Test 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
=========================
numbers.txt
6.00, 12.20, 3.00, 34.10, 34.56
-------------------------
Total of 5 number(s)
Largest number:  34.56
Smallest number: 3.00
Average:         17.97
=========================
Add a number to the list: 123
=========================
*** COPY ***
6.00, 12.20, 3.00, 34.10, 34.56, 123.00
-------------------------
Total of 6 number(s)
Largest number:  123.00
Smallest number: 3.00
Average:         35.48
=========================
End Test 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Content of "numbers.txt"
*****************************************************************
6.00
12.20
3.00
34.10
34.56
*****************************************************************
Test 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
=========================
numbers.txt
6.00, 12.20, 3.00, 34.10, 34.56
-------------------------
Total of 5 number(s)
Largest number:  34.56
Smallest number: 3.00
Average:         17.97
=========================
=========================
*** COPY ***
6.00, 12.20, 3.00, 34.10, 34.56
-------------------------
Total of 5 number(s)
Largest number:  34.56
Smallest number: 3.00
Average:         17.97
=========================
Empty list
Empty list
Ascending:
=========================
*** COPY ***
3.00, 6.00, 12.20, 34.10, 34.56
-------------------------
Total of 5 number(s)
Largest number:  34.56
Smallest number: 3.00
Average:         17.97
=========================
Descending:
=========================
*** COPY ***
34.56, 34.10, 12.20, 6.00, 3.00
-------------------------
Total of 5 number(s)
Largest number:  34.56
Smallest number: 3.00
Average:         17.97
=========================
=========================
*** COPY ***
6.00, 12.20, 3.00, 34.10, 34.56, 1000.00
-------------------------
Total of 6 number(s)
Largest number:  1000.00
Smallest number: 3.00
Average:         181.64
=========================
=========================
*** COPY ***
6.00, 12.20, 3.00, 34.10, 34.56, 1000.00
-------------------------
Total of 6 number(s)
Largest number:  1000.00
Smallest number: 3.00
Average:         181.64
=========================
=========================
*** COPY ***
6.00, 12.20, 3.00, 34.10, 34.56, 1000.00
-------------------------
Total of 6 number(s)
Largest number:  1000.00
Smallest number: 3.00
Average:         181.64
=========================
Ascending:
=========================
*** COPY ***
3.00, 6.00, 12.20, 34.10, 34.56, 1000.00
-------------------------
Total of 6 number(s)
Largest number:  1000.00
Smallest number: 3.00
Average:         181.64
=========================
=========================
*** COPY ***
3.00, 6.00, 12.20, 34.10, 34.56, 1000.00
-------------------------
Total of 6 number(s)
Largest number:  1000.00
Smallest number: 3.00
Average:         181.64
=========================
=========================
*** COPY ***
3.00, 6.00, 12.20, 34.10, 34.56, 1000.00
-------------------------
Total of 6 number(s)
Largest number:  1000.00
Smallest number: 3.00
Average:         181.64
=========================
Descending:
=========================
*** COPY ***
1000.00, 34.56, 34.10, 12.20, 6.00, 3.00
-------------------------
Total of 6 number(s)
Largest number:  1000.00
Smallest number: 3.00
Average:         181.64
=========================
=========================
*** COPY ***
1000.00, 34.56, 34.10, 12.20, 6.00, 3.00
-------------------------
Total of 6 number(s)
Largest number:  1000.00
Smallest number: 3.00
Average:         181.64
=========================
=========================
*** COPY ***
1000.00, 34.56, 34.10, 12.20, 6.00, 3.00
-------------------------
Total of 6 number(s)
Largest number:  1000.00
Smallest number: 3.00
Average:         181.64
=========================
End Test 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

Content of "numbers.txt"
*****************************************************************
6.00
12.20
3.00
34.10
34.56
*****************************************************************
Test 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

In this test we use your insertion and extraction operators to do
File IO. This makes sure your implementation is done correctly
using the reference of istream and ostream and NOT cin and cout

End Test 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

Content of "numbers.txt"
*****************************************************************
6.00
12.20
3.00
34.10
34.56
-5.00
222.10
333.20
*****************************************************************

Content of "add.txt"
*****************************************************************
-5.00
222.10
333.20
*****************************************************************

Content of "output.txt"
*****************************************************************
=========================
numbers.txt
6.00, 12.20, 3.00, 34.10, 34.56, -5.00, 222.10, 333.20
-------------------------
Total of 8 number(s)
Largest number:  333.20
Smallest number: -5.00
Average:         80.02
=========================
=========================
*** COPY ***
-5.00, 3.00, 6.00, 12.20, 34.10, 34.56, 222.10, 333.20
-------------------------
Total of 8 number(s)
Largest number:  333.20
Smallest number: -5.00
Average:         80.02
=========================
=========================
*** COPY ***
333.20, 222.10, 34.56, 34.10, 12.20, 6.00, 3.00, -5.00
-------------------------
Total of 8 number(s)
Largest number:  333.20
Smallest number: -5.00
Average:         80.02
=========================
*****************************************************************

PART 1 Submission (lab)

Files to submit:

Numbers.cpp
Numbers.h
main.cpp

Data Entry

Enter 123 when prompted.

Submission Process:

Upload the files listed above to your matrix account. Compile and run your code using the g++ compiler as shown in Compiling and Testing Your Program and make sure that everything works properly.

Then, run the following command from your account

  • replace profname.proflastname with your professor’s Seneca userid
  • replace ?? with your subject code (200 or 244)
  • replace # with the workshop number
  • replace X with the workshop part number (1 or 2)
~profname.proflastname/submit 2??/w#/pX

and follow the instructions.

Important: Please note that a successful submission does not guarantee full credit for this workshop. If the professor is not satisfied with your implementation, your professor may ask you to resubmit. Re-submissions will attract a penalty.

DIY (50%)

Pay

Ask for 1 followup to fix error, add data, e.t.c

Copy product URL