List of all available Websheets
Viewing cpp/selection/count-odd 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 | This program reads in integers until the user enters 0. Count how many numbered entered are odd and output that total at the end of the program. |
Public permissions | |
Remarks Comments, history, license, etc. | Copied from problem cpp/selection/count-neg (author: redekopp@usc.edu) Copied from problem cpp/control/countodd (author: redekopp@usc.edu) Originally by Mark Redekopp (redekopp@usc.edu) and Dave Pritchard (daveagp@gmail.com) |
Engine | |
Template / Reference solution |
using namespace std; int main() { // You may declare avariables below or add any additional // code before the loop starts. \[ int num_odd = 0; int value = 1; ]\ // Use a loop and selection idiom below \[ while ]\ ( \[ value != 0 ]\ ) { cout << "Enter an integer or 0 to quit:" << endl; cin >> \[ value ]\; \[ if( value % 2 == 1 ){ num_odd++; } ]\ } cout << "You entered " << \[ num_odd ]\ << " odd numbers" << 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": "3 6 2 7 1 9 0"}, {"stdin": "0"}, {"stdin": "1 3 5 7 0"} ] |
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'.