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

List of all available Websheets


Viewing cpp/var-expr/char_arith 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
 
It is important to remember that the computer treats <tt>char</tt> variables as numbers until <br>
we actually try to print them.  In addition, it will convert <tt>char</tt> constants to<br>
their ASCII number.  So writing <tt>'a'</tt> is the same as writing the number <tt>97</tt>.
<p>
<strong>Thus we can perform arithmetic on those numbers or use comparison operators!</strong>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/var-expr/hello (author: daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
  // Make the words "true" and "false" print for Boolean expressions
  cout << boolalpha;
   
  char c = 97;
  cout << "97 as a char is really " << c << endl;
  
  // try to print out the letter 'b' by doing arithmetic
  c = \[ c + 1 ]\;
  cout << "This should print 'b' " << c << endl;
   
  // Make the new value of 'c' equal the old value plus 25
  c = 'a' + 25;
  
  // *** Can you guess what character will print ****
  cout << "Can you guess what will print...answer: " << c << endl;
  
  // Make another char variable
  char d = 'B';
  // Let's try to check if the value of a char variable is between a-z
  cout << (d >= 'a' && d <= 'z') << endl;
   
   
  // One other difference is how digits are stored.  There is 
  // a difference between 1 and '1'.  1 is the decimal number 1.
  // '1' is the ASCII code (decimal 49) that causes '1' to display
  // on the screen.
  char e = \[ '1' ]\;  // play with this value
  cout << "Let's try to display the digit 1:  " << e << endl;
   
   
  // Let's try to print multiple digits.  What's wrong with this?
  \[
  cout << "10" << endl;
  \show:
  cout << '10' << 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 [{}]
 
[
   {}
]
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'.