C++

¡Supera tus tareas y exámenes ahora con Quizwiz!

Booleans in C++

&& is 'and' , || is 'or' , ! is 'not'

Which Side Increment ++ Decrement --

***** But when printing you can put the operator in front of the variable so it "increments" before reading and printing the variable. GREAT GOTHCA TEST QUESTION cout << ++a << endl; cout << --b << endl;

Practice C++

3. Try out/play around in Jupyterlab (or your IDE) with the activities in the "book" at the end of chapter 1.

./hello_test "[greeting]"

It says some tests fail, and you can get more info by typing certain commands. In this case, that command is ./hello_test "[greeting]" - complete with dot slash and double quotes and square brackets. When you do that, you're running a specific unit test, and you'll likely need to do that a lot in the weeks to come

Increment ++ Decrement --

int a = 2; a = a + 1; // a becomes 3 a++; // a becomes 4 increment operator int b = 2; b = b - 1 . // b becomes 1 b--; // b becomes 0

C style Loops

int number [ ] = { 3, 9 , 2, 8 }; int sum = 0; for (int i=0; i < 4; i++) { . // **** C style Loop .... sum = sum + numbers[i]; } cout << "sum of numbers is: ' << sum << endl;

Looping through Arrays

int sum = 0 for (int n : numbers) { .... sum = sum + n; // this can be any code for n } cout << "sum is: " << sum << endl;

initialize and declare an array in one step

int vals [ ] = { 3, 9, 2, 8}; compiler determines size of array based on what you provided. Good for small amount of data. cout << vals[0] << endl; // prints 3 cout << vals[3] << endl; // prints 8

To display values on the screen in C++

you use an entity called cout and the << operator (sometimes called the insertion operator). For example, the statement cout << 39 + 3; displays the number 42. cout << "The answer is " << 6 * 7; displays The answer is 42

Type cast ---- Changing a variable type (new type) x

Desired type put in Parenthesis in front of variable (float) x; // this will change x from int to float Change integer X and integer Y to get float Z float z = (float) x / (float) y When doing a for loop you can use 'auto' . The compiler knows the type but you or a person reading the code do not. int sum = 0 for (auto n : numbers) { .... sum = sum + n; } cout << "sum is: " << sum << endl;

This is a sanity check that you can edit, build, and run C++ programs. Get the pattern into your head: edit, build, run.

Get the pattern into your head: edit, build, run. Summarizing how to do this in the command line: 1. Make sure you're in the right place (so use cd and pwd). 2. Build your program with make <target> or just make if you're building the default target. 3. Run your program with ./some_program_name, using dot-slash in front.

$ make

On the first line, by typing $ make you build the project with the default target (that's why you just type "make" with nothing after it).

$ python grade.py hello_test

On the second line, $ python grade.py hello_test you run a python script and have it run hello_test in a few ways, and it collects data about the outcomes

The endl symbol

The endl symbol denotes an end of line marker. When this marker is sent to cout, the cursor is moved to the first column in the next screen row. If you don't use an end of line marker, then the next displayed item will simply follow the current string on the same line. In this program we only printed one item, but in general we will want to print multiple items, and it is a good habit to end all lines of output with an end of line marker.

What's the dot and slash about? ./hello_main

What's the dot and slash about? That means "look for a file in the current directory". This is needed because the terminal will only look for executable files in certain locations. If you use the dot slash in front of your program name, it will look in your current directory.

For Loop - you can use "auto" for type

When doing a for loop you can use 'auto' . The compiler knows the type but you or a person reading the code do not. int sum = 0 for (auto n : numbers) { .... sum = sum + n; } cout << "sum is: " << sum << endl;


Conjuntos de estudio relacionados

Principles of Radiographic Imaging Final

View Set

Ch 90 Male reproductive textbook of basic nursing 10th Ed

View Set

ch. 11 nucleic acid structure and DNA replication

View Set