Description
CS 10 Study Guide
***HELP: to find what you need tyr [Ctrl + F] and type keyword so find section easier
Basic Knowledge:
∙ Input: A program collects date; from storage, screen, etc. ∙ Process: A program performs computations on that date. ∙ Output: A program puts that data somewhere; storage, on screen, etc. ∙ Variables: to refer to data, like x, y, and z.
∙ Compiler: turns "human language" to "computer language" Library: <iostream>
START PROGRAMMING
∙ Programs Start with main() and will run what is in {}
Don't forget about the age old question of What is the meaning of enduring patterns of behavior?
∙ Lines of code should end with ; like sentences end with periods ∙ Return 0 ends the program
∙ cin and cout are for characters in and out for inputs and outputs ∙ string literal are text in " "
∙ char
∙ char myChar = 'character' or number literal //note: only one character is recorded per char
∙
∙ string
∙ Strings-need to add the library
∙ string myString = "---"
∙ Cin will collect until the first white space; can be fixed with getline ( cin, myString)
∙ Multiple cout statements continue printing on the same output line. cout << endl starts a new output line.
∙ reserved word or keyword: is a word that is part of the language, like int, short, or double We also discuss several other topics like What are the main states of matter?
∙ Identifier is name created by the programmer for an item like a variable or function. Must,
o be a sequence of letters (a-z, A-Z), underscores (_), and digits (0-9) o start with a letter or underscore Don't forget about the age old question of How would you handle an employee who violates company policy?
Don't forget about the age old question of In what two separate ways do plants obtain nutrients?
#include <iostream> is the library for character buffers
using namespace std prioritizes library
int main () is the beginning of the code
return 0 runs with no errors
int : integers
Comments: // or /* */ to comments
// comments after this form will only have what is after the "//" ignored in that line
/*
All lines within these form of comments
Will be comments
And ignored by compiler
*/
Static_cast < > stores the information to the <storage location>
A type cast explicitly converts a value of one type to another type.
If you want to learn more check out How do broken dams cause flooding?
ERRORS:
A programmer can use static_cast<type>(expression) myIntVar is 7, then static_cast<double>(myIntVar) conIf you want to learn more check out How long did it take for the buildings to collapse?
compiler errors; cant compute runtime error; stuck on a line of code and cannot continue
logic error; -----
Math:
Arithmetic operators:
Int
+ add
-subtract
*multiply
/divide (divide by zero is a runtime error)
%remainder after division (known as a mod)
_________________________________________
Double
+
-
*
/
NO MOD FOR DOUBLE - compiler error
compound operators provide a shorthand way to update a variable, such as userAge += 1 being shorthand for userAge = userAge + 1.
Other compound operators include -=, *=, /=, and %=.
myNum = myNum + 1
Same as: myNum += 1
Other forms are: i ++, i --: which are i = i + 1 and i = i – 1 //i is just a generic variable
floating-point (double) number is a real number, like 98.6, 0.0001, or -666.667. The term "floating-point" refers to the decimal point being able to appear anywhere in the number.
But we will be writing double in the code. (much like int)
**REMEMBER when dividing int it will cut out the decimals unless it is a double
There are many ways of making things double; static_cast<double>, make a double variable, divide by a double in some cases
% evaluates the remainder of the division of two integer operands. Both numbers must be integers
Library: <cmath>
#include <cmath>
Allows for more math options, but the library must be added above.
Function
Behavior
Example
sqrt(x)
Square root of x
sqrt(9.0) evaluates to 3.0.
pow(x,
y)
Power
pow(6.0, 2.0) evaluates to 36.0.
fabs(x)
Absolute value of x
fabs(-99.5) evaluates to 99.5.
Cool info on variables:
Variable
type:
int (integer) double
long
char
string
bit
Size of
memory:
32 bits
64 bits
64 bits
8 bits
?
1 bit
What is stored:
A whole number (-, 0, +)
Real numbers
Letter or character Collection or chars True or false
Max:
~4 billion
numbers
Small to large
Char 0 to del ?
0-1
Cout uses <<
Cin uses >>
“ “ are for strings
‘ ‘ are for single chars
Loops:
while () {}: causes LOOPS! Will keep running what is in { } as long as what is in ( ) is true
for (int i = 0; i < N; ++i) : is another form for a loop that is good for doing a loop a certain number of times. It has a built in counter (in this example its it i) and will go up by 1 every loop until it is N (number of loops/where to stop)
do{ }: dowhile loops are loops that execute the loops’ body before checking the loop condition. Example below.
do {
// Loop body
} while (loopExpression);
Index – the location of string and the character in the location. Note: index start at number 0.
“Bob” : index 0 = ‘B’ | index 1 = ‘o’ | index 2 = ‘b’ Library: <string>
//Note that someString can be any string and is a generic variable
someString.at () : what is in the ( ) is the index and the .at will pull the char at the index
EX) someString = “Hello”
someString.at (2) will return ‘l’
someString.size() and someString.length() : they both give the number of characters in the string. Can use either or.
someString = “Hello”
someString.size() and someString.length() both return the number 5
someString.append() : will take the string or string variable inside the ( ) and add it to the end of someString
someString = “Hello”
someString.append (“_World”) returns “Hello_World”
someString.push_back () : is similar to .append but instead of a string it will only add a char that is in the ( )
someString = “Hello”
someString.push_back (s) returns “Hellos”
someString1 + someString2 : add the string. If one is a string, the other can be a char
someString.insert (index , substring) : will put the substring at the index
someString = “Hello”
someString.insert (2 , “olo”) returns “Helololo”
someString.replace(index, number, subtring ) : will replace part of the string, starting from index to number of characters, and replace it with substring
someString = “Hello”
someString.replace (2 , 3 , ‘y’) returns “Hey”
someString.fine () : will output the index where, what is in ( ), is first found
someString.substr (index , len) : return substring from index and have len chars
Library: <cctype>
isalpha (index) : will give true if char is alphabetic isdigit (index) : true if char is a digit
isspace (index) : true if it’s a whitespace
toupper ( index) : will return the char at index in uppercase form tolower (index ) : will return the char at index in lowercase form
Library: <cstdlib>
rand(): returns a random integer between 0 and RAND_MAX.
//important to know that it is pseudo-random and that it will yield the same sequence of values when it is ran. pseudo-random is due to seeds
srand(): changes seed by using the value in the ( ). Default seed is 1.
Library: <ctime>
this library helps with using different seeds constant as time is always changing.
Ex) srand (time(0)); //this is generate unique seeds.
FUNCTIONS
Functions:
Functions: are a named list of statements (statements: what’s is in the {}) Ex) int NumberSquared(){}
Parameters:
Parameters: are the function's input, specified in the function ( )
Ex) int NumberMultiplied( int userNumber, int userNumber2 ){}
Return Statement:
Return: What should be returned by the function
The use of using the functions is to have cleaner and more readable code and be able to recall code without having to write the statements over and over again. This also allows for groups to work on codes without too much interference with each other and will be easier to understand what the code does by looking at the functions' name.
To call a function, you put the functions name and in the ( ) put the variables you want to be used.
Note: the variables do not need to have the same name as the parameters because the variables will be inputted as the parameters for the functions' statements
Ex) cout << NumberMultiplied(x,y);
//assume that x and y have been declared
Helpful Techniques:
∙ Modular development: dividing a program into separate parts that can be developed and tested separately and then integrated to a single program.
∙ Incremental development: writing, compiling, and testing a small amount of code, then writing and testing more until code is complete.
∙ Function stub: is a function definition whose statements have not yet been written.
Pass by Value: argument's value is copied into a local variable for the parameter.
Pass by Reference: parameter does not create a local copy of the argument; instead, the parameter refers directly to the argument variable's memory location.
Scope: a defined variable or function item that is visible to a part of a program.
Global Variable: a variable declared outside any function, unlike a local variable that is declared inside a function.
Function Declaration/Prototype: specifies a function's return type. Ex return userNum1;
Good things to know is how to swap variables. To do it a temporary variable is needed to hold an original variable of the swapping variable
Ex) int num1 = 4
int num2 = 7
int temp_num1;
temp_num1 = num1;
num1 = num2;
num2 = temp_num1;
void function
∙ 1. when we just want to print something to the screen
∙ 2. if the function makes no decision, it calculates nothing, or if it gets nothing
∙ 3. if you need to return more than one item from the function, use reference parameters with the void type
∙ 4. When the parameter might be huge, use reference parameters and maybe a void type
** huge parameters are string, array, vector**
parameters are any piece of info
the function needs to do its job
List of how to create a function
1. Define the type that you want to return.
2. Give it a unique identifier ( or name)
3. Define parameter list. Decide what info function needs to do its job. 4. Return a dummy value.
5. Test function stub to make sure it works okay.
6. Go back and define working part of function.
7. Make sure it gets called from somewhere. Either main or another function.
Vectors: vectors hold information in the index
Similar to strings, we have:
push_bac k()
void push_back(const int newVal);
Append new element having value newVal.
back()
int back();
Returns value of vector's last element. Vector is unchanged.
pop_back ()
void pop_back();
Removes the last element.
In addition, there is a .resize(N) option to resize the vector
Things to know:
Terminal Commands:
cd
Change directory
ls
List
g++
Compiles c++
./
Keeps you in current directory
cd..
Bring you out of
directory