Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/recursion/array_max_head by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Using head recursion (i.e. where you recurse first and do your work on the way back up)
create a method <tt>amax()</tt>
that finds the maximum element in an array of non-negative integers.  
<p>
<strong>Return -1 if the list is empty.</strong>
   <p>
Consider carefully what data needs to be communicated as you recurse down the array and then as you return back up the array.<br><strong>If you believe it necessary, define a helper function.</strong>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/recursion/list_max (author: daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <algorithm>
using namespace std;
// You can prototype a helper function here if you need it
\[
   
]\
   
// This is the primary max() function that the user should call
int amax(int* array, int len)
{
\[
   if(len == 0) {
      return -1;
   }
   else {
      int tempmax = amax(array+1, len-1);
      return max(tempmax, array[0]); // use the std::algorithm max() to find max of 2 numbers
   }
   
]\
}
// You can write your helper function here if you need it
\[
   
]\
int main() {
   int len;
   cin >> len;
   int* data = new int[len];
   for(int i=0; i < len; i++){
      cin >> data[i];  
   }
   cout << amax(data, len);
   return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {"stdin": "3 1 0 3"},
   {"stdin": "3 3 0 1"},
   {"stdin": "3 1 3 0"},
   {"stdin": "0 "},
   {"stdin": "5 9 4 4 0 19"},
   {"stdin": "5 2923 2 20 43 298"}
]
Forbidden substrings
json list of strings
e.g. ["for","while"]
 
["for", "while", "do"]
remove
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.