Chapter 2 Flashcards

1
Q

What is a variable? What are the different types?

A

A variable is a place to store information. There are integers (int) floating point numbers, strings, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are assignment statements and what do they do?

A

They store the value of an expression in a variable and replaces the previously stored value. When you set a variable equal to something, that is called an initialization.

EX: variable = statement;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What factors are and aren’t allowed when naming a variable?

A
  • Variables are case sensitive
  • Variables should have descriptive names
    Cans:
  • numbers
  • letters
  • underscores_

Cannot:
- symbols (!, $, %)
- spaces
- reserved words (int, double, include)
- start with a number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Examples on naming variables: legal or illegal?

  • Day_Of_Week
  • 3dGraph
  • _employee_num
  • June1997
  • MixedUp#3
A
  • Day_Of_Week // legal
  • 3dGraph // illegal
  • _employee_num // legal, not recommended
  • June1997 // legal
  • MixedUp#3 // illegal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different data types we deal with?

A
  • Number data types (int, double, etc)
  • Characters
  • Booleans
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the difference between integers and doubles?

A

Integers are whole numbers while doubles are decimal numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the boolean type mean?

A

True or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are character types used for?

A

Make sure to use single quotations to denote

  • They are used to store a single character such as ‘X’ or ‘$’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why do overflow errors occur?

A

An overflow error is when the number is too large and the numeric type can’t store it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why do underflow errors occur?

A

Usually happens for floating types like doubles

  • They occur when the result is too small to be represented in the type being returned by the function.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is casting?

A

Casting is a conversion between different data types.

It is better to use explicit casting rather than implicit casting bc info can be easily lost

Explicit Casting Format: static_cast<data> ()</data>

EX: int n = static_cast<int> (10.9);
- n now holds 10</int>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an roundoff error?

A

Roundoff errors happen bc some numbers can’t be precisely represented with the binary

A trick to solve this is to +0.5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is arithmetic evaluated in C
++?

A

They are evaluated with the same rules in mathematics. Follow PEMDAS!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Compare post increment vs. pre increment. (x++ vs. ++x)

A

Post increment is when the incrementation occurs after the command, while pre incrementation occurs before the command. The same goes for pre/post decrementation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Division vs. Mod (%)

A

Division gives us the number after dividing two numbers. % gives us the remainder after dividing two integers. If the remainder is 0, that means both integers are even.

If the numerator is less than the denominator, the % is equal to the numerator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What C++ library must be used when using math functions?

A

<cmath> : defines many mathematical functions such as sqrt (square root) and pow (raising to a power)

Should look like #include <cmath>
</cmath></cmath>

17
Q

What are constants?

A

Constants are values that cannot be changed

18
Q

How can constants be distinguished?

A

They are typically written in capital letters to be distinguished

EX: there will always be 365 days in a year, so we may use const int YEAR = 365;

19
Q

What are magic numbers and why should they be avoided?

A

Magic numbers are a numeric constant that appears in your code without explanation. Avoiding them limit confusion.

20
Q

What are string literals?

A

String literals are a sequence of characters enclosed in quotation marks.

EX: cout &laquo_space;“I love programming!”;
*notice how the quotations appear on the same line.”

21
Q

What C++ library must be used when dealing with strings

A

We must use <string>. Don't forget to include using namespace std;</string>

22
Q

What are string variables?

A

String variables are variables that store string literals. String literals are a sequence of characters that are enclosed within quotation marks.

*Strings can hold letters, numbers, spaces, and symbols (periods, commas, exclamation, question mark, addition, etc)

23
Q

Explain the difference between strings and chars

A

Strings can hold a sequence of characters while chars can only hold one character. You can tell the difference by looking at the quotation marks. Strings will always have the double quotation (“ “), while chars will always have the single quotations. (‘ ‘)

24
Q

What is concatenation?

A

Concatenation is a form of string manipulation. We use the + operator to add different strings together.

EX:
{ string s1, s2, s3, s4;
s1 = “Hello “;
s2 = “every”;
s3= “one!”;
s4= s1+ s2+ s3;
coud &laquo_space;s4 &laquo_space;endl;
return 0; }

Output: Hello everyone!

25
Q

What is the function of string.length()?

A

string.length() finds the length of characters that are in your string. You can fill in the parenthesis to find specific characters of your string.

If the length is 0, that is called an empty string

26
Q

Explain the string manipulation: operator [ ].

A

The operator [ ] returns individual characters of a string, and is also called subscript or array index operator

In C++, counting starts at 0

EX:
string s = “Hello”;
char c = s[0]; // c holds H

H E L L O
0 1 2 3 4

27
Q

Explain the string manipulation: substring.

A

name.substr(i); - substring starting at position i until the end

name.substr(i, n) - substring starting at position i until length n

28
Q

Explain the string manipulation: erase

A

name.erase(i) - erase all characters starting from index i to the end

name.erase(i,n) - erase at most n characters starting at index i

29
Q

What do we use to allow user keyboard input? What happens if the buffer is empty?

A

cin&raquo_space; variable1

*Can also be chained and look like this: cin&raquo_space; variable1&raquo_space; variable2&raquo_space; … *

If the buffer is empty, the program stops until there is keyboard input (hitting the enter key)

30
Q

What is getline? When is it used?

A

Because cin skips white spaces (if it is a string), we need to use getline in order to read the white spaces. Without getline, only the first word will be read

Getline can only read strings or interpret input as strings

Format: getline (cin, s);

31
Q

What is cin buffer?

A

Cin buffer is a temporary storage space to hold all inputs entered by cin

Ex:
int m=100, n = 1;
double x = 0.5;
cin&raquo_space; m&raquo_space; n&raquo_space; x; cout &laquo_space;m &laquo_space;“ ” &laquo_space;n &laquo_space;“ ” &laquo_space;x &laquo_space;endl;
input: 3.14 // 3 is assigned to m, but there is an error for n because there is a dot instead of an integer so it gives 0 and 0.5 is assigned to x

output: 3 0 0.5

32
Q

What are input failure flags and how do we implement them in our code?

A

cin.fail() is used to detect if the user inputs something we are not looking for.

cin.fail() returns true if the reading fails, and returns false if the reading is successful

We also have cin.clear() which is used to reset the flag to false (0), but doesn’t change the existing input in the buffer

we use cin.ignore or getline to empty the buffer

33
Q

Explain setw(n)

A

Library: #include <iomanip></iomanip>

Format: cout &laquo_space;setw(n) &laquo_space;variable1;

setw stands for set width, and n is an integer that represents the minimum width for the next output operation

Used for making rows and columns, padding/ spacing, output alignment, etc.

34
Q

What is setpercision(n) used for?

A

Helps with precision when it comes to floating point values (decimals) when they are being displayed so that there are at most n significant digits

Ex:
double a = 123.123456789;
double b = 123.1;
cout &laquo_space;setprecision(5) &laquo_space;a &laquo_space;“ ” &laquo_space;b;
// output 123.12 123.1

35
Q

What does using fixed with setprecision(n) do?

A

Using fixed with setprecision(n), it shows you up to n digits after the decimal point.

fixed doesn’t mofidfy variable values. All numbers are rounded in display.

Format: cout &laquo_space;fixed

EX:
double a = 123.123456789;
double b = 123.1;
cout &laquo_space;fixed &laquo_space;setprecision(5) &laquo_space;a &laquo_space;“ ” &laquo_space;b;
// output 123.12346 123.10000

b is 123.10000 because fixed uses 0 as padding as needed

36
Q

What is boolalpha used for?

A

Booleans output 0 for false, and 1 for true, but when we use boolalpha, we will receive an output that says true or false rahter than 0 and 1.

*If you want to switch back to 0 and 1, you can use noboolalpha to do so *