Midterm Review for CISC-1600-E01

  1. Set up a minimal C++ program.
    1. A computer is a machine that follows instructions,
      and the list of instructions you put into the computer is called a program.
    2. Why do we say the #includes? What would go wrong if we didn’t?
      (The #includes are not needed for words that are built-in to the C++ language, such as int and for.
      Needed for words that are not built-in to the C++ language, such as cout and cin, EXIT_SUCCESS and EXIT_FAILURE.)
    3. Why do we say using namespace std;? What would go wrong if we didn’t?
      (See dime1.C vs. dime4.C.)
    4. Why do we return EXIT_SUCCESS or EXIT_FAILURE?
    5. Every statement in C++ ends with a semicolon, just as every sentence in English ends with a period (or exclamation point or question mark). Why don’t we end every statement in C++ with a period to make it more like English?
    6. Indent every statement by a number of tabs that is equal to the number of pairs of {cury braces} that enclose the statement.
      The } of a for loop is directly below the f. See for2.C.
      The } of an if statement is directly below the i. See if.C.
      The } of main (at the end of the program) is directly below the { of main (at the top of the program). See dime1.C.
  2. Compile and execute a C++ program on our machine storm.cis.fordham.edu.
    1. Use wget to download a C++ program from the web into your home directory on storm.cis.fordham.edu.
      What does the w stand for?
    2. Use c++ to translate a C++ program into the language of 1’s and 0’s that the computer actually understands.
      What file does this command create?
    3. Execute a C++ program on storm.cis.fordham.edu and send the program’s output to the screen.
      What does the ./ in front of the a.out mean?
      jsmith@storm:~$  cd
      jsmith@storm:~$  ./a.out
      
    4. Use > to execute a C++ program on storm.cis.fordham.edu and send the program’s output to a file in your home directory.
      jsmith@storm:~$  cd
      jsmith@storm:~$  ./a.out > filename.txt
      
  3. Output with cout.
    1. A string of characters is in "double quotes", not 'single quotes'. See dime1.C.
    2. Within the double quotes, the invisible newline character has a backslash (\n), not a forward slash (/n), and a lowercase n, not an uppercase N.
    3. Output two or more items with one cout statement.
      Output the contents of a variable.
      	cout << price << "\n";
      
  4. Variables.
    1. In a single statement, we can
      1. Define a variable (create the variable)
      2. Declare the variable (announce the name and data type of the variable)
      3. Initialize the variable (put a value into the variable at the moment when the variable is born)
      For example,
      	int i {10};
      	double price {29.95};
      
    2. Do not put unpredictable garbage into a variable when the variable is born.
      	double price;   //BAD: puts unpredictable garbage into the variable.
      
    3. How long does a variable, once created, remain in existence?
      1. A variable created within the {curly braces} of main lives until we return from main (with either EXIT_SUCCESS or EXIT_FAILURE), with the following exception.
      2. A variable created within the parentheses of a for loop lives only as long as we are going around and around the loop.
        	for (int i {0}; i < 10; ++i) {
        		cout << i << "\n";
        	}
        
        	//i no longer exists when we get to this point.
        
      3. A variable created within the parentheses of a “range for loop” is reborn every time around the loop.
    4. Use an operator to change the contents of an existing variable. For example,
      	i = 10;     //assignment with a single, not a double, equal sign
      	i += 2;
      	++i;
      	cin >> i;
      
  5. Perform input in five steps.
    1. Prompt the user with a question.
    2. Create a variable to receieve the input.
    3. Attempt to draw input from cin into the variable.
    4. Check if the input was successful. If not,
      1. Send an error message to cerr.
      2. Terminate the program prematurely with an exit status of EXIT_FAILURE.
    5. Check if the value that the user input was valid. If not,
      1. Send an error message to cerr.
      2. Terminate the program prematurely with an exit status of EXIT_FAILURE.
    	cout << "In what year were you born?\n";
    	int year {0};
    	cin >> year;
    
    	if (!cin) {
    		cerr << Sorry, that wasn't a number.\n";
    		return EXIT_FAILURE;
    	}
    
    	if (year < 1924 || year > 2024) {   //The || means "or".
    		cerr << "Sorry, you could not have been born then.\n";
    		return EXIT_FAILURE;
    	}
    
    Why doesn’t this attempt at input compile with c++?
  6. Data types: the two most important ones that we’ve seen are int vs. double.
    1. When you perform arithmetic with ints, the result is an int.
      For example, the value of the expression i+j is of data type int.
    2. When you perform arithmetic with doubles, the result is a double.
      For example, the value of the expression d+e is of data type double.
    3. You need to know the data type of an expression, because some operators (e.g., division) give you different results depending on this data type.
      	//i and 3 are ints, so the expression i/3 is also an int.
      	//The value of this expression is therefore 3
      	int i {10};
      	cout << i/3 << "\n";
      
      	//d and 3.0 are doubles, so the expression d/3.0 is also a double.
      	//The value of this expression is therefore 3.33333
      	double d {10.0};
      	cout << d/3.0 << "\n";
      
  7. Expressions
    1. An expression is made of operators and operands.
    2. The operators are things like +     -     *     /     %     =     <<
    3. The operands can be variables (i     j     k) or literals (10     20     30.75).
    4. How many operands can an operator have?
      1. Some operators are unary: -i
      2. Most operators are binary: i+j
      3. One operator we haven’t seen yet is ternary: a?b:c
    5. Division, quotient, remainder. Break a large quantity into a quotient and remainder using int operations:
      	int pennies {613};             //the large quantity
      	int dollars {pennies / 100};   //quotient
      	int cents   {pennies % 100};   //remainder
      
    6. Operators that can change the value of a variable. For example, +=     *=     ++     >>
    7. Operator precedence: a+b*c
    8. Operator associativity: 1-2-3     z = y = x
    9. Typical question: how many operators are in the following expression? In what order are they executed, and why?
      i = 10 * j
      i = (10 * j) + 20
      cout << 10 + 20 << "\n"
  8. Control Structure
    1. while loops (will not be a big topic on the midterm)
    2. for loops, counting up and down (e.g., bottles of beer), counting by ones or by tens. We’ve done four things with for loops (see below).
    3. Nested for loops:
      	//Output a 10 by 10 square of X's.
      	//i is the row number, j is the column number.
      
      	for (int i {0}; i < 10; ++i) {
      		for (int j {0}; j < 10; ++j) {
      			cout << "X";
      		}
      		cout << "\n";
      	}
      
    4. if statements
      1. Three pairs of comparison operators in the expression in the parentheses of an if statement:
        == and !=
        < and >=
        > and <=
        Know the difference between the operators = and ==.
      2. Combine if statments with else into a chain of else/ifs.
        	if (n > 0) {
        		cout << "The number is positive.\n";
        	} else if (n < 0) {
        		cout << "The number is negative.\n";
        	} else {
        		cout << "The number is zero.\n";   //if none of the above
        	}
        
      3. Combine if statments by nesting (and1.C and and2.C) just as we can combine for statements by nesting (lucy2.C).
  9. Arrays will not be on the midterm, because we haven’t covered them yet.

Four tasks we’ve done with for loops

	//Output the same thing over and over.

	for (int i {0}; i < 10; ++i) {
		cout << "X";
	}

	cout << "\n";
	//Output the series of values that we're looping through.

	for (int i {0}; i < 10; ++i) {
		cout << i << "\n";
	}
	//Add up the series of values that we're looping through.

	int sum {0};

	for (int i {0}; i < 10; ++i) {
		sum += i;     //means sum = sum + i
	}

	cout << "The total is " << sum << ".\n";
	//Multiply a value by a certain factor over and over again.

	double money {1000.00};

	for (int i {0}; i < 10; ++i) {
		money *= factor;     //means money = money * factor
	}

	cout << "The money has grown to " << money << ".\n";