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

We have been assigned this for our group project but it is not available in the

ID: 3550709 • Letter: W

Question

We have been assigned this for our group project but it is not available in the guided answers. We aren't looking to cheat or get the answers for free but would like to compare and figure out where we have gone wrong. Others have posted answers on here but the answers have holes. We'd like to see the answer as it would be presented in the guided answers section if possible. Did I mention the project is due by midnight? We think we have it but I just want to be sure.


Here is the question:


(Stock Market) Write a program to help a local stock trading company

automate its systems. The company invests only in the stock market. At the

end of each trading day, the company would like to generate and post the

listing of its stocks so that investors can see how their holdings performed

that day. We assume that the company invests in, say, 10 different stocks.

The desired output is to produce two listings, one sorted by stock symbol

and another sorted by percent gain from highest to lowest.


The input data is provided in a file in the following format:


symbol openingPrice closingPrice todayHigh todayLow

prevClose volume

For example, the sample data is:

MSMT 112.50 115.75 116.50 111.75 113.50 6723823

CBA 67.50 75.50 78.75 67.50 65.75 378233

.

.

.


The first line indicates that the stock symbol is MSMT, today

Explanation / Answer

Dear,


It is big project type question providing you some classes which would help you

listType class


#include <iostream>
#include <fstream>

using namespace std;

template <class T>
class listType
{
public:
    bool isEmptyList() const;
    // Function returns a nonzero value (TRUE)if list is empty,
    // otherwise it returns the value 0 (False)
    bool isFullList() const;
    // Function returns a nonzero value (TRUE)if list is full,
    // otherwise it returns the value 0 (False)
    void setLength(int len);
    int showLength() const;
    void search(T searchItem) const;
    // Search the list for searchItem
    // Postcondition: found is set to a nonzero value (TRUE)if    
    //    searchItem is found in the list,
    //    otherwise found is set to 0(False)
    void insert(T newElement);
    // Inserte newElement in the list
    // Prior to insertion list must not be full
    // Postcondition: list is old list plus the newElement
    void deleteItem(T deleteElement);
    // if deleteElement is found in the list it is deleted
       // If list is empty output the message "Cannot delete from the
    // empty list"
    // Postcondition: list is old list minus the deleteItem if
    //   deleteItem is found in the list
    void sort();
    // sort the list
    // Precondition: list must exist
    // Postcondition: list elements are in ascending order
    void print() const;
    // Output the elements of the list
    void getList(ifstream&);  
    // read and store elements in the list
    // Postcondition: length = number of elements in the list
    //    elements = array holding the input data
    void destroyList();
    // Postcondition: length = 0
    void printList() const;
    // Output the elements of the list
    listType(int listSize);
     // constructor with parameters
    // Create an array of size specified by the parameter listSize
    // Postcondition: elements contains the base address
    //    of the array, length = 0 and maxsize = listSize
    listType();
    // default constructor
    // Create an array of 50 components
    // Postcondition: elements contains the base address
    //    of the array, length = 0 and maxsize = 50
    ~listType();
    // destructor
    // delete all elements of the list
    // Postcondition: array elements is deleted
protected:
    void binarySearch(T searchItem,
                    int& found, int& index);

    int maxSize; // maximum number that can be stored in the list
    int length; // number of elements in the list
    T *elements; //pointer to the array that holds list elements
};


// Function returns a nonzero value (TRUE)if list is empty,
// otherwise it returns the value 0 (False)
template <class T>
bool listType<T>::isEmptyList() const
{
    return (length == 0);
}

// Function returns a nonzero value (TRUE)if list is full,
// otherwise it returns the value 0 (False)
template <class T>
bool listType<T>::isFullList() const
{
    return (length == maxsize);
}

template <class T>
void listType<T>::setLength(int len)
{
    length = len;
}

template <class T>
int listType<T>::showLength() const
{
    return length;
}


// Insert newElement in the list
// Prior to insertion list must not be full
// Postcondition: list is old list plus the newElement
template <class T>
void listType<T>::insert(T newElement)
{
    if(!isFullList())
    {
        elements[length] = newElement;
    }
    else
    {
        cout << "Cannot insert into a full list! ";
    }
}

// if deleteElement is found in the list it is deleted
// If list is empty output the message "Cannot delete from the
// empty list"
// Postcondition: list is old list minus the deleteItem if
//                deleteItem is found in the list
template <class T>
void listType<T>::deleteItem(T deleteElement)
{
    if(!isEmptyList())
    {
        T *temp = new T[length];

        int counter = 0;

        //Copy all items except the deleted item
        for(int i = 0; i < length; i++)
        {
            if(elements[i] != deleteElement)
            {
                temp[counter++] = elements[i];
            }
        }

        //Clear elements list
        delete [] elements;
        elements = new T[counter];
        length = 0;

        //Assign temp list to elements list
        for(int j = 0; j < counter; j++)
        {
            elements[j] = temp[j];
            length++;
        }
    }
    else
    {
        cout << "Cannot delete from the empty list! ";
    }
}

// Output the elements of the list
template <class T>
void listType<T>::print() const
{
    for(int i = 0; i < length; i++)
    {
        cout << elements[i];
    }
}
   
// Postcondition: length = 0
template <class T>
void listType<T>::destroyList()
{
    length = 0;
}

// Output the elements of the list
template <class T>
void listType<T>::printList() const
{
    for(int i = 0; i < length; i++)
    {
        cout << elements[i];
    }
}
   
// constructor with parameters
// Create an array of size specified by the parameter listSize
// Postcondition: elements contains the base address of the
//                   array, length = 0 and maxsize = listSize
template <class T>
listType<T>::listType(int listSize)
{
    maxSize = listSize;
    length = 0;
    elements = new T[maxSize];
}
    

// default constructor
// Create an array of 50 components
// Postcondition: elements contains the base address of the
//                   array, length = 0 and maxsize = 50
template <class T>
listType<T>::listType()
{
    maxSize = 50;
    length = 0;
    elements = new T[maxSize];
}
   
// destructor
// delete all elements of the list
// Postcondition: array elements is deleted
template <class T>
listType<T>::~listType()
{
    delete[] elements;
}

template<class T>
void listType<T>::sort()   //selection sort
{
    int i, j;
    int min;
    T temp;

    for (i = 0; i <length; i++)
    {
        min = i;
        for (j = i + 1; j < length; ++j)
           if (elements[j] < elements[min])
            min = j;
        temp = elements[i];
        elements[i] = elements[min];
        elements[min] = temp;
    }//end for
}//end sort



template<class T>
void listType<T>::getList(ifstream& infile)
{
    int i;

    for (i = 0; i < length; i++)
        infile >> elements[i];   
}

template<class T>
void listType<T>::search(T searchItem) const
{
    int found;
    int index;

    for(int i = 0; i < length; i++)
    {
        if(elements[i] == searchItem)
        {
            found = 1;
            index = i;
            break;
        }
    }

    if (found)
        cout << "Item is in the list" << endl;
    else
        cout << "Item is not in the list" << endl;
}

template<class T>
void listType<T>::binarySearch(T searchItem,
                               int& found, int& index)
{

    int first = 0;
    int last = length -1;

    int mid;

    found = 0;

    while( !found && (first <= last))
    {
        mid = (first + last) / 2;

        if (elements[mid] == searchItem)
            found = 1;
        else
            if (elements[mid] > searchItem)
                last = mid -1;
            else
                first = mid + 1;
    }

    if(found)
    {
        index = mid;
    }
    else
    {
        index = -1;
    }
}


StockType


class stockType
{
    friend ostream& operator<< (ostream&, stockType&);
    friend ifstream& operator>> (ifstream&, stockType&);

public:
    void setStockInfo(newString symbol, double openPrice,
                      double closeProce, double high,
                      double Low, double prevClose,
                    int    shares);
    newString getSymbol();
    double getPercentChange();
    double getOpenPrice();
    double getClosePrice();
    double getHighPrice();
    double getLowPrice();
    double getPrevPrice();
    int getNoOfShares();

    stockType();
    stockType(newString symbol, double openPrice,
            double closeProce, double high,
            double Low, double prevClose,
            int    shares);

    int operator ==(stockType &other);
    int operator !=(stockType &other);
    int operator <=(stockType &other);
    int operator >=(stockType &other);
    int operator >(stockType &other);
    int operator <(stockType &other);

private:
    newString    stockSymbol;
    double        todayOpenPrice;
    double        todayClosePrice;
    double        todayHigh;
    double        todayLow;
    double        yesterdayClose;
    double        percentChange;
    int        noOfShares;
};


ostream& operator<< (ostream &out, stockType &obj)
{
    out << setprecision(2) << fixed << right;
    out << setw(6) << obj.stockSymbol;
    out << setw(8) << obj.todayOpenPrice;
    out << setw(8) << obj.todayClosePrice;
    out << setw(8) << obj.todayHigh;
    out << setw(8) << obj.todayLow;
    out << setw(10) << obj.yesterdayClose;
    out << setw(8) << obj.getPercentChange() << "%";
    out << setw(8) << obj.noOfShares;
    out << endl;

    return out;   
}

ifstream& operator>> (ifstream &in, stockType &obj)
{
    in >> obj.stockSymbol;
    in >> obj.todayOpenPrice;
    in >> obj.todayClosePrice;
    in >> obj.todayHigh;
    in >> obj.todayLow;
    in >> obj.yesterdayClose;
    in >> obj.noOfShares;

    return in;
}

void stockType::setStockInfo(newString symbol, double openPrice,
                double closePrice, double high,
                double low, double prevClose,
                    int    shares)
{
    stockSymbol = symbol;
    todayOpenPrice = openPrice;
    todayClosePrice = closePrice;
    todayHigh = high;
    todayLow = low;
    yesterdayClose = prevClose;
    noOfShares = shares;
}

newString stockType::getSymbol()
{
    return stockSymbol;
}

double stockType::getPercentChange()
{
    double rslt = 0.0;

    rslt = (todayClosePrice - yesterdayClose) / yesterdayClose * 100;

    return rslt;
}

double stockType::getOpenPrice()
{
    return todayOpenPrice;
}

double stockType::getClosePrice()
{
    return todayClosePrice;
}

double stockType::getHighPrice()
{
    return todayHigh;
}

double stockType::getLowPrice()
{
    return todayLow;
}

double stockType::getPrevPrice()
{
    return yesterdayClose;
}

int stockType::getNoOfShares()
{
    return noOfShares;
}

stockType::stockType()
{
    stockSymbol = "";
    todayOpenPrice = 0.0;
    todayClosePrice = 0.0;
    todayHigh = 0.0;
    todayLow = 0.0;
    yesterdayClose = 0.0;
    noOfShares = 0;
}

stockType::stockType(newString symbol, double openPrice,
                double closePrice, double high,
                double low, double prevClose,
                    int    shares)
{
    stockSymbol = symbol;
    todayOpenPrice = openPrice;
    todayClosePrice = closePrice;
    todayHigh = high;
    todayLow = low;
    yesterdayClose = prevClose;
    noOfShares = shares;
}

int stockType::operator ==(stockType &other)
{
    return (stockSymbol == other.stockSymbol);
}

int stockType::operator !=(stockType &other)
{
    return (stockSymbol != other.stockSymbol);
}

int stockType::operator <=(stockType &other)
{
    return (stockSymbol <= other.stockSymbol);
}

int stockType::operator >=(stockType &other)
{
    return (stockSymbol >= other.stockSymbol);
}

int stockType::operator >(stockType &other)
{
    return (stockSymbol > other.stockSymbol);
}

int stockType::operator <(stockType &other)
{
    return (stockSymbol < other.stockSymbol);
}


stockListType class


class stockListType: public listType<stockType>
{
public:
    void printBySymbol();
    void printByGain();
    void printReports();
    void sort();
    void sortByGain();

    stockListType();
    stockListType(int size);
private:
    int   *indexByGain;
};


void stockListType::printBySymbol()
{
    sort();
    printReports();
}

void stockListType::printByGain()
{
    sortByGain();


    //Output file contents to screen
    cout << "********* First Investor's Heaven *********" << endl;
    cout << "*********     Financial Report     *********" << endl;
    cout << "Stock" << "              Today" << "                Previous Percent" << endl;
    cout << left << setw(6) << "Symbol" << setw(8) << " Open" << setw(8) << " Close" << setw(8) << " High";
    cout << setw(8) << " Low" << setw(10) << " Close" << setw(8) << " Gain" << setw(8) << " Volume" << endl;
    cout << setw(6) << "------" << setw(8) << " ------" << setw(8) << " ------" << setw(8) << " ------";
    cout << setw(8) << " ------" << setw(10) << " --------" << setw(8) << " ------" << setw(8) << " ------" << endl;

    //Iterate through list and print contents
    int i;
    for(i = 0; i < length; ++i)
        cout << elements[indexByGain[i]] << endl;

    //cout << "Closing Assets: $??????.??" << endl;
    cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
    cout << endl;
}

void stockListType::printReports()
{
    //Output file contents to screen
    cout << "********* First Investor's Heaven *********" << endl;
    cout << "*********     Financial Report     *********" << endl;
    cout << "Stock" << "              Today" << "                Previous Percent" << endl;
    cout << left << setw(6) << "Symbol" << setw(8) << " Open" << setw(8) << " Close" << setw(8) << " High";
    cout << setw(8) << " Low" << setw(10) << " Close" << setw(8) << " Gain" << setw(8) << " Volume" << endl;
    cout << setw(6) << "------" << setw(8) << " ------" << setw(8) << " ------" << setw(8) << " ------";
    cout << setw(8) << " ------" << setw(10) << " --------" << setw(8) << " ------" << setw(8) << " ------" << endl;

    //Iterate through list and print contents
    int i;
    for(i = 0; i < length; ++i)
        cout << elements[i] << endl;

    //cout << "Closing Assets: $??????.??" << endl;
    cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
    cout << endl;
}

void stockListType::sort()
{
    listType<stockType>::sort();
}

void stockListType::sortByGain()
{
    int i, j;
    int temp;
    indexByGain = new int[length];
   
    //Pre-populate the index array
    for(int cntr = 0; cntr < length; cntr++)
    {
        indexByGain[cntr] = cntr;
    }

    //Sort by the percentage change
    for (i = 0; i <length; i++)
    {
        for (j = i + 1; j < length; j++)
        {
           if (elements[indexByGain[j]].getPercentChange() > elements[indexByGain[i]].getPercentChange())
           {
                temp = indexByGain[i];
                indexByGain[i] = indexByGain[j];
                indexByGain[j] = temp;
           }
        }
    }//end for
}

stockListType::stockListType()
{
    indexByGain = new int[50];
}

stockListType::stockListType(int size)
{
    indexByGain = new int[size];
}


Hope this will help you!!


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote