quiz answers 1. show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

18
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout << a + b * c << endl; 11 2. Give an expression in C++ that is true when the variable age is between 0 and 100, inclusive. 0 <= age && age <= 100

Upload: reynard-holland

Post on 03-Jan-2016

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Quiz Answers

1. Show the output from the following code fragment:

int a = 5, b = 2, c = 3;cout << a + b * c << endl; 112. Give an expression in C++ that is true when the

variable age is between 0 and 100, inclusive.

0 <= age && age <= 100

Page 2: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Quiz Answers (cont'd)

3. What is a data object and what is a variable? A data object is a box that holds a value. A

variable is a label for the box.

4. What is the value of the expression 5/3 and the

value of the expression 5.0/3.0?

1 1.6666...

Page 3: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Quiz Answers (cont'd)

5. Give the syntax for the if-else statement. if (<Boolean-expression>) <statement> else <statement>

6. What's wrong with the following code fragment? int a, b; cout << a + b << endl;

The variables aren't initialized.

Page 4: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

More on Boolean Expressions

Simple Boolean (true/false) expressions are built by using relational operators: <, <=, >=, >, ==, !=

More complex ones can be built by combining simple expressions with Boolean operators: &&, ||, and !.

The && operator is “and.” Both operands must be true. The || operator is “or.”

Page 5: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Examples of Boolean Expressions

grade is between 90 and 100, inclusive:

90 <= grade && grade <= 100

(note: 0 <= grade <= 100 won't work) guests is even and is less than 100: guests % 2 == 0 && guests < 100 myAge or brothersAge is greater than 21: myAge > 21 || brothersAge > 21

Page 6: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

More Examples

age is not between 20 and 30, inclusive: !(20 <= age && age <= 30) Also, age < 20 || age > 30 would work. a is bigger than b and bigger than c: a > b && a > c

Page 7: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Count-Controlled Loop

A count-controlled loop can be used to repeat a section of code a variable number of times, if we know the value of the variable in advance:

int n;cin >> n;for (int i = 0; i < n; i++) cout << “Hello, world!” << endl;

Page 8: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Common Pitfall

Do not put a semicolon at the end of the loop statement:

for(int i = 0; i < 10; i++); <code>

The semicolon ends the for statement and the loop

does nothing. The code following the loop will be

done only once.

Page 9: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Semicolons in C++

A semicolon changes an expression into a statement: a+b is an expression, a+b; is a statement (although it doesn't do anything).

This is commonly use with assignment. a = b + c is an assignment expression. a = b + c; is an assignment statement.

The semicolon by itself is the empty statement.

Page 10: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Nested for-loops

For-loops can be nested, that is, the code inside of one loop is another loop.

This is commonly used to process data in tables. The two loops should have different counters and

may have different tests as well.

Page 11: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Nested For-loop Example

The following code prints out the mult. table:

#include <iostream>#include <iomanip>using namespace std;

int main(void) { for(int i = 1; i <= 10; i++) { for(int j = 1; j <= 10; j++) cout << setw(3) << i * j << " "; cout << endl; }

}

Page 12: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Example Output

1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100

Page 13: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

While-Statement

The while-statement can be used to create sentinel-controlled loops (those that read until a special value) and general-condition loops (those that loop until while some conditional is true)

The syntax is: while (<Boolean-expression>) <statement>

Page 14: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

While-Statement Operation

When executing a while-statement, the test (Boolean-expression) is checked first. If it is true the statement is executed and the test is checked again. This continues until the test becomes false.

The statement should somehow change the value of the test. If it doesn't, and the test is initially true, we have an infinite loop.

Page 15: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

While-Statement Example

int n = 1;while (n < 2000) { cout << n << “ “; n = n * 2;}cout << endl;

produces the output:1 2 4 8 16 32 64 128 256 512 1024

Page 16: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Sentinel-Controlled Loop

A sentinel-controlled loop reads values in from the user and process them until a special (non-valid) value is read.

The loop then stops when the sentinel is read. The sentinel is not processed like the data. The sentinel can be a special value, such as a

negative number (assuming data is not negative).

Page 17: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Sentinel-Controlled Loop Example

To find the sum of a list of non-negative grades (ints), we could read until a negative number:

int grade, sum = 0;cin >> grade;while (grade >= 0) { sum += grade; cin >> grade;}cout << sum << endl;

Page 18: Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout

Exercise

Modify grades.cpp to print out the average grade.

The average is found by dividing the sum of the grades by the number of grades.

You will need to add a counter to count the number of grades read in.

Watch out for integer division.