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

List of all available Websheets


Viewing cpp/arraypassing/search 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>Write a function that will take in an array of integers <tt><strong>nums</strong></tt> 
   along with the <tt><strong>size</tt></strong> of the array and a single integer <tt><strong>target</strong></tt> and searches the <tt>nums</tt>
   array to check if <tt>target</tt> exists in the array.</p>
<p>If <tt>target</tt> does exist in the array, return the <strong>index</strong> at which it is located.</p>
<p>If <tt>target</tt> does NOT exist in the array, return the <strong>-1</strong>.</p>
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/arraypassing/extra-credit (author: redekopp@usc.edu)
Copied from problem cpp/arraypassing/grade-histogram (author: redekopp@usc.edu)
Copied from problem cpp/arrays/reverse (author: redekopp@usc.edu)
Copied from problem cpp/cs103/hw-arrays/reverse_copy (author: redekopp@usc.edu)
remove
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
\[ REDACTED ]\ search(\[ REDACTED ]\, int size, int target)
{
   \[
REDACTED
]\
}
int main()
{
   int numbers[50];
   int n = 0, num, target, loc;
   
   // Get all the numbers
   cin >> num;
   while(num != -1){
      numbers[n++] = num;
      // Get the next number
      cin >> num;
   }
   
   
   // Call the search function
   target = -4;
   loc = search(\[ REDACTED ]\, n, target);
   if(loc != -1){
     cout << target << " exists at location " << loc << endl;  
   }
   else {
     cout << target << " does not exist in the array" << endl;  
   }
   
   target = 7;
   loc = search(\[ REDACTED ]\, n, target);
   if(loc != -1){
     cout << target << " exists at location " << loc << endl;  
   }
   else {
     cout << target << " does not exist in the array" << 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": "100 213 7 93 75 199 -400 -1"},
   {"stdin": "-4 0 0 0 0 0 7 -1"},
   {"stdin": "90 1 100 1 95 1 -4 0 -1"}
]


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'.