List of all available Websheets
Viewing cpp/pointers/cmdargs_sum by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
Property | Value |
---|---|
Description html markup shown to student | Write a program <tt>cmdargs_sum</tt> that adds up all of its command-line arguments. <!--We want to write a program that adds a series of integers provided by the user as command line arguments. We have declared a <tt>sum</tt> variable for you. You need to figure out the loop counter initialization and condition as well as how to convert each command line argument to an integer before it is added. Also, if there are no arguments your program should not crash but just output th sum of 0.--> For example, if you run <pre>./cmdargs_sum 2 5 19 3</pre> The output should be: <pre>29</pre> Assume the inputs are all integers. <!--If some one runs the program as: <tt>./cmdargs1</tt> The output should be: <tt>0</tt>--> |
Public permissions | |
Remarks Comments, history, license, etc. | Originally by Mark Redekopp (redekopp@usc.edu) and Dave Pritchard (daveagp@gmail.com) |
Engine | |
Template / Reference solution |
using namespace std; // main should accept command-line arguments int main(\[int argc, char* argv[]]\) { int sum = 0; int i; // convert and add up each argument for (\[i=1]\; \[i < argc]\; i++) { sum += \[atoi(argv[i])]\; } cout << "Sum is " << sum << 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 [{}] | [ {"args": ["2", "5", "19", "3"]}, {}, {"args": ["-2"]} ] |
Solution visibility |
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'.