NIU CSCI 240 Exam 1 Study Guide
When is the 3rd subexpression in for (--; --; --) statement executed?
after the body of the loop has been executed
Why is a while loop described as "top-driven"
it must be tested before the body of the loop then executed
Explain in detail what the following instruction does (assuming i is declared as int): cin >> i;
it will allow a user to input an integer
Integers
no decimal point, comma, or $; leading + - allowed range (on our system): about plus or minus 2 billion; int constants are written as: 1234 -3 43
Link error
one of the functions used in the program is not found when it's time to run the program. In the program above, there are no such functions
Run-time error
program does not run to completion. Example: divide by zero: set x = 10 and y = 0 and then try to compute x/y
Logic error
program runs to completion, but the results are wrong. Example: you really wanted x - y but coded x + y in calculating an average, you use the wrong number to divide
setw (size)
sets a MINIMUM width; size - must be an integer value must #include <iomanip>; the width is only valid for the variable that is printed; ex: int num1 = 1234; int num2 = 56789; cout << setw(6) << num1; _ _ 1 2 3 4 cout << setw(6) << num1 << setw(6) << num2; _ _ 1 2 3 4 _ 5 6 7 8 9
Given the following declaration: double x; What is the value of x?
some value in the range of -+ 2 mil; unknown; garbage= value
Typecast
tell C++ that it should temporarily use the variable as if it were some other data type. To do this, put the name of the data type you want in parenthesis before the variable name. It will not change the variable type or value permanently, but it will force C++ to temporarily treat the variable as a different type: (double) sum/count // or sum /(double)count
What will the following code fragment print? for (i = 0; i < 5; i++); cout << i + 1; cout << i;
the code will print out 65
Given the following: double price = 30.00; double tax = 1.80; double sum; sum = price + tax; Explain in detail what the last line does in terms of variables, calculations, and assignment of values.
the last line of code adds together the variables, price and tax giving you the sum
Data
there are several data types (numbers, characters, etc.); each individual data item must be declared and named; each individual data item must have a value before use; initial values come from: program instructions user input disk files
Name the 3 C++ statements used to create a loop.
while for do while
Which of the following increments x by 1?
x+=1
Write a WHILE loop to display the first 5 multiples of 10 on one line. For example: 10 20 30 40 50
x=10 while (x<60) { cout<<x<< " "; x+10;
About how many decimal places of accuracy does a float have?
6
Most lines in a C++ program end with a
; (semi-colon)
Which of the following is a legal way to create a C-style symbolic constant? #define MAX_VAL = 30; #define MAX_VAL 30 #define MAX_VAL = 30 #define MAX_VAL
#define MAX_VAL 30
Create a symbolic constant named PI to represent the value of pi (3.14) in two different ways.
#define PI 3.14 or const double PI = 3.14
Name two libraries that should be #include'd at the top of a C++ program.
#include <iostream> #include <iomanip>
Truth Tables: &&
(true && true) => true (true && false) => false (false && true) => false (false && false) => false So for (c1 && c2) to be true, both must be true.
Truth Tables: ||
(true || true) => true (true || false) => true (false || true) => true (false || false) => false So for (c1 || c2) to be true, one or the other or both must be true.
Arithmetic Operations
+ addition - subtraction or unary negation (-5) * multiplication / division (see special notes on division below) % modulus division -- integer remainder of integer division
What is the value of the expression 25 % 3? 8 1 0 undefined
1
What is the value (in C++) of the expression 3/2?
1 , no decimals will appear because 3 and 2 are integers so the answer will be an integer
About how many decimal places of accuracy does a double have?
12
What is the value (in C++) of the expression 4 / 2 * 2?
4
C++
C++ is a superset of the C programming language. Any C code is also C++ code, but some C++ code is not C.
Suppose x = 0; y = 5; Is the following condition true or false? if (x > 0 && x < 10 || y = = 4)
False x not 0 , x is greater than 0 but less than 10 so the whole thing is FALSE because whatever is near && (which is x in this case) make the statement true or false
Symbolic Constants
Symbolic Constants allow us to associate a value with a symbolic and meaningful name. You use the name wherever you want the value. By convention, symbolic constants are always CAPITALIZED as a signal to the (human) reader of the program that the value is a constant. Use these whenever you have a fixed value used in several places in a program. This value will not change while the program is running, but the value may change at some later time (example: postal rates - cents per ounce). You would then change the value and recompile the program.
Explain in detail what the following instruction does (assuming i is declared as int): cin >> i;
The code above states that there will be a value inputed (allows the user to input a value)/ associated with i later on. For example it could be used in a way such as i++, if the input value of i is 1 the ++ will incrementally increase i by 1 thus making i 2.
Scope
The scope of a variable is the location within a program where a variable can be used. With control structures, all of the statements that are contained within a block of code belong to the control structure. Therefore, any variable that is declared within such a block of code only has meaning between the declaration for the variable and the closing curly brace for the block of code.
Floating Point Format Flag
There are two ways to format floating point numbers: fixed -- print with a fixed number of digits after the decimal point scientific -- print in scientific notation ex: double y = 50.0512; cout << fixed << setprecision(2); cout << y; 50.05
Do While Loop
This version of a loop instruction ensures the body of the loop always executes at least once. After each repetition, the exit condition is tested. So this is classified as a bottom-driven loop.
The formula for converting a Fahrenheit temperature to Centigrade is 5/9(F - 32). What is wrong with writing it in C++ as C = 5/9 * (F - 32); assuming that C and F are both declared as doubles, and F has a valid value.
What's wrong with it is you're always going to get 0 because it is integer division and 5 does not go into 9 evenly. You can fix it by typecasting or writing 5.0/9 (that will give you the remainder).
If a read-loop is written to process an unknown number of values using the while construct, and if there is one read before the while instruction there will also be one
at the bottom of the body of the loop
Bool
bool variables can have one of two possible values: true and false
String
can hold 0 to many characters; string constants are written with double quotes: "Hello, world"; ex: string MyName = "Amy";
Char
can range from 1-31 characters long; can hold just one character; char constants are written with single quotes: 'a'
When you write an illegal C++ statement and try to compile and run the program, you will get a
compile error
What instruction will display data on the screen from a C++ program?
cout
The three basic loop statements in C++ are while, _______, and _________.
for do while
Write a fragment of code that accepts integers from the user until a negative number is entered. The prompt should be "Enter a number (negative to quit):" Add up the numbers and print the sum (not including the negative number). Assume and use the following declarations: int sum = 0; int num;
cout<<"Enter a number, negative to quit"; cin>>num; while(num > =0) { sum += num; cout<< "Enter a number, negative to quit"; cin>> num;} cout<<"The sum is "<<sum;
Suppose you have two integer variables (named num and sum) with valid values. Write a single cout instruction to display them as follows: num is __ sum is __ the underscore characters will show the actual values in num and sum - for example: num is 4 sum is 24
cout<<"num is 4"<<num<<endl<<"sum is 24"<<sum;
According to the lecture notes, the two main conceptual components of a program are _____ and _____.
data and instructions
The three basic flow-of-control patterns are sequence, ___________ and __________
decision and loop or repetiotion
setprecision( num )
does 1 of 2 things: If setprecision is used by itself, the value num specifies the total number of digits to display. If setprecision is used in conjunction with the fixed flag, the value num specifies the number of digits to display after the decimal point num - must be an integer value the value is rounded up when it is displayed the precision stays set until it is changed must #include <iomanip>
Which data type has the largest range? int float double it depends on the input value
double
Give two reasons why symbolic constants might be used in a C++ program.
ease of modifying code and gives a meaningful name for a value (easier to read code)
Write a for loop that does exactly the same thing as the following code: i = 1; while (i < 11) { cout << "the square root of " << i << " is " << sqrt(i); i += 2; }
for (i =1; i<11, i+=2) cout<<"The square root of"<<i<<"is"<<sqrt(i);
Write a for loop to display the first 5 multiples of 10 on one line. For example: 10 20 30 40 50
for(x=1; x<=5;x++) cout<<x*10<< " ";
Float and Double
has decimal point; leading + - allowed no comma, $; range (float) plus or minus 10 to 38th (limited to ~ 6 significant digits); range (double) plus or minus 10 to 308th (limited to ~12 significant digits); floating point constants are written as 3.08 -32.0 0.15 9.3e7 (9.3 times 10 to the 7th power)
Incrementing/ Decrementing
i ++; i += 1; i = i + 1 all increment by 1 n--; j -= 1; j = j - 1 all decrement by 1
Modify (rewrite) the following instruction so that the subtraction is evaluated first: i = a * b / c - d;
i= a*b/(c-d);
Name one command that is used to implement the decision statement control structure that will be studied in this course.
if
Write a fragment of code that checks a string variable to see if it contains your first name or not, and then prints either "that's me" or "that's not me" accordingly. Assume that the string variable aName has a valid string in it before the test is made.
if (aName == "Stephanie") cout<<"That's me>> else cout<<"That's not me";
Write a decision statement to test if a number is even or not. If it is, print "even". If it is not, add 1 to it and print "it was odd, but now it's not".
if(num%2==0) cout<<"even"; else { num++; cout<<"It was was odd, but now it's not"; }
main() marks the beginning of a C++ program. What C++ reserved word precedes it?
int
Select the three control structures that (along with sequence) will be studied in this course.
int decision repetition/looping #include branch and return/function calling
Testing if a point is inside a rectangle
int ptX, ptY; int rectLeft, rectRight, rectTop, rectBot; if (ptX > rectLeft && ptX < rectRight && ptY > rectBot && ptY < rectTop) { cout << "pt is in rect"; } else { cout << "pt is not in rect"; }
What is the correct way to declare an integer variable named "score"?
int score;