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...
Python. The array C has multibel peaks (+ , -). write a code that detect the...
Python. The array C has multibel peaks (+ , -). write a code that detect the vlaues of all those peaks. C = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0011 -0.0031 0.0011 0.0034 -0.0039 0.0004 0.0052 -0.0045 -0.0025 0.0067 -0.0038 -0.0034 0.0077 -0.0034 -0.0054 0.0076 -0.0011 -0.0063 0.0068 0.0004 -0.0084 0.0061 0.0037 -0.0086 0.0018 0.0063 -0.0045 -0.0069 0.0078 0.0027 -0.0132 0.0054 0.0133 -0.0131 -0.0060 0.0180 -0.0037 -0.0169 0.0140 0.0128 -0.0210...
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
Write in C Language Write all these code in one program and upload. Consider this array...
Write in C Language Write all these code in one program and upload. Consider this array char address1 [ ] = "12330 Washington Blvd, suite 300, Sacramento, CA 94560-2341" ; (NOTE: The constant string on the RHS gets copied into the array address1 on the LHS) 1. Write a piece of code to count only letters in the string address1 using the function isAlpha   2. Convert to all Upper Case Write a program to convert address1 to all uppercase letters...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT