The daily USD/EGP exchange prices are available over the period from December 1, 2016 to April 21, 2017 in the file : “USD-EGP.txt” ). Assuming that the average exchange price over the given period is M, a change is positive when the price rises over M, and it is negative when it drops below that average. From the given data set, we need to find the dates of each of the N most positive changes (e.g. N = 10) as they represent the N highest exchange prices over the whole data set. Likewise, we need to find the dates of each of the N most negative changes (e.g. N = 10) as they represent the N lowest exchange prices over the whole data set.
This problem can be solved using Priority Queues where each item is composed of 2 elements: the date and the exchange price change from the average. Priority here is for the price change.
We also need to find the start date and the end date of the contiguous period over which the sum of price changes is maximum. This problem is called the Maximum Subsequence Problem. The problem statement is as follows: Given a sequence of integers (possibly negative), a1,a2,...,an, find the values of the indices
(i,j) that maximizes the value of the sum
(This is zero if all values are negative). Example: Suppose the changes in price are: - 0.2, 1.1, - 0.4, 1.3, - 0.5, - 0.2 Smax = a2+a3+a4= 2.0 (i = 2 and j = 4)
Required Implementations: 1. Implement the Min and Max PQ classes. 2. Find the N highest and N lowest exchange rate days over the whole data set (e.g. N = 10) 3. Implement the Maximum Subsequence Algorithm to find the start and end days of the contiguous period over which the sum of price changes is maximum. ___________________________________________________________________________
#include <iostream>
#include <cctype>
#include <limits>
int main()
{
std::cout << "Do you want someone to do all the work for you? ";
char answer{};
std::cin >> answer;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::toupper(answer) == 'Y' ) { std::cout << "Post it in the Jobs section.\n"; }
else { std::cout << "Show what you have coded so far.\n"; }
std::cout << "Good luck.\n";
}
// File: PQ.h
// Priority Queue header file (Minimum Heap)
/* ______________________________________________________________________________
The PQ is implemented as a minimum Heap.
The elements of the heap are of type (E).
The top of the heap will contain the smallest element.
The heap condition is that a parent is always
less than or equal to the children.
The heap is implemented as a dynamic array a[] with a
size specified by the class constructor.
Location a[0] is reserved for a value "itemMin" smaller
than any possible value (e.g. a negative number)
_________________________________________________________________________________
*/
#ifndef PQ_H
#define PQ_H
template <class E>
class PQ
{
public:
// Class Constructor with input size parameter
PQ(int , E);
// Class Destructor
~PQ();
// Member Functions
void insert(E); // insert element into heap
E remove(); // remove & return the top of the heap
void dispHeap(); // Display Heap Array as a tree
private:
// Pointer to Storage Array
E *a;
// Maximum Size (not including a[0])
int MaxSize;
int N; // index of last element in the array
E itemMin; // itemMin to be stored in a[0]
// Heap Adjustment Functions
void upheap(int k);
void downheap(int k);
void disp_Level(int Nrows, int level, int s, int e);
};
#endif // PQ_H
// File:PQ.cpp
// PQ (min Heap) Class implementation
// Constructor with argument. Max size is mVal elements
// not including a[0] which will receive -32767
// The constructor creates the heap array, initializes
// the end of the heap to N=0,and itmMin
template <class E>
PQ<E>::PQ(int mVal , E lm)
{
MaxSize = mVal;
a = new E[MaxSize + 1]; N = 0;
itemMin = -32767; // Minimum Heap
a[0] = itemMin;
}
// Class Destructor
template <class E>
PQ<E>::~PQ()
{
delete[] a;
}
// Heap Adjustment Functions
// upheap element at location (k) in the heap
// as long as it is less than the parent
template <class E>
void PQ<E>::upheap(int k)
{
E v = a[k];
while (a[k / 2] >= v)
{
a[k] = a[k / 2]; k = k / 2;
}
a[k] = v;
}
// downheap element at (k) in the heap
template <class E>
void PQ<E>::downheap(int k)
{
int j = 2 * k; E v = a[k];
while (j <= N) {
if ((j < N) && (a[j] > a[j + 1])) j++;
if (v <= a[j]) break;
a[j / 2] = a[j]; j *= 2;
}
a[j / 2] = v;
}
// Insert element (v) in the heap and adjust heap
template <class E>
void PQ<E>::insert(E v)
{
a[++N] = v; upheap(N);
}
// remove and return top of the heap, then adjust heap
template <class E>
E PQ<E>::remove()
{
E v = a[1];
a[1] = a[N--]; downheap(1);
return v;
}
template <class E>
void PQ<E>::dispHeap()
{
int s = 1, e = 1, rlength, k,
Nlevels = int(ceil(log(float(N)) / log(2.0) + 0.01));
for (int level = 0; level < Nlevels; level++)
{
disp_Level(Nlevels, level, s, e);
rlength = e - s + 1;
s = e + 1;
k = e + 2 * rlength;
e = (k < N) ? k : N;
}
}
template <class E>
void PQ<E>::disp_Level(int Nrows, int level, int s, int e)
{
int skip = int(pow(2.0, Nrows - level) - 1);
for (int i = s; i <= e; i++)
{
cout << setw(skip) << " ";
cout << setw(2) << a[i];
cout << setw(skip) << " ";
}
cout << "\n\n\n";
}
int sum = 0;
int M = 20;
const int N = 10; // N changes
string date;
string price; // from the file there are the dates and the prices;
//int N;
PQ<int>Heap(16,-32767); // PQ Class
string word;
//int M; // Average Exchange Price
int A[N];
void Fill() // function to open text file that contains the exhange prices
{
ifstream infile;
infile.open("File.txt"); // open the input file
if (!infile.fail())
{
while (!infile.eof()) // while not end of file
{
getline(infile, date, ' ');
getline(infile, price,'\n');// read each word from the input file