Question

In: Computer Science

Assume that the monthly profits of a company are given in an array, each entry representing...

Assume that the monthly profits of a company are given in an array, each entry representing
the profits for the corresponding month. Propose an algorithm to find a group of consecutive
months where the profit was the highest.
For example, the profits, in millions, for a year Jan-Dec is given by
A=[2 -5 3 4 0 -1 5 8 -9 1 -2 0];
The highest profit was from the period March-August and the value is 19 million. Assume
that the array can be very large{not just 12 months. Submit a well comment c++ pseudocode along
with an example of the algorithm

Solve for algorithms with complexity O(n)

Solutions

Expert Solution

Code:

#include <iostream>
#include <algorithm>

using namespace std;

//Algorithm
//Find the local maxima and global maximum at each month
//local maxima can be found out for each month by taking the maxima of
//the previous local_maxima and the particular month revenue with the
//Particular month revenue

int main()
{
   //Declare the array
   int a[] = { 2,-5,3,4,0,-1,5,8,-9,1,-2,0 };
   int size = 12;

   //initilaise the local maxima and global maxima to be the first element at the start
   int local_maxima = a[0];
   int global_maxima = a[0];

   //start the loop from the second element of the array
   for (int i = 1; i < size; i++)
   {
       //For each month revenue find the local_maxima
       //the local maxima will be equal to the maximum of the sum of previous local maximum
       //with the present month revenue
       //durig each pass of the loop check whether the local maximum is larger than
       //global_maximum if that is the case then update the global_maximum

       local_maxima = std::max(local_maxima + a[i], a[i]);

       if (local_maxima > global_maxima)
           global_maxima = local_maxima;
   }

   cout << "Maximum revenue for the consecutive month is : " << global_maxima;

   return 0;
}

OUTPUT:


Related Solutions

Assume that you have an array of 100 elements representing the grades students are stored in...
Assume that you have an array of 100 elements representing the grades students are stored in memory. Suppose the grades are in IEEE single precision format. Write a MIPS program to compute the average of the students grades and store the result in register $f0. Assume the array base address is stored in register $s0 and a floating point value of 100 is stored in memory with it address given in register $s2.
Given a total due and an array representing the amount of change in your pocket, determine...
Given a total due and an array representing the amount of change in your pocket, determine whether or not you are able to pay for the item. Change will always be represented in the following order: quarters, dimes, nickels, pennies. To illustrate: changeEnough([25, 20, 5, 0], 4.25) should yield true, since having 25 quarters, 20 dimes, 5 nickels and 0 pennies gives you 6.25 + 2 + .25 + 0 = 8.50. Examples changeEnough([2, 100, 0, 0], 14.11) ➞ false...
Matlab Given an array of monthly rainfall that covers some number of years (where each column...
Matlab Given an array of monthly rainfall that covers some number of years (where each column is a month and each row is a year) create a function YEARLY that prints the average rainfall for each year For example, if the information below were to be stored in a 5x12 matrix RAIN... J F M A M J J A S O N D 2003 1 2 1 2 1 2 1 2 1 2 1 2 2004 1 1...
Write a sequence of assembly language instructions to subtract each entry of an array A of...
Write a sequence of assembly language instructions to subtract each entry of an array A of five two’s complement 16-bit binary integers from the corresponding entry of an array B of five two’s complement 16-bit binary integers and construct a third array C of two’s complement 16-bit binary integers. i.e. C[i] = A[i] - B[i]. Use the following data for the arrays A and B. A: 10, -15, 20, 4, -5 B: 25, -5, -30, 6, 10 please answer in...
java You are creating an array where each element is an (age, name) pair representing guests...
java You are creating an array where each element is an (age, name) pair representing guests at a dinner Party. You have been asked to print each guest at the party in ascending order of their ages, but if more than one guests have the same age, only the one who appears first in the array should be printed. Example input: (23, Matt)(2000, jack)(50, Sal)(47, Mark)(23, will)(83200, Andrew)(23, Lee)(47, Andy)(47, Sam)(150, Dayton) Example output: (23, Matt) (47, Mark) (50, Sal...
Two data sets are given below, each representing times (in minutes) to complete a given task...
Two data sets are given below, each representing times (in minutes) to complete a given task for two different samples of students. Data Set A: 3.1, 4.7, 7.3, 8.1, 8.1, 8.7, 8.8, 9.1, 9.1, 9.2 Data Set B: 5.2, 5.6, 5.8, 6.9, 7.0, 7.7, 7.9, 8.5, 8.8, 12.5 Complete the back-to-back stem plot for data set B by adding the numbers that belong in each class below, using a class width of 1.  Do not enter spaces between the numbers. If...
1) Given an array queue implementation with one unused entry and frontIndex and backIndex references, match...
1) Given an array queue implementation with one unused entry and frontIndex and backIndex references, match the status of the queue with the following information about frontIndex and backIndex for a queue with capacity of 5: a.) frontIndex is 0 and backIndex is 4 b.) frontIndex is 1 and backIndex is 4 c.) frontIndex is 3 and backIndex is 0 empty queue one element in the queue three elements in the queue four elements in the queue five elements in...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
In C++, create a 3-by-3 array that is capable of storing integers and initialize each entry...
In C++, create a 3-by-3 array that is capable of storing integers and initialize each entry to 0. Ask the user to supply a row and a column and a value, then put the value into the array at that row and column. Print the array to the screen as a table with 3 rows and 3 columns, and ask the user for another row, column, and value. Repeat until user inputs values for all entries. Once finished, compute and...
Randomly generate 0, 1, or 2 in a 100 by 100 2d array. For each entry...
Randomly generate 0, 1, or 2 in a 100 by 100 2d array. For each entry in the 2d array, count the number of 0’s and 1’s in the surrounding entries. Save the result in two 2d arrays: one for the number of 0’s, one for the number of 1’s. For example: 0   0   1 2   1   0 0   0   0 The number of 0’s for a[0][0] is 1; the number of 1’s for a[0][0] is 1; The number of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT