C++ Programming: From Problem Analysis to Program Design
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN: 9781337102087
Author: D. S. Malik
Publisher: Cengage Learning
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

I need this code to be with python 3

1. Approach: Non recursive:

//include necessary header files
#include <iostream>
using namespace std;
//main function
int main()
{
    int days,buy_on_this_day ,sell_on_this_day;
    //get number of days as input from user
    cout<<"Enter number of days: ";
    cin>>days;
    int stock_price[days];
    for(int i=0;i<days;i++)
    {   cout<<"Enter stock_price";
        cin>>stock_price[i];
    }
    int i=0;
    for(int i=0;i<days-1;i++)
    {
        //comparing  current price with next day price and finding the  minima
        while(i<days-1 && stock_price[i+1]<=stock_price[i])
        i++;

        if(i==days-1)
        break;
        buy_on_this_day  =i++;

        while(i<days && stock_price[i]>= stock_price[i-1])
        i++;
        sell_on_this_day =i-1;
        cout<<buy_on_this_day <<" : index of the change before  we buy"<<endl;
        cout<<sell_on_this_day<<"  :index of the change before we sell"<<endl;
    }
    int profit;
    int maxim =0;
    int minSofar = stock_price[0];
    for(int i=0;i<days;i++)
    {  //calculating the min, profit and max profit
        minSofar = min(minSofar, stock_price[i]);// update min s
        profit= stock_price[i]-minSofar; // update profit
        maxim = max(maxim,profit);// update maximum profit
        
    }
    cout<<"Maximum Profit: "<<maxim;
}

 

//2.APproach:Recurrsive:

//include necessary header files
#include <iostream>
using namespace std;
//recurrsive function to find max profit
int maxPro(int stock_price[], int starting_day, int last_day)
{
    if (last_day <= starting_day)
        return 0;
    int maximum_profit = 0;
    for (int i = starting_day; i < last_day; i++)
     {
        for (int j = i + 1; j <= last_day; j++)
         {
            if (stock_price[j] > stock_price[i]) 
            {
                //update  profit
                int pro = (stock_price[j] - stock_price[i]) + maxPro(stock_price, starting_day, i - 1) + maxPro(stock_price, j + 1, last_day);
               //update maximum profit
                maximum_profit = max(maximum_profit, pro);
            }
        }
    }
    return maximum_profit;
}
//main function
int main()
{   //declare variables
    int days,buy_on_this_day ,sell_on_this_day ;
    //get days and stock price as input
    cout<<"Enter number of days: ";
    cin>>days;
    int stock_stock_price[days];
    for(int i=0;i<days;i++)
    {    cout<<"Enter stock price";
        cin>>stock_stock_price[i];
    }
    int ans =maxPro(stock_stock_price,0,days-1);
    cout<<"max profit: "<<ans

 
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning