Extra Notes Flashcards
return vs void functions
use & sparingly, only use when you really need them
if you only need to return 1 value, just use non-void function
char
for single digit inputs
eg. user inputs an answer “b” for an exam
string
for multiple digit/char inputs
eg. user inputs their first name
”” and ‘’
can use either single or double quotations for characters and strings
partially filled arrays: prompting user
useful to write:
cout «_space;“please write up to “ «_space;DECLARED_SIZE «_space;” numbers terminated with “ «_space;SENTINEL;
eg of program to square a sequence of numbers
1) initialise
2) cout request for input
3) readtosentinel() to read in inputs
4) use square_array() function which can use square() function
5) use print_array() function to print results
functions and expressions
can put expressions into functions
eg. approx = newtonsqrt(n, n/2);
efficiency using !
instead of:
while (function(x) = false)
you can write:
while (!(function(x)))
if function is a bool
efficiency with if statements and bool returns
if (n%f == 0)
return true;
else
return false
can be shortened to:
return(n%f == 0);
using constants like Pi
declare PI at start, ie.
const int PI/const double Pi = …..
then use as such:
area = d/2 * d/2 * PI;