Question

In: Computer Science

Write a code using c# Maximum Sub Array.

Write a code using c# Maximum Sub Array.

Solutions

Expert Solution

CODE

using System;

class Exercise

{

  static int maxSubArraySum(int []a)

  {

    int size = a.Length;

    int max_so_far = int.MinValue,

      max_ending_here = 0;

    for (int i = 0; i < size; i++)

    {

      max_ending_here = max_ending_here + a[i];

      

      if (max_so_far < max_ending_here)

        max_so_far = max_ending_here;

      

      if (max_ending_here < 0)

        max_ending_here = 0;

    }

    

    return max_so_far;

  }

  public static void Main ()

  {

    int [] a = {4, -1, 8, 3, 1, -9, 10, 11, 12, 1, -8};

    Console.Write("Maximum contiguous sum is " + maxSubArraySum(a));

  }

}


Related Solutions

Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n); 2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the...
How do I write a C# and a C++ code for creating a character array containing...
How do I write a C# and a C++ code for creating a character array containing the characters 'p', 'i', 'n','e','P','I','N','E' only and then using these lower and capital case letter character generate all possible combinations like PInE or PinE or PIne or PINE or piNE etc. and only in this order so if this order is created eg. NeIP or EnPi or NeIP or IPnE and on. You can generate all the combinations randomly by creating the word pine...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
Write a C function that finds and displays the maximum value ina two-dimensional array of...
Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
I need to write a c/c++ code to find the maximum sum in those possible combinations...
I need to write a c/c++ code to find the maximum sum in those possible combinations of arrays. There are N arrays with S elements. ex2: 4 arrays with 3 elements.We want to find the greater number in the same column of two arrays(may possible be three or four...arrays), and sum those greater number to find the greatest sum in all of the combinations in those 4 arrays, like: A: [50, 60, 70] B: [80, 40, 20] C: [30, 100,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT