Question
in C++.
the project 2 is included here, scroll down please
Use the Stack class that you built in project 2 to implement span2 Download historical data for any stock of your choice Comp
# Algorithm spans2(X, n) Snew array of n integers Anew empty stack for i0 to n - 1 do while A.empty()A n 1 n LA.topO] Xi] ) d
Use the Stack class that you built in project 2 to implement span2 Download historical data for any stock of your choice Compute Span for each data point
# Algorithm spans2(X, n) Snew array of n integers Anew empty stack for i0 to n - 1 do while A.empty()A n 1 n LA.topO] Xi] ) do n А.роp() if A.empty) then n n I +!- [£]S else S[]i-A.top) A.push(i) n return S
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since the question doesn't want the explanation of the algorithm that has been used so I will skip that part and will directly provide you with a c++ code imitating the implementation of the algorithm spans2(). I will be taking the help of standard template library available in c++ to use the stack data structure instead of just writing it from scratch. So I really quite didn't understand what the question meant by downloading historical data for a stock of your choice. So what I did was looked at the data of Nifty50 from 15th May 2019 to 15th June 2019 and took their turnover to be the data points. I will provide you with the required screenshots. Okay enough talk, here is the c++ code and the required screenshots. The array X contains the turnover of Nifty50 from 15th May 2019 to 15th June 2019. I will provide you a screenshot of the input and the output respectively.

Input From the website:-

Turnover Date Open High Low Close Shares Traded (Cr) 15-May-2019 11271.70 11286.80 11157.00 414174258 11136.95 17931.75 11180

The code in C++:-

/Desktop/poop.cpp - Sublime Text (UNREGISTERED) poop.cpp X #include <bits/stdc++.h> //imports all headers 1 2 using namespace

Output:-

soumya@lvar: ~/Desktop soumya@Ivar:~/Desktop$ g++ -std=c++11 poop.cpp soumya@Ivar:~/Desktop$ ./a.out 1i3 4 11 7 1 1 3 1 2 3 1

Here is the text version of the code in case you need it. Just create a file named "inp.txt" and copy your input there along with the length of the data size at the very beginning and then run the code. Or you can just enter the data at runtime in which case just comment out the freopen line of the code:-

#include <bits/stdc++.h> // imports all headers

using namespace std;

inline void span(double X[], int n)
{
double S[n];
stack <int> A;
for(int i = 0; i < n; i++)
{
while(!A.empty() && X[A.top()] <= X[i])
A.pop();
if(A.empty())
S[i] = i + 1;
else
S[i] = i - A.top();
A.push(i);
}
for(int i = 0; i < n; i++)
cout << S[i] << " ";
cout << endl;
}

int main()
{
freopen("inp.txt", "r", stdin);
int n;
cin >> n;
double X[n];
for(int i = 0; i < n; i++)
cin >> X[i];
span(X, n);
return 0;
}
If you have any kind of doubt regarding understanding the solution or even in fact regarding the code and also if any kind of problem arises then just hit me up in the comment section down below and I will be there to help you out at any time. Thank you.

Add a comment
Know the answer?
Add Answer to:
in C++. the project 2 is included here, scroll down please Use the Stack class that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement...

    PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...

  • Implement these in Python 3 preferably. Task: Implement the following algorithms to solve the Stock Span...

    Implement these in Python 3 preferably. Task: Implement the following algorithms to solve the Stock Span Problem using the ADT for a Stack in ython. The implementation of the ADT of a Stack (10 points) Implementation of computeSpans1 (20 points) Implementation of computeSpan2 (30 points) Provide the derived computational complexity of both computeSpans1 and computeSpans2 (40 points) Algorithm computeSpans1 (P) Input: an n-element list P of numbers such that P[i] is the prices of the stock on day I Output:...

  • 1. Here are codes to define a stack class based on dynamic array, please complete the...

    1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available                         // copy original's array member into this new array {              // Please complete the function here        } else {          cerr << "*Inadequate memory to allocate...

  • 5. Stack (12 pts) Build a Stack class for a st The Stack should operat and...

    5. Stack (12 pts) Build a Stack class for a st The Stack should operat and methods also listed below ack of doubles that is compatible with the driver code below e in a LIFO (last in, first out) fashion and implement the variables a. Data Members: Declare the data item(s) you will need to manage a stack of doubles. Method Members: public void push (double input): This should add the double input to your stack public double pop ():...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • I have added a little Code but I need help with the rest. /** A class...

    I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> {    // Data fields    private Node<T> topNode; // references the first node in the chain    private int numberOfEntries;       public...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Need help with java In this program you will use a Stack to implement backtracking to solve Sudok...

    need help with java In this program you will use a Stack to implement backtracking to solve Sudoku puzzles. Part I. Implement the stack class.  Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only: public MyStack() //the constructor should simply set the topPtr to null public void push(E e) public E pop()  Test your class thoroughly before using it within the soduku program Part II. Create...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT