C++ paper so far. Please look over to see if ok. Then I will move on.
C++ PROGRAMMING
C++ is case sensitive but it is not space sensitive.
PREPROCESSOR COMMANDS
# - preprocessor. Tells the compiler to process before compilation.
#include <iostream> : Tells the processor to include the contents of the input/output stream header file before it compiles.
The next three may not be the new standard:
#include <ifstream.h> - file input.
#include <ofstream.h> - file output.
#include <iomanip.h> - required for certain in/out changes.
#include <cmath> - Required for some complicated math and conversion. (One thing to remember about this library is that everything is output as type: double). Here are some of the functions supported:
ceil (x) - rounds x to the smallest integer not less than x. 9.2 would be 10. -8.8 would be -8.0.
floor (x) - rounds x to the largest integer not greater than x. 9.2 would be 9. -9.8 would be 9.0.
cos (x) - trigonometric cosine of x. 0.0 would be 1.0.
exp (x) - exponential function ex. 1.0 would be 2.71828.
fabs (x) - absolute value of x.
fmod (x, y) - remainder of x/y as a floating point #. fmod (13.657, 2.333) would be 1.992.
log (x) - natural logarithm of x (base e). 2.718282 would be 2.0
log10 (x) - logarithm of x (base 10). 100 would be 2.0.
pow (x, y) - x raised to the y power. pow (2, 3) would be 8.
sin (x) - trigonometric sine of x. 0.0 would be 0.
sqrt (x) - square root of x. 900.0 would be 30.0.
tan (x) - trigonometric tangent of x. 0.0 would be 0.
#include <cstdlib>
rand () - Random selection. Uses a module operator to represent the max # of choices. 1 + rand () % 6. The 1 represents the starting #.
srand () - Random selection. When executed it does not start the same every time. Better than rand. This function takes an unsigned variable type. An unsigned variable type looks like this: unsigned x; Ex2: unsigned long x; Notice that int did not need to be used because unsigned by default is of type int. An unsigned variable cannot have a negative value.
COMMENTS
// - this makes a comment. Anything after // is ignored by the C++ compiler.
/* - likewise, this also is comment. Don't forget this ending symbol - */
STATEMENTS
Statements always end with ;
VARIABLES
Local - any variable declared inside a function or function body or otherwise called block {}.
Global - any variable declared outside all function blocks {}.
To use a variable it must be declared (int x;) before it is used. You can specify many at once too - int x, y, z;
int is the variable type. It states that the variable is an integer - a whole number (0, 1...). You will use int a lot because letters in C++ have # values too. Here are some more:
float - real # (3.12546) If the decimal #'s are too long it will be rounded off as in math.
double - takes more memory space than float, but is more accurate because more numbers after the decimal are used.
Note: When using long or double be careful; C++ does not always round off correctly. There should be some classes that you can get to do exact math.
char - a single character and nothing more. Can be a number but can't be used for math. c @ $ 1. . . etc.
string - a sentence or even a single character. Note: strings are not really variable types but can be treated as such for most purposes.
long - same as int but reserves more space. However, There is no difference between int and long in some compilers.
When a variable is declared C++ reserves a memory location in the computer for that variable. When cin >> is used it will place whatever the user enters in the memory location. If the user then enters another character, the original data is destroyed and the new replaces it.
A variable should be less than 31 characters long and should not contain any spaces. Use_for spacing. They also cannot begin with a digit.
int main ( ) - this is a function. Main is usually where the program starts execution. Even if main is not at the top of the source file.
( ) - indicates that main is a function. Notice that there is no ; That is because it is not a statement, it is a function.
Casting - changing one variable type to another. Example: static_cast<float>(x)
ESCAPE SEQUENCES
\n - New line \t - Tab \r - return to the beginning of the line \a - Alert (beep) \\ - backslash
\" - double quote ( " )
IN/OUT STREAMS
cout << "HI there! \n";
cout - "pronounced, see out". And these: << tell the computer to send the information contained in " " to the output device. The output device is usually the screen and printer.
I can also do this: cout<< a*b + (x-y) << endl; which will subtract x from y, multiply a and b, and add them together, outputting the result.
endl; - End line. Not only does it do the same as \n but it also flushes the output buffer.
flush; - flushes the output buffer.
Stream Manipulators:
If you want to keep the decimal places to a certain length do this: cout << setprecision(2) << x << endl; Note: setprecision requires the preprocessor directive <iomanip.h>
If you want the variable to print out as a fixed # as opposed to a scientific # do this: cout << setiosflags (ios::fixed) << endl;
If you want cout to print decimals numbers no matter what then use this: cout << setiosflags (ios::showpoint) << endl; 89 will print as 89.00.
All three combined: cout << "x is " << setprecision (2) << setiosflags (ios::fixed | ios::showpoint) << x << endl; The bar ( | ) is a separator between types of s.m.'s.
setw (4) - set width to 4 spaces. If the output is less than the reserved space then the value is right justified by default. If it is more than the reserved amount then it is adjusted to accommodate.
(ios::left) - left justifies output.
MATHMATICAL OPERATIONS IN C++
C++ is mathematically sound. It will do basic and complicated math and even follows the order of operations.
A couple things you need to remember:
Never put your program in a situation where it could try to divide by 0. That could be a fatal error.
If you declare a variable as type int and want to get a pretty accurate answer you need to do a variable type cast or declare float or double to accommodate for decimals. Otherwise C++ will not round it to the next highest number but will truncate it - remove the decimal numbers all together. 98.99 would become 98.
Some examples:
+, -, *, /, =, pow (x, y) - x to the y power. Ex: pow (x * 4, 2 ). When using power it is always a type, double value.
x = x+3; can be written as: x+ = 3; x=x/2; can be written as x/=2;
Incrementing and Decrementing - adds or subtracts 1, either before or after use. Examples:
to increment x after it is used, write x++; to decrement x before it is used, write --x;
int c=1; cout << x << endl; cout<< x++ << endl; cout<< x << endl; // would yield: 1-1-2
Note: do not try to increment an equation. Ex: ++ (x+y)
RELATIONAL OPERATORS
a==b - a is equal to b. (Be careful: = is not the same as ==. = is an assignment operator. x=y is stated as x is assigned the value of y.)
a != b - a is not equal to b. The ! is "Not" and can be used like this - if (! (x==y) && (x==g) ).
<, >, <=, >= - same as math. Do not switch the order of the <= to => because it will be an error.
if (a&&b!=c) - && and.
if (a||b!=c) - || or.
Note: Do not put spaces in between any of the above operators like this ! =, < =, & & . . .
?: - conditional operator. Closely related to if/else. Ex: cout << (x >= 6 ? "true" : "false");
The ? can be seen as "if it is true do this" and the : "if it is false do this".
Various ways to use these operators:
if (x == 1 || y<=6) ++x; x >= 6 ? cout << "true" : cout << "false"; if ((x == 1) && (y<=6)) ++x;
if (! (x==y) ) x++; for (x != y) cout << "not =";
OTHER OPERATORS
Assignment
=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
Bitwise
& - and, ^ - xor, | - or
RETURN STATEMENT
return 0; - simply ends the program successfully.
CONTROL STRUCTURES:
IF / ELSE
Remember this: if and else expect only one statement to be specified. If you want to use 2 or more you must {brace} the body of both if and else. Note: else does not need a corresponding if.
if (a>=90 || a>=91)
cout << "A";
else if (a>=80)
cout << "B";
else if (a>=70)
cout << "C";
WHILE (loop)
while (x != 0){ //use the "!= 0" to make sure you don't accidentally divide by 0. Dividing by 0 a program error.
x = x/2; cout << x;}
Example:
int main (){
int x =1; while (x <= 3) {cout << x << endl; ++x;} return 0;} yield - 1\n 2\n 3\n (\n - is a new line)
FOR (loop)
Just like the if loop, the for loop expects only one statement. If there are more than one it must be {braced}. The for loop is more complicated than the while loop. But it is easier to read and debug.
for (int x=1; x <= 3; x++) /* int x=1; is the initialization of the variable. x<=3; is the condition upon which the for loop executes the body or not. x++ is the increment. x is incremented after it loops back to the top. */
cout << x << endl; yield - 1\n 2\n 3\n
Note: if you do not have any arguments to put in the for loop, do not leave it blank. Do this - for (;;).
Note: the declared variable in the for loop can only be used in that for loop. It will be unknown to the rest of the program. See scope in STORAGE CLASS SPECIFIERS.
Example 2:
int x=2, y=10;
for (int j = x; j <= 4; j += y/x) is == for (int j = 2; j <= 80; j += 5) // j += 5 adds 5 to the value of j.
DO/WHILE (loop)
When using the do loop remember that the body will always be carried out at least once. It will check to see if the conditions are met outside the brace in while and continue to loop as long as the conditions hold true.
do {Your commands}while (x != 11)
BREAK AND CONTINUE STATEMENTS
break; - is used to completely exit out of a loop. Break is required to exit a switch structure.
continue; - is used to immediately go back to the top of a loop.
Re: C++ paper so far. Please look over to see if ok. Then I will move on.
Quote:
Originally posted by aewarnick
The type int is used about 90% of the time
Where do you get this statistic? Seems very unlikely to me, as in proper C++ it would seem that the most commonly used types are classes.