Question

An investor opens a new account and wants to invest in a number of assets. Each asset begins with a balance of O, and its val

In the first round, a contribution of 10 is made to investments 1 and 2. In the second round, a contribution of 5 is made to

Constraints • 3 sns 107 1sos 2 * 105 1 s left s right sn Os contribution s 10°

Language: java

1 0
Add a comment Improve this question Transcribed image text
Answer #1

Required Function's Code Screenshots:

long maxValue (int n, **rounds) int rounds_rows, int rounds_columns, int // Define the variable to store // the maximum amoun// Run the loop to add the // contribution to all the investments // between left and right, both inclusive. for(j=left; j<=r

Required Function’s Code:

long maxValue(int n, int rounds_rows, int rounds_columns, int **rounds)

{

    // Define the variable to store

    // the maximum amount invested.

    long max = 0;

    

    // Define an array of size n,

    // to store the n investments.

    long *investments = (long*)malloc(sizeof(long)*n);

    int i=0;

    

    // Initially set all

    // the investments to 0.

    for(i=0;i<n;i++)

    {

        investments[i] = 0;

    }

    i=0;

    // Run the loop to

    // perform the rounds.

    while(i<rounds_rows)

    {

        // Get the left value

        // of the current round.

        int left = rounds[i][0];

        

        // Get the right value

        // of the current round.

        int right = rounds[i][1];

        // Get the contribution

        // for the current round.

        int contribution = rounds[i][2];

        // Since the user uses 1-based

        // indexing, subtract 1 from left

        // and right as the program uses

        // 0-based indexing. That is, the

        // investments in the array start

        // from 0 and not 1.

        right = right - 1;

        int j=0;

        // Run the loop to add the

        // contribution to all the investments

        // between left and right, both inclusive.

        for(j=left; j<=right; j++)

        {

            investments[j] += contribution;

        }

        i++;

    }

    // Traverse the investments array

    // to find the maximum element.

    max = investments[0];

    for(i=1; i<n;i++)

    {

        if(investments[i]>max)

        {

            max = investments[i];

        }

    }

    // Return the

    // maximum investment.

    return max;

}

Below is the code to show the working of the function.

Code Screenshots:

#include <assert.h> #include <stdio.h> #include <stdlib.h> int rounds rows, int rounds_columns, int long maxValue (int n, **rright = right 1; int j=0; // Run the loop to add the // contribution to all the investments // between left and right, both ifor (i=0;i<rounds_rows;i++) { rounds [i] (int*) (malloc(sizeof (int) *rounds_columns)); int j=0; // Read the details // of ea

Sample Output:

5 3 3 1 2 10 2 4 5 3 5 12 The maximum amount invested is: 17

Code to Copy:

#include <assert.h>

#include <stdio.h>

#include <stdlib.h>

long maxValue(int n, int rounds_rows, int rounds_columns, int **rounds)

{

    // Define the variable to store

    // the maximum amount invested.

    long max = 0;

    

    // Define an array of size n,

    // to store the n investments.

    long *investments = (long*)malloc(sizeof(long)*n);

    int i=0;

    

    // Initially set all

    // the investments to 0.

    for(i=0;i<n;i++)

    {

        investments[i] = 0;

    }

    i=0;

    // Run the loop to

    // perform the rounds.

    while(i<rounds_rows)

    {

        // Get the left value

        // of the current round.

        int left = rounds[i][0];

        

        // Get the right value

        // of the current round.

        int right = rounds[i][1];

        // Get the contribution

        // for the current round.

        int contribution = rounds[i][2];

        // Since the user uses 1-based

        // indexing, subtract 1 from left

        // and right as the program uses

        // 0-based indexing. That is, the

        // investments in the array start

        // from 0 and not 1.

        right = right - 1;

        int j=0;

        // Run the loop to add the

        // contribution to all the investments

        // between left and right, both inclusive.

        for(j=left; j<=right; j++)

        {

            investments[j] += contribution;

        }

        i++;

    }

    // Traverse the investments array

    // to find the maximum element.

    max = investments[0];

    for(i=1; i<n;i++)

    {

        if(investments[i]>max)

        {

            max = investments[i];

        }

    }

    // Return the

    // maximum investment.

    return max;

}

int main(void) {

    

    // Declare the

    // required variables.

    int n;

    int rounds_rows;

    int rounds_columns;

    // Read the user input.

    scanf("%d",&n);

    scanf("%d", &rounds_rows);

    scanf("%d", &rounds_columns);

    // Define the array to store

    // the details of the rounds.

    int **rounds;

    rounds = (int**)(malloc(sizeof(int*)*rounds_rows));

    int i=0;

    for(i=0;i<rounds_rows;i++)

    {

        rounds[i] = (int*)(malloc(sizeof(int)*rounds_columns));

        int j=0;

        

        // Read the details

        // of each round.

        for(j=0;j<rounds_columns;j++)

        {

            scanf("%d", &rounds[i][j]);

        }

    }

    // Call the function and get

    // the maximum investment.

    long max = maxValue

    (n, rounds_rows,

    rounds_columns,rounds);

    

    // Display the

    // maximum investment.

    printf("The maximum amount"

    " invested is: %ld\n", max);

    

    // Return from the

    // main() function.

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Language: java An investor opens a new account and wants to invest in a number of...
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 help me fill out the blanks to the prewritten code for c++ 1. OBLJ UI...

    please help me fill out the blanks to the prewritten code for c++ 1. OBLJ UI SuuenLD, JUN, UML function find MultiMax that finds both the value ane handle ties, the find MultiMax function she are and assumed to be initialized alues should be as the returned values show 4. (30 pts) Use the space provided to write the function find Multima location of the maximum element of an array. To handle ties, the store the index locations in an...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • An investment company would like to know which stocks to invest in to achieve maximum return....

    An investment company would like to know which stocks to invest in to achieve maximum return. At the start of every day of the week (Monday through Friday) they decide how much to invest on that day, for example, X dollars. If in the next day they are able to match the initial investment (the X dollars) with another investment half that amount (X/2 dollars) they can expect a total return on the third day that doubles the initial amount...

  • An investor has $150,000 to invest in investments A and B. Investment A requires a $10,000...

    An investor has $150,000 to invest in investments A and B. Investment A requires a $10,000 minimum investment, pays a return of 12% and has a risk factor of .50. Investment B requires a $15,000 minimum investment, pays a return of 10% and has a risk factor of .20. The investor wants to maximize the return while minimizing the risk of the portfolio. The following Minimax formulation of the problem has been solved in Excel. 1Problem data 2 Expected return...

  • An investor has $20,000 to invest in a portfolio of three accounts. Account 1 offers a...

    An investor has $20,000 to invest in a portfolio of three accounts. Account 1 offers a return of 2% and at a low risk. Account 2 offers a return of 4% and has a medium risk. Account 3 offers a return of 5% but at a relatively high risk. He decided to invest no more than $3000 into account 3 and at least twice as much in Account 1 into the investment of Account 2. a) Formulate this as a...

  • a. Your firm wants to invest $5,000,000 in a new project. There are two projects available...

    a. Your firm wants to invest $5,000,000 in a new project. There are two projects available and investment can be made in only one of them. Cash flows are as follows. (Higher payoff in the last year is due to scrap value.) Year Investment A Investment B 0 -$5,000,000 -5,000,000 1 $1,500,000 $1,250,000 2 $1,500,000 $1,250,000 3 $1,500,000 $1,250,000 4 $1,500,000 $1,250,000 5 $1,500,000 $1,250,000 6 $1,500,000 $1,250,000 7 $2,000,000 $1,250,000 8 0 $1,600,000 State the problem, then valuate each...

  • JAVA Language public class ArraySkills { public static void main(String[] args) { // *********************** // For...

    JAVA Language public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // String[] myData; // 1. Instantiate the given array to hold 10 Strings. // 2. Add your name to the Array at index 0 and a friend's name to the Array at index 4 // 3. Move your...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not...

    Tree Plot Please write a Java program to print or plot a binary tree in a 2-dimensional character format. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree.    Your program must define 3 binary trees as follows. Each tree is defined in an integer 16 x 3 array. Programming Techniques: (1) The array for the binary tree can be integer data type with 16 rows by 3 columns. Please always make index...

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