CS1A EXAM 2 C++
WHAT IS THE NOT OPERATOR?
!
WRITE CODE IN YOUR HEAD FOR A SWITCH STATEMENT THAT CHANGES THE INPUT CARDINAL DIRECTIONS ('W' for West, 'E' for East, and so on)
#include <iomanip> #include <iostream> using namespace std; int main() { char cardinalDirections[3]; cout << "please enter cardinal directions N, E, W, S of where you live in relation to city hall:" cin >> cardinalDirections; cin.ignore(1000, '\n') switch (cardinalDirections); case 'w' : case 'W' : cout << "West"; break; case 'e' : case 'E' : cout << "East"; break; etc. and so on default: "invalid input, please try again"; break;
COUT
-how we communicate with user -COUT - inserts data to output stream using << insertion operators -can output a string literal using " " or variables - pre-defined variable in C++
WHAT IS THE DIFFERENCE BETWEEN A COMPILE TIME ERROR AND A RUN TIME ERROR?
-syntax errors are compile errors we cannot build or compile the code -logic errors are run time errors when our code compiles but then produces incorrect output
WHAT APPROACHES SHOULD YOU USE TO FIX A RUN-TIME ERROR?
-try to isolate problem - commenting out to pinpoint section of faulty code -coding incrementally/ a little at a time will help reduce run time errors -
cin.getline(c-stringName, stringwidth);
-used to extract strings including whitespace as a character
WHY DO WE HAVE STYLE?
-Readability -Modifiablity -Reusability -easier to debug
using namespace std;
-Tells the compiler that we will be using all the standard C++ functions -Functions are small code segments that we use to build our program
WHAT IS A TEST PLAN?
-a formalized plan including a table of inputs and expected outputs. -it should cover both white box and black box testing -include all category of values - expected values - unexpected values - boundary values
SELECTION
-based on some condition, either one part of the condition is executed or another part -program chooses which part to execute based on condition -conditions are boolean expressions
endl
-best by itself or used with a variable or constant and setw()
CIN
-extracts information from the input buffer using extraction operator >> -can only have variables on right side of cin statement
WHAT APPROACH SHOULD YOU USE TO FIX A COMPILE-TIME ERROR?
-fix from top to bottom -look at the line above and below error message -try to think about what the message is telling you
WHAT IS THE AND OPERATOR?
&&
WHAT IS THE DIFFERENCE BETWEEN 'A' & "A" IN C++?
'A' represents a single character "A" represents a c=string of 2 characters ('A' & '\0') null terminator
WHAT IS THE ORDER OF PRECEDENCE FOR ARITHMETIC AND BOOLEAN OPERATORS FROM HIGHEST TO LOWEST?
() ++ -- ! * / % + - < <= > >= == != && || =
DO WHILE
-POST-TEST event based loop - CONDITION TESTED AT BOTTOM of loop -LCV can initialized inside DO loop, changed inside the loop, and checked at end condition -checks instructions if true it runs again -part of the program is executed at least one time and repeats until some condition is false - runs at least one time before condition is checked -
WHEN WOULD YOU USE A WHILE LOOP?
- As a counter - count number of inputs - Running totals - sum a number of inputs - When you don' know how many times you need to run that part of the program (event based)
AVOIDING RUNTIME ERRORS
- Code incrementally little at a time and check for errors - check dependent variables - check lcv in loops and each variable iteration - use cin.ignore(1000, '\n') to hold the cursor so you can track code through each iteration
WHILE LOOP
- PRE-TEST event based loop - LCV is initialized before loop, checked in while statement compares to sentinal value, and changed just before the end of the loop. LCV must be updated within the loop - choose if you don't know how many times to run the loop -if condition evaluates to true is enters loop if FALSE IT BYPASSES the loop
FOR LOOP
- PRE-TEST loop - LCV is initialized, checked and changed within for statement - choose this if you know how many times you need something to loop
FIXING COMPILE TIME ERRORS
- Read the message and think about what it says - Look at line above and below error message - Go to project an clean project - Possible def use error, a defined value not used or is missing - close previous projects - use proper style - name variables with names that make sense
WHEN WOULD YOU USE A DO WHILE?
- Run when we don't know how many times it should run - Event-controlled - Similar to while - when you need to error check a user's inputs - when we know we have to run the code at least 1 time
WHAT ARE THE 3 CONTROL LOGIC STRUCTURES?
- Sequence - Selection - Repetition
HOW DO WE DEFINE/ASSIGN A VALUE FOR A VARIABLE? (get a value into a variable?)
- cin >> variableValuesAssignedUponInput; - can also be defined in an assignment statement ex : variableValue = variableValue + 1;
cin.ignore(1000, '\n');
- cleans input buffer - use after a cin.get(variable); every time
SHORT-CIRCUIT EVALUATION
- evaluates logical expressions left to right - when using AND && operator evaluation STOPS as soon as FALSE condition is found - when using OR || operator evaluation STOPS as soon as TRUE condition is found
NAME THE DATA TYPES
- int - char - bool - float - long - short - double - longlong
WHAT CAN APPEAR ON RIGHT SIDE OF COUT << STATEMENTS?
- literals - variables - constants - expressions anything really
FIXING RUNTIME ERRORS
- localize which part of code is causing issues - isolate by commenting out areas - insert cerr statements and output variables to make sure they have expected values - Desk Check code!
WHAT ARE 3 TYPES OF SELECTION STATEMENTS?
- one way - two way - multi way
cin.width(5);
- used like setw but with cin - will limit the amount of characters to be input - not recommended because it will leave whatever the user input that was over the size alotted by the cin.width and leave it in input buffer.
cin.get(variable);
- used to get a single character - it can be any single character even whitespace - if used with variable it places value in variable - if used without variable the character is discarded - need to use cin.ignore(1000, '\n') to flush the input buffer after -
WHICH IDENTIFIERS CAN APPEAR ON RIGHT SIDE OF CIN >> STATEMENTS?
- variables
DESCRIBE THE BASIC STRUCTURE OF A PROGRAM
-DIRECTIVES i.e., Pre-Processor directives/ information the program needs (a list of all necessary header files used in the program # include <iostream> , #include <iomanip>, #include <cstring>, using namespace std;) -HEADING i.e, int main() -INSIDE THE int main() FUNCTION { named constant declaration variable declaration executable statements return 0; }
int main () { body of function (i.e. program statements) return 0; }
-Program execution begins with this function -All C++ programs must have this function -MUST BE an int -Must start with { and end with } -Must have return 0; as the last statement
WHAT ARE THE STEPS INVOLVED IN DEVELOPING AN ALGORITHM THAT INCLUDES LOOPS
1 - Define input/output 2 - processing 3 - determine if a loop 4 - while : LCV - sentinal value - boolean expression 5 -for : Determine how many times to run
NEGATE THE FOLLOWING BOOLEAN OPERATORS 1) !( this || that) 2) !(this && that)
1) !this && ! that 2) !this || !that
WHAT STEPS ARE INVOLVED IN DEVELOPING AN ALGORITHM THAT INCLUDES A LOOP?
1) Define Inputs/Outputs 2)Processing 3)determine what loop if there is one 4) while : LCV Sentinal value boolean expression 5)For: determine how many times to run
WHAT DO THESE EVALUATE TO? 1) T && T 2) T && F 3) F && T 4) F && F
1) T 2) F 3) F 4) F
WHAT DO THESE EVALUTATE TO? 1) T || T 2) T || F 3) F || T 4) F || F
1) T 2) T 3) T 4) F
NEGATE THE FOLLOWING RELATIONAL OPERATORS 1) !(a == b) 2) !(a != b) 3) !(a >= b) 4) !(a > b) 5) !(a <= b) 6) !(a < b)
1) a !=b 2) a == b 3) a < b 4) a <= b 5)a > b 6)a >= b
THERE ARE 3 DIFFERENCES IN HOW WE DECLARE VARIABLES VS. CONSTANTS, WHAT ARE THEY?
1) constants are assigned a value when they are declared 2)have const in front of data type 3) CAPITALS Variables - lower cased for single word - must be camelCased for 2+ words - can contain numbers CONSTANTS - UPPERCASED - UNDER_SCORE_SPACING - can contain numbers
WHAT ARE 2 THINGS THE COMPILER NEEDS TO KNOW WHEN WE DECLARE AN IDENTIFIER? - how do we provide this information to the compiler?
1) the type of data that will be stored 2) how much memory is needed to store data -info is provided to compiler by the data type i.e, char, float, int, bool, double, short, long, longlong etc.
NAME 2 TYPES OF IDENTIFIERS
1) variables 2) CONSTANTS
WHAT ARE THE 4 QUESTIONS WHEN EVALUATING AN ALGORITHM?
1)Does the algorithm solve the stated problem? 2)Is the algorithm well-defined? 3)Does the algorithm produce an output? 4)Is it efficient
INDENTIFY THE 6 PARTS OF DEVELOPING AN ALGORITHM
1)Identify the input 2)identify the output 3)identify the process 4)develop the HIPO chart 5)develop the pseudocode and flowchart 6)test the algorithm with a desk check
FOR ANY LOOP WHAT 3 THINGS MUST WE DO?
1)Initialize LCV 2)Check LCV in some conditional statemment 3)Change LCV
NAME 4 COMMON LOOP ERRORS
1)Not initializing the LCV 2)Not updating the LCV within a WHILE or DO WHILE loop 3) Improperly written loop can be infinite - make sue that the condition that ends the loop can and will happen 4) Off by 1 errors are common in FOR loops check the LCV
WHEN WOULD WE DECLARE AN IDENTIFIER A CONSTANT?
1)When code might be modified in future 2)When same # is used more than 1 time throughout the code. 3)Readability 4)When you need a numerical literal
WHAT 3 STEPS NEED TO HAVE FOR EVERY LOOP?
1- initialize 2 - check 3- change
WHERE DO INITIALIZATION, CHECK AND CHANGE HAPPEN ON A FOR LOOP?
all within the for statement
WHAT SHOULD THE DATA TABLE TELL THE READER?
1.)What the variables are used for 2.)describing them
CONSTANTS
Contain data values that can't be changed during program execution - can be used/retrieved - can't be modified during program execution
RELATIONAL OPERATORS
< less than > greater than == equal to != not equal >= less than or equal to <= greater than or equal to
EXTRACTION OPERATOR
>> - used with cin - ignores leading whitespace - reads data until it reaches white space - Everything else goes into the input buffer - starts extracting once it hits a non-whitespace character - ignores whitespace character - the \n newline character stays in input buffer -next time we extract it will pull form input buffer first -it is important to clear the input buffer
WHAT IS THE DIFFERENCE BETWEEN 'A' and A in C++
A without the quotes is treated as an identifier by the system and since it is uppercase would be a CONSTANT
VARIABLES
Contain data values that may change during program execution - can be used/retrieved - can be modified during program execution
EACH INPUT BLOCK SHOWN ON A FLOWCHART REQUIRES THESE TWO STATEMENTS TO PROMPT THE USER AND TO INPUT VALUE INTO SPECIFIED MEMORY LOCATION (VARIABLE)
Cout for the prompt Cin for the input
WHAT OPERATORS WOULD WE USE TO CHECK TWO OR MORE CONDITIONS IN ONE STATEMENT?
Boolean Operators
WHEN IS A VALUE PLACED IN A CONSTANT
Compile time
WHEN IS THE AMOUNT OF MEMORY THAT WILL BE ALLOCATED FOR A CONSTANT DETERMINED?
Compile time
WHEN IS THE AMOUNT OF MEMORY THAT WILL BE ALLOCATED FOR A VARIABLE DETERMINED?
Compile time
WHAT DOES return 0; DO?
This returns the value 0 to the system so it knows the program completed/terminated properly
BASIC PSEUDOCODE DO WHILE LOOP
DO INPUT IF(condition) OUTPUT END IF WHILE(condition);
WHICH LOOP IF YOU WANT THE LOOP TO BE CONTROLLED AS EVENT BASED RATHER THAN COUNTER AND THE BODY MUST BE EXECUTED AT LEAST ONE TIME?
DO WHILE
WRITE A CODE SEGMENT THAT VALIDATES THAT A USER HAS ENTERED A VALUE BETWEEN 1 & 5
DO WHILE
WHEN IS IT OK TO PUT COMMENTS TO RIGHT OF OUR CODE?
Data Table Close brackets and DO while loops
T/F C++ IS AN EXAMPLE OF AN INTERPRETED LANGUAGE
FALSE
T/F IF THEN STATEMENTS CAN HAVE THE INSTRUCTIONS ON THE FALSE SIDE?
FALSE
T/F YOU CAN USE % WITH A FLOAT IN TYPE COERCION
FALSE
WHICH LOOP(S) ARE COUNTER BASED?
FOR
WRITE A CODE SEGMENT THAT SUM 6 NUMBERS - WHAT LOOP SHOULD YOU USE?
FOR
BASIC PSEUDOCODE FOR LOOP
FOR INPUT PROCESSING END FOR
WHICH LOOP(S) ARE PRE-TEST LOOPS?
FOR WHILE
LIST THE 3 LOOP STRUCTURES
FOR WHILE DO WHILE
WHICH LOOP IF YOU WANT THE LOOP BODY TO EXECUTE A SPECIFIC NUMBER OF TIMES?
FOR LOOP
T/F IT IS OK TO INITIALIZE VARIABLES IN THE DECLARATION SECTION? (when do we initialize?)
False (before defining a value for it, just before their use in the program)
T/F STYLE REQUIREMENTS HAVE NOTHING TO DO WITH DEBUGGING
False - readable code is easier to debug
T/F YOU SHOULD ONLY TEST YOUR CODE WITH THE VALUES PROVIDED
False - this may not cover all scenarios
WHAT DIAGRAM HAVE WE USED THAT DEPICTS THE FLOW OF AN ALGORITHM USING SPECIFIC SYMBOLS/SHAPES TO INDICATE VARIOUS PROGRAMMING TECHNIQUES?
Flowchart
WHAT DIAGRAM HAVE WE USED THAT REPRESENTS THE TOP DOWN DESIGN OF THE BASIC PROGRAM MODULES AND HOW THEY ARE RELATED?
HIPO chart
WHICH STATEMENT(S) SHOULD YOU USE FOR A TWO-WAY STATEMENT?
IF-ELSE CONDITIONAL STATEMENTS
WHICH STATEMENT(S) WOULD YOU USE WHEN YOU HAVE ONLY A FEW COMPARISONS OR YOU NEED TO CHECK DIFFERENT VARIABLES?
IF-Then-Else-IF
BASIC PSEUDOCODE WHILE LOOP
INPUT WHILE (condition) PROCESSING INPUT END WHILE
WHAT DO THE THREE MODULES IN THE 2nd LAYER OF A STRUCTURE CHART REPRESENT?
INPUT, CALCULATE, OUTPUT
WHAT DO WE USE IN PROGRAMMING LANGUAGES TO TELL THE COMPUTER TO HOLD A SPACE IN MEMORY TO STORE DATA WE WANT TO REUSE?
Identifiers
WHERE DO INITIALIZATION CHECK AND CHANGE HAPPEN ON A WHILE LOOP?
Initialized - before entering the loop Checked - WITHIN the while statement compared to sentinal value Changed - at the bottom jsut before end of the loop LCV is modified dynamically within the loop
WHAT CONTROLS WHEN A LOOP WILL EXECUTE AND WHEN IT WILL EXIT?
Loop Control Variable
WHAT IS AN LCV?
Loop Control Variable
LCV
Loop Control Variable - all loops controlled by a condition -a value is compared against a variable -the value is called the sentinel value for any loop we must INITIALIZE the lcv CHECK the lcv CHANGE
IF WE DO NOT INITIALIZE AN ACCUMULATOR TO 0 WILL THIS CAUSE A COMPILE ERROR?
NO a run time error
CAN A SWITCH STATEMENT REPLACE ANY IF-THEN-ELSE-IF STRUCTURE?
No
WHERE IN THE C++ PROGRAM DO WE PUT THE PRE-PROCESSOR DIRECTIVES?
Top
REPETITION
Part of the code is executed over and over - this can either be a set number of times or until a condition (using boolean expressions) is met
WHICH CONTROL LOGIC STRUCTURE DO WE USE WHEN WE NEED TO CONTINUE A SET OF INSTRUCTIONS WHILE A BOOLEAN EXPRESSION EVALUATES TO TRUE?
Repetition
WHEN IS A VALUE PLACED/STORED IN A VARIABLE?
Run time
WHY DO WE HAVE IDENTIFIERS?
SO WE CAN: - retrieve data - reuse data - modify data
WHICH CONTROL LOGIC STRUCTURE DO WE USE WHEN WE NEED TO EXECUTE A SET OF INSTRUCTIONS WHEN A BOOLEAN EXPRESSION EVALUATES TO TRUE AND BYPASS THEM WHEN IT EVALUATES TO FALSE?
Selection
WHICH CONTROL LOGIC STRUCTURES ARE BASED ON BOOLEAN EXPRESSIONS?
Selection & Repetition
WHICH SELECTION STATEMENT IS BEST USED IF THERE ARE MANY UNIQUE CONDITIONS BASED OFF THE SAME VARIABLE?
Switch
WHICH STATEMENT WOULD YOU USE IF YOU WANT TO MAKE SEVERAL COMPARISONS USING THE SAME VARIABLE?
Switch
T/F C++ IS AN EXAMPLE OF A HIGH LEVEL LANGUAGE
TRUE
T/F FOR WHILE & DO WHILE LOOPS YOU NEED TO DETERMINE WHEN YOU WANT THE LOOP TO EXECUTE AND WHEN YOU WANT IT TO STOP
TRUE
T/F IT IS IMPORTANT WITH WHILE AND DO WHILE LOOPS TO CHOOSE THE CORRECT LCV
TRUE
T/F THE STATEMENTS WITHIN ANY SELECTION STRUCTURE SHOULD BE UNIQUE TO THAT CONDITION
TRUE
T/F YOU CAN ONLY USE % WITH INT IN TYPE COERCION
TRUE
WHAT DATA TYPE WOULD WE USE TO STORE AN ACCUMULATOR?
Technically depends on what you're accumulating could be int, float, or double
PRE-PROCESSOR DIRECTIVES
Tells the pre-processor what we will be using #include <iostream> #include <iomanip> #include <cstring> using namespace std;
WHAT IS THE DIFFERENCE BETWEEN TESTING AND DEBUGGING?
Testing finds errors Debugging localizes and repairs them
WHAT ARE YOU DETERMINING WHEN YOU ARE DETERMINING WHEN YOU WANT A WHILE AND DO WHILE TO EXECUTE AND WHEN YOU WANT THEM TO STOP
The LCV
GIVE AN EXAMPLE OF CODE OF TYPE CASETING
age1 = 2; age2 = 3; ageCount = 4 aveAge = float(age1 + age2) / ageCount; aveAge = float((age1) + age2)/ ageCount; aveAge = (age1 + age2) / float (ageCount);
WHICH LOOP IF YOU WANT THE LOOP TO BE CONTROLLED AS EVENT RATHER THAN A COUNTER AND THE BODY MAY OR MAY NOT EXECUTE? OR IF YOU DONT KNOW HOW MANY TIMES THE LOOP WILL RUN?
WHILE
WRITE A CODE SEGMENT THAT SUMS A GROUP OF POSITIVE INTEGERS - WHAT LOOP SHOULD YOU USE?
WHILE
WHEN WOULD YOU USE A FOR LOOP?
When a part of the program is needed to run a set number of times
FLOATING POINT EXPRESSIONS
When all the operands are floats and the result is placed in a float or output
INTEGER EXPRESSIONS
When all the operands in an expression are integers and the result is placed in an integer or output
TYPE COERCION
When the data type of a value is changed implicitly through mixed-mode arithmetic
DEF-USE PAIR
Whenever we define a value for a variable we need to use that value somewhere in our code
NAME 2 EVENT CONTROLLED LOOPS
While DO While
CAN AN IF-THEN-ELSE (OR ELSE-IF) STRUCTURE REPLACE ANY SWITCH STATEMENT?
Yes
ESCAPE SEQUENCES
\n - Newline - moves cursor to next line better with text strings \t - Horizontal tab -moves cursor to next tab stop \a - Alarm - causes the computer to beep \\ - Backslash - causes a backslash to be printed \' - Single quote - causes single quotation to print \" - Double quote - causes double quotation to print
IDENTIFIERS
a descriptive name that maps to a location in computer memory. Identifiers give a place in memory a name, and when used, the location can be referenced. -called SYMBOLIC REFERENCING -can only have letters, numbers, or underscore(_) -must begin with a letter -can't have spaces -no special characters -case sensitive
ACCUMULATOR
a running total must be initialized
BREAK STATEMENT
a statement that forces a block of code to terminate or exit. if you do not use inside switch then all of the statements succeeding will execute. sometimes we want this though.
KEYWORD
a word that has some sort of predefined meaning in the context of a programming language
WHAT DATA TYPE WOULD WE USE TO STORE A LETTER GRADE?
char
EXPLAIN THE DIFFERENCE BETWEEN THE FOLLOWING DECLARATIONS char charVal; char charVal[11];
char charVal; reserves only 1 byte for single character char charVal[11]; reserves 11 bytes for a string of characters
HOW WOULD WE DECLARE A C-STRING VARIABLE WHICH COULD STORE A NAME OF UP TO AND INCLUDING 10 CHARACTERS?
char name[11]; (10 characters for the name and +1 for the null terminator)
DATA TABLE
comments to the right of variables being declared in variable declaration The declaration section must contain a data table which must contain - how data is defined/ used i.e. INPUT & CALC -describes what data it will contain ex: char name; // INPUT & OUTPUT - user's name input and output in program
HOW WOULD WE DECLARE A CONSTANT NAMED A WHICH WOULD STORE THE SINGLE VALUE 'A'?
const char A = 'A';
GIVE AN EXAMPLE OF HOW TO DECLARE CONSTANT IN CODE
const datatype CONSTANT_NAME = 5; or more specifically: const int TAX_RATE = .075;
WHAT DOES CONSTANT DECLARATION LOOK LIKE IN CODE?
const float TAX_RATE = 0.075; const int WEEKLY_HRS = 40;
COUNTER
counting the # of instances must be initialized
WRITE THE CODE IN YOUR HEAD AS A CONDITIONAL OPERATOR int age; bool regStat; if(age >=18) { if (regStat) { cout << "you can vote"; } else { cout << "not registered"; } else { cout << "too young"; }
cout << (age >= 18? ( regStat ? "you can vote" : "not registered") : "too young" )
WHAT IS THE DOCUMENTATION NEXT TO THE DECLARATION SECTION CALLED?
data table
char
datatype 1 byte Characters enclosed in quotes ex: 'a', 'b', 'c'
bool
datatype Boolean 1 byte once of two values: true or false 1, 0
double
datatype double precision float 8 bytes Floats up to 15 digits same as float but large numbers
float
datatype floating point numbers 4 bytes pos. or negative decimal numbers - including fractional part (up to 7 digits) ex: 32.2345, 0.0, -.045, 123.123456
long
datatype long integer 4 bytes (8 on some systems) same as int
int
datatype 4 bytes pos. or neg. integers (whole numbers) (9 digits 2^32) ex: 34, 6, 12345, -23 etc.
WHAT DOES THE COMPILER NEED TO KNOW WHEN DECLARING IDENTIFIERS?
datatype the type of data how much to store
GIVE AN EXAMPLE OF HOW TO DECLARE A VARIABLE IN CODE
datatype variableName; or more specifically: int variableName;
WHAT DATA TYPE WOULD WE USE TO STORE THE AVERAGE OF A SUM OF INTEGERS?
float and technically a double
WHEN WOULD YOU NEED AN ACCUMULATOR?
for running totals - sum an unknown number of inputs
in the conditional statement (hrsWrkd < 40 ? (hrsWrkd - 40)*rate*1.5:0.0); which is the condition? which is true statement? which is the false statement?
hrsWrkd<40 is condition (hrsWrkd-40)*rate*1.5 is true statement 0.0 is false statement
C-Strings
if you have more than one character you need to store you use c strings - An array of characters - The last character is called the null terminator (\0) null terminator tells the compiler where the end of the string is - We need to specify how many characters we want ex: char variableName[15]; can hold 15 characters, 14 + null terminator (null terminator counts as character)
GIVE AN EXAMPLE OF WHEN IT IS USEFUL NOT TO USE BREAK STATEMENTS WITHIN A SWITCH STATEMENT
if you want the succeeding statements to execute such as switch (letterGrade) case 'a' : case 'A' : gradePoint = 4.0 letterGrade = gradePoint; break; case 'b' : case 'B' : gradePoint = 3.0 letterGrade = gradePoint break; default : cout << "invalid";
GIVE AN EXAMPLE OF A ONE WAY SELECTIONS STATEMENT
if-then
WHICH STATEMENT WOULD YOU USE IF YOU WANT TO OUTPUT "YOU ARE OVER A CENTURY!" WHEN AGE IS GREATER THAN 100.
if-then
WHICH STATEMENT WOULD YOU USE IF YOU WANT TO OUTPUT "GO TO THE BEACH" WHEN THE WEATHER IS GOOD AND "STAY HOME AND WATCH A MOVIE" WHEN IT IS NOT
if-then-else
GIVE EXAMPLES OF A TWO-WAY SELECTION STATEMENTS
if-then-else conditional statements
WHICH STATEMENT WOULD YOU USE IF YOU WANT TO OUTPUT THE SIGN OF AN INTEGER ('+' IF int1 is > 0, 0 if it is equal to 0 and '-' if it is negative)
if-then-else-if
GIVE EXAMPLES OF A MULTI-WAY SELECTION STATEMENTS
if-then-else-if switch nested if nested conditional statements if with nested switch nested switch
WHERE DO INITIALIZE CHECK AND CHANGE HAPPEN ON A DO WHILE LOOP?
initialization can happen inside the loop check happens at the bottom or end LCV MUST be updated within the loop
WHAT SHOULD WE ALWAYS DO BEFORE USING AN ACCUMULATOR?
initialize it to 0
WHAT SHOULD THE LCV BE WHEN YOU ALLOW THE USER TO ENTER IN NUMBERS UNTIL THEY ENTER 0
inputNum != 0
SEQUENCE
instructions are executed one after the other in the order they appear in the program
WHAT DATA TYPE WOULD WE USE TO STORE A COUNTER?
int
WHAT DOES VARIABLE DECLARATION LOOK LIKE IN CODE?
int payRate; char name[25]; float averageAge;
WHAT IS THE DIFFERENCE BETWEEN A PRIMARY AND SECONDARY DECISION?
primary is outer if secondary is inner if
WHEN DO WE INITIALIZE A VARIABLE?
just before their use in the program, before we have defined a value for it. ex: int count; count = 0 incorrect - incount = 0; we only do this with CONSTANTS
'\n'
new line -best when used with text
WHAT IS A LITERAL IN C++?
on right side of cout (string, int, or float)
HOW DO WE USE A VALUE STORED IN A VARIABLE?
output and in an expression - cout << variableValue << " is your value for your variable";
REWRITE THE EXPRESSION DISTRIBUTING THE ! OPERATOR !(num1 >= num2 && num3 < num4 || num1 != -1)
remember the order of precedence! (num1 < num2 ||num3 >= num4) && num1 == -1
SYNTAX OF A PROGRAMMING LANGUAGE?
rules that dictate how valid instructions are written
SEMANTICS OF A PROGRAMMING LANGUAGE?
rules that dictate the meaning attached to the instructions
ALGORITHM
step by step definition of a process
WHICH STATEMENT WOULD YOU USE IF YOU WANT TO OUTPUT THE CARDINAL DIRECTIONS BASED ON A CHAR ('W' - "West", 'E' - "East", ...)
switch
#include <iomanip>
tells pre-processor we will be using output manipulators like setw, setprecision, fixed, showpoint etc
#include <iostream>
tells the pre-processor we will be using input output functions cout and cin statements
#include <cstring>
tells the pre-processor we will be using strngcpy etc
BLACK BOX TESTING
testing FUNCTIONALITY of code think about what the code should do and write test cases before coding - you don't see code - Test based on specifications of program - determine test cases and expected output - compare the actual output to the expected output - Plans are written ahead of time - Testing Inputs and Outputs
WHITE BOX TESTING
tests structure and think about loops and selection statements - You can see the code - you define test cases based on what you see of the code - Testing STRUCTURE (internal behavior) of the code ensuring it meets the design - TEST CONDITIONAL STATEMENTS - Test pathways of nested statements - if statements true/false - Loops - while loops - what happens if first input causes loop to exit? - test boundaries if (hours>40) then test values: 40, 39, 41
COMMENTS
text in source code using // the compiler ignores block comments use /* comment block contents -anything between these will be commented out*/
WHERE IN THE C++ PROGRAM DO WE PUT THE DECLARATION SECTION?
under int main()
WHEN I AM USING IF != && in a conditional statement
use == || in assigning value to bool during initialization before the conditional statement using !=
WHEN DO WE STOP REFINING IN A HIPO CHART?
when each module represents a single instruction
WHEN DO WE INITIALIZE A CONSTANT?
when it is declared
WHICH BOOLEAN OPERATOR STOPS AS SOON AS FALSE CONDITION IS FOUND OR MET?
when using AND && operator evaluation STOPS as soon as FALSE condition is found
WHICH BOOLEAN OPERATOR STOPS AS SOON AS TRUE CONDITION IS FOUND OR MET?
when using OR || operator evaluation STOPS as soon as TRUE condition is found
TYPE CASTING
when you add two values together and convert them to a floating point value in the assignment using float data type and ()
WHEN WOULD YOU NEED A COUNTER?
when you need to count an unknown number of inputs when the user controls how many times the loop should execute when you want to average something
WHAT IS THE OR OPERATOR?
||