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

List of all available Websheets


Viewing cpp/passing/swap_elements by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Define a function <tt>void swap_elements(int arr[], int i, int j)</tt> 
that takes an array <tt>arr</tt>,
and swaps the elements at positions <tt>i</tt> and <tt>j</tt>.
<p>You should <b>not</b> use cin or cout, that part is done for you
in order to facilitate testing.
Public permissions remove
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
void swap_elements(int arr[], int i, int j) {
\[
   int tmp = arr[i];
   arr[i] = arr[j];
   arr[j] = tmp;
]\
}
int main() {
   // read n, i, j, then n inputs
   int the_inputs[100];
   int n, i, j;
   cin >> n >> i >> j;
   for (int k=0; k<n; k++) cin >> the_inputs[k];
   cout << "Before calling your function, the array contains: ";
   for (int k=0; k<n; k++) cout << the_inputs[k] << " ";
   cout << endl;
   // call YOUR function
   swap_elements(the_inputs, i, j);
   // is the output correct?
   cout << "After calling your function, the array contains: ";
   for (int k=0; k<n; k++) cout << the_inputs[k] << " ";
   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 0 1\n2014 9 17"},
   {"stdin": "8 5 7\n99 11 17 71 39 0 93 38"},
   {"stdin": "8 3 3\n99 11 17 71 39 0 93 38"}
]
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'.