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

List of all available Websheets


Viewing cpp/recursion/array_max_tail_void 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 tail recursion (i.e. where you do some work on the way down the recursion and then just return the answer 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>
<strong>Assume you MUST define a helper function but the helper function MUST return void.  Consider how a reference variable can be used.</strong>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/recursion/array_max_tail (author: redekopp@usc.edu)
Copied from problem cpp/recursion/array_max_head (author: redekopp@usc.edu)
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
void amax_helper(int* array, int len, \[ int& mymax ]\ );
   
// This is the primary max() function that the user should call
int amax(int* array, int len)
{
\[
   int mymax = -1;
   amax_helper(array, len, mymax);
   return mymax;
   
]\
}
// You can write your helper function here if you need it
void amax_helper(int* array, int len, \[ int& mymax ]\ )
{
\[
   if(len == 0)
     return;
   else {
      mymax = max(mymax, array[0]);
      amax_helper(array+1, len-1, mymax );  
   }
]\
}
   
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'.