2 + 3. Anatomy of a program Flashcards
when we run a program we are running
a) an object file
b) a machine language file
-computers understand machine language, and object files are in machine language
object file
file containing object code
use of xcode
to type in + edit your program files
terminal utility
gives UNIX interface, used to enter UNIX commands to iMac
terminal commands
cd: change directory
ls: show programs in folder
make: compiles program
when compiling program
don’t type .cpp, just write the file name
troubleshooting: ‘program’ is up to date
probably forgot to save edited program
troubleshooting: no rule to make target ‘program’
mistyped filename or file not in current directory
troubleshooting: use of undeclared identifier
syntax error, didnt declare a variable
Use of // or /* */
a comment (ignored)
include
allows program to use built-in facilities for input and output (IO)
int main () {}
a function that returns an integer (int), function is called main
return 0
main returns the integer 0
{ }
used to define functions/processes
variables
places which store values
valid variable names
start w/ letter or underscore
rest is letters, digits, or underscore
notes on using variables
- case sensitive eg. rate vs Rate vs RATE
- don’t use reserved keywords eg. int, for, while
data type
what kind of thing you want to store in a variable
Data types
- int
- double
- bool
- char
- string
int
integers
double
decimal number
bool
true/false values
char
used to hold a letter/digit or other symbol eg. “a” “&” or “ “ (a space)
string
for sequences of characters eg “hello”
note on double: float
can be used for smaller less precise numbers
note on string
include
data type not built in, must include string library at top
variable declaration
variables + data types must be declared before use
giving variables values
a) reading eg. cin»_space; x;
b) assignment eg. sum = 0; name = “Mary”;
always give variables an initial value before use
=
the assignment operator
iostream
contains definitions for cout and cin
cout and cin arrow directions
cout «
cin»_space;
/
divides
eg. 10/3 = 3 (for int)
%
remainder
eg. 10%3 is 1
integer division
only if both operands are integers that division is integer division
cmath library common commands
cos/sin/tan exp log pow sqrt fabs
assigning int to a decimal value
by assigning a decimal value to an int variable, you effectively drop the decimals
eg. int i; double x = 3.1415; i = x; i will equal 3