List of all available Websheets
Viewing cpp/pointers/divide 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>divide</tt> that is capable of performing integer or floating-point division. For example <pre> ./divide d 12.3 10.0 </pre> asks for division of doubles and prints <tt>1.23</tt>, while <pre> ./divide i 10 3 </pre> asks for integer division and should print <tt>3</tt>. <p> Print <tt>wrong number of arguments</tt> if the number of arguments is wrong. You don't need to do any further error checking for this exercise. |
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; int main(int argc, char* argv[]) { if (\[argc != 4]\) { cout << "wrong number of arguments" << endl; } else { // if first argument starts with 'i', divide integers if (argv[1][0] == \['i']\) { // parse arguments as integers \[int]\ x = atoi(argv[2]); \[ int y = atoi(argv[3]); ]\ // divide and print cout << x / \[y]\; } else { // parse as doubles, divide and print double x = \[atof(argv[2])]\; \[ double y = atof(argv[3]); cout << x / y; ]\ } } 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": ["d", "12.3", "10.0"]}, {"args": ["i", "10", "3"]}, {"args": ["d", "7", "2"]}, {"args": ["i", "7", "2"]}, {"args": ["d", "1.8", "1.2"]}, {"args": ["d", "45"]}, {"args": ["i", "-2", "-3", "-4"]} ] |
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'.