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

List of all available Websheets


Viewing cpp/pointers/ret2 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
 
<p>Functions in C++ can only return 1 value.  What if we want to return 2 or more values?</p>
<p></p>
<p>Write a function that returns the sum and difference (a-b) of two integers.</p>
<p>At least one value will have to be passed by reference and then set by the function via a pointer. We usually
   calls these parameters (that are passed via pointer so they can be set by a function) <b>an output parameter</b>.</p>
<p>You may choose which value you return and which you pass by reference.</p>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/pointers/roll2 (author: redekopp@usc.edu)
Originally by Mark Redekopp (redekopp@usc.edu) and Dave Pritchard (daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <cstdlib>
using namespace std;
// computes and returns the sum and difference of a and b.
\[int ]\ sum_diff(int a, int b, \[int* d]\) 
{   
\[
   *d = a - b;
   return a+b;
   ]\
}
int main() {
   int x, y;  // two integers 
   int sum, diff;
   
   cin >> x >> y;
   \[
      sum = sum_diff(x, y, &diff);      
    ]\
   cout << sum << " " << diff << endl;
   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" : "2 3"},
   { "stdin" : "-1 4" }
]
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'.