-----BASIC C++ PROGRAM TEMPLATE----- #include using namespace std; int main (int argc, char **argv) { /* Variable Declaration */ /* Instructions */ return 0; } ------ COMMENTS --------------- /* Put explanatory messages for yourself and other programmers (not the computer) in comments like this one */ //single line comments look like this ------EDIT SAVE COMPILE RUN ------------- or EDIT, SAVE, COMPILE, FIX COMPILER ERRORS, RUN, CHECK OUTPUT 1) Edit your code with an editor like emacs or nano (learning to navigate in at least one editor on polaris is going to be important for this class) E.g. emacs -nw my program.cpp OR nano myprogram.cpp Remember to save you changes! 2) Compile your code with a compiler (translates human readable source code into executable format the computer needs to actually run the program) E.g. g++ myprogram.cpp Fix any compiler errors! Go to first problem by it's line number and fix. 3) Run your program E.g. ./a.out Check that the output of the program is what you expect -----SIMPLE TYPES----- TYPE EXAMPLES int 0,1, -1, 2, -2,... unsigned int 0, 1, 2,3, 4, ... float 2.89, 67777799.0, -1000000000.0 double char 'a', 'Z', '\n' bool true, false string "August", "This sentence is a string" possible numeric values are not infinite like in math - e.g. there is a fixed amount of space to store an int and so unlike in real like there is a maximum integer unsigned int vs int - use space to store all positives vs half positive and half negative unsigned and int vs float float can store bigger and smaller numbers but to do so can give up precision float vs double - more space used for both bigger numbers and more precise numbers -----SAMPLE VARIABLE DECLARATIONS------ int month; char answer; bool isReady = false; char initial = 'A'; float wage = 8.50; string name = "John Smith"; unsigned int year = 1996; Remember to use descriptive variable names!! -----INPUT/OUTPUT---- cin >> variable; cout << "Hi\n"; cout << "Hi " << name << "\n"; cout << "Hi " << name << endl; ----- ARITHMETIC OPERATORS------- + add * multiple - subtract / divide (remember for ints gives just whole number part of answer, cuts off any decimal portion) % modulus (gives integer remainder after division) remember order of operations like in math can use parentheses to force/clarify the order you want ----- ASSIGNMENT STATEMENTS------- Example assignment statements hoursPerMonth = daysPerMonth*24; age = age+1; index = index -1; total_price = num_item1 * item1_price + num_item2 *item2_price; ------- BOOLEAN EXPRESSIONS -------- boolean expressions evaluate to true or false Example boolean expressions bool isTransferStudent; (isTransferStudent) (month > 2) (answer == 'Y') ------ COMPARE OPERATORS ----------- == equal (Remember one = sets something equal. Two == tests if two things are equal) (Also remember that it is dangerous to test doubles with == they may be very very close but not identical in precision) != not equal > greater than >= greater than or equal < less than <= less than or equal ------ BOOLEAN OPERATORS ---------- && AND || OR Combine boolean expressions to make a larger boolean expression ((month >=1) && (month <= 12)) ((answer == 'Y') || (answer == 'y')) ------ IF-ELSE STATEMENTS --------------- if (boolean expression) { true_statement1; true_statement2; } else { false_statement1; false_statment2; } Notes: Always using {} in your if statments is safer else clause is optional ------ IF-ELSE IF-ELSE STATEMENTS --------------- unsigned y; if (y==1) { 1 } else if (y< 10) { 0, 2-9 } else if (y < 20){ 10-19 } else { 20+ } Can have as many else if clauses as you like Evaluated one at a time and take first option that matches! Don't take multiple branches even if match multiple Always have exactly 1 if, can have 0-N else if, can have 0 or 1 elses