Notes week of Oct 13

Muddiest points & want to learn

what i really need to know is that the float and double do they mean the same thing when it comes to decimals. for example i would like to know if i type double or float to a decimal point it does not matter

They both store decimals, the difference is in the precision (approximately the number of digits after the decimal point. Usually float is stored in 32 bits, and double is 64 bits.

Challenge homework activity 2.16.2: Fixed range of random numbers.

The given program already contains srand(seedVal), so you don’t need to seed the random number generator. You just use rand(), but then you need to force it into the range between 100 and (including) 149.

rand() returns a number between 0 and RAND_MAX. We can start by clipping it using the modulo operator (%). How many numbers are there in the desired range? Since it starts with zero (100, not 101), there are 50 numbers. So we start with

    rand() % 50

which will generate numbers in the range 0–49. So now we just add 100 to put them in the range 100–149. Here’s the final result:

    cout << rand()%50 + 100 << endl;
    cout << rand()%50 + 100 << endl;

rand() and srand() are still haunting me. I understand the concept, but have trouble executing it.

We’ll spend more time on this when we do a project that requires random numbers.

3.3.2 Challenge homework On givenYear = 1900, the program outputs “20th century” and “Long ago”

if (givenYear >= 2100)
{cout << "Distant future" << endl;}

else if (givenYear >= 2000)
{cout << "21st century" << endl;}

else if (givenYear >= 1900)
{cout << "20th century" << endl;}

else (givenYear <= 1899);
{cout << "Long ago";}

You don’t want a condition (or a semi-colon) following that last else. It should end just like this:

else
{cout << "Long ago";}

activity 3.4.2 #6 - confusing

You want a condition that is true for the smallest and largest three-digit number. The smallest three-digit number is 100 (so the given first part of the condition is num >= 100). The largest three-digit number is 999. So the next part of the condition is num <= 999. To see if the number is in that range, you want both of these to be true, so combine them using &&

is there an easy way to remember when to use single or double equal signs (= or ==)

One equal sign is for assignment. You are placing some value into a variable:

double x = 3.145;  // Declare and assign
z = 99.1143;       // Just assign value

Two equal signs are for equality. You are asking the (boolean) question, are these two values equal?

if(x == 3.142) {   // Are these equal?
  // blah blah
}

homework 3.6.1 - no idea what i’m doing here (bool in general)

  • i get confused between when certain words are used as variables vs when they are actually implemented in the C++ lauguage.

(ex. HW 3.7.1 & 3.7.2 - use of string compared to userString) goes in hand with the whole = vs. ==

" “3.6.2

3.7.1

3.7.2"

activity 3.6.1: Using bool

how to use booleans when coding