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
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...
a) Write C code initialize an array of ints to the last four digits of your...
a) Write C code initialize an array of ints to the last four digits of your phone number. Use a loop to add the digits. Calculate and display the average of the digits. b) Write C code using a struct to hold student information: integer id integer number of hours taken integer number of hours passed double gpa
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT