Question

You are given an integer array in the console program which declares and initializes it as...

You are given an integer array in the console program which declares and initializes it as follows, for example:

int int_array[] = {1, -3, 23, 4, 9, 2, 29, -3, 0, 2, 48, 7, 6, -1};

In that array there are n elements, where 0 < n < INT_MAX. Your program is to find the largest sum of any consecutive elements in that array. In the above example, the largest sum is obtained by adding 23, 4, 9, 2, 29, -3, 0, 2, 48, 7, and 6 (sum = 127). The largest sum is defined to be greater than 0. Consecutive elements mean their array indices are adjacent to each other, namely:

int_array[2] = 23

int_array[3] = 4

int_array[4] = 9

int_array[5] = 2

int_array[6] = 29

int_array[7] = -3

int_array[8] = 0

int_array[9] = 2

int_array[10] = 48

int_array[11] = 7

int_array[12] = 6

Other continuous elements inside the array get you a sum that is lower than the above.

Restrictions and Guidelines :

1. You may use the following code snippet. You may freely change your int_array examples in your main() to test your code. This snippet is just an example.

2. Implement the getLargestConsecutiveSum() function by filling up the function block indicated by // YOUR IMPLEMENTATION GOES HERE.

5. The answer is 127 in the above example.

============CODE SNIPPET BEGINS HERE ================

#include<iostream>
using namespace std;
int getLargestConsecutiveSum(const int[], int);
int main() {
    int int_array[] = {1, -3, 23, 4, 9, 2, 29, -3, 0, 2, 48, 7, 6, -1};
    cout << "Largest Consecutive Sum: " << getLargestConsecutiveSum(int_array, sizeof(int_array)/sizeof(*int_array));
    return 0;
}
int getLargestConsecutiveSum(const int arr[], int length) {
    // YOUR IMPLEMENTATION GOES HERE!!!
}

============CODE SNIPPET ENDS HERE ================

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<iostream>

using namespace std;

int getLargestConsecutiveSum(const int[], int);

int main() {
    int int_array[] = {1, -3, 23, 4, 9, 2, 29, -3, 0, 2, 48, 7, 6, -1};
    cout << "Largest Consecutive Sum: " << getLargestConsecutiveSum(int_array, sizeof(int_array) / sizeof(*int_array));
    return 0;
}

int getLargestConsecutiveSum(const int arr[], int length) {
    int maxSum = arr[0];
    for (int i = 0; i < length; ++i) {
        int sum = 0;
        for (int j = i; j < length; ++j) {
            sum += arr[j];
            if (sum > maxSum)
                maxSum = sum;
        }
    }
    return maxSum;
}

Add a comment
Know the answer?
Add Answer to:
You are given an integer array in the console program which declares and initializes it as...
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
  • Given an array of integer, please complete a function multiply(). The function accepts the first memory...

    Given an array of integer, please complete a function multiply(). The function accepts the first memory address of an array and the size of an array as parameters and returns the multiplication of all the elements. In order to get a full score, the function must use a loop to iterate through each element of an array. Pseudo code example:   multiply([ 1, 2, 3], 3) → 6   multiply([1, 1, 4 ,2], 4) → 8   multiply([7, 0, 0, 7, 0, 7],...

  • C++ program Write a code segment to find the max element in each row and then...

    C++ program Write a code segment to find the max element in each row and then store the values to the "result" array. For example, { {2, 7, 9, 6, 4}, => 9 {6, 1, 8, 10, 4}, => 10 {4, 3, 7, 2, 9}, => 9 {9, 9, 0, 3, 1}, => 9 {8, 8, 7, 8, 9}, => 9 {1, 2, 1, 2, 3} } => 3 the result array has the values [9, 10, 9, 9, 9,...

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

  • Write a program in C++ language that finds the largest number in an array of positive...

    Write a program in C++ language that finds the largest number in an array of positive numbers using recursion. Your program should have a function called array_max that should pass in an int array, and an int for the size of the array, respectively, and should return an int value. As an example, given an array of size 10 with contents: {10, 9, 6, 1, 2, 4, 16, 8, 7, 5} The output should be: The largest number in the...

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

  • Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random...

    Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random numbers between 0 and 100. The array is sent to a function that finds the indices of all items > 50. A new array is created and the indices are stored inside it. The size of the new arrays MUST BE the same as the number of items > 50. The function returns the new array which is then printed out by main. Here...

  • c++, need help thank you Given an array of ints, return the sum of all elements...

    c++, need help thank you Given an array of ints, return the sum of all elements from the array that come before the first element that equals number 4 in the array. The array will contain at least one 4. Function prototype: int pre4int array ( ], int size); Hint: to find the array size, use: int size = sizeof(array) / sizeof( array[0]); Sample runs: int array1[ ] = {1, 2, 4, 1); pre4(array1, 4); // returns 3 int array2...

  • please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class...

    please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class Chapter9a_FillInTheCode { public static void main(String[] args) { String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"}, {"Detroit", "Newark", "Boston", "Seattle"}}; // ------> exercise 9a1 // this code prints the element at row at index 1 and column at index 2 // your code goes here System.out.println("Done exercise 9a1.\n"); // ------> exercise 9a2 // this code prints all the states in the...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

  • ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given....

    ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...

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