int main() {
int hour = 7;
int min = 50;
cout << "The current time is: " << endl;
cout << hour;
cout << ":"; cout << minute;
cout << endl;
cout << "I'm going to be late for my 8am!";
}
6
There are 6 cout statements, but that doesn’t mean there are 6 lines of output!
5
There are 5 lines of cout statments, but that doesn’t mean there are 5 lines of output!
3
Even though there are 6 cout staments written on 5 lines, there are only 3 lines of output in the terminal.
2
There are 2 endl statements. But what happens when you have more output after the endl?
0! There is an error!
Everything is syntacticly legal! You can have cout statements on multiple lines of code that have one line of output… or you can have multiple cout statements on one line of code that have multiple lines of output!
Your math teacher just gave an exam that had all of the students panicking. Four students decide to share their scores to see who did the best. At the end of the program’s execution, who has the highest score on the exam?
int main() {
int Regina = 6 * (3 + 2) / 100;
int Gretchen = (3 + 5) * 6 / 100;
int Karen = 6 * 3 + 2 / 100;
int Cady = (3 * 5) * 6 / 100;
}
Regina
Using the order of operations we have Regina scoring 30 / 100.
Gretchen
Using the order of operations we have Gretchen scoring 48 / 100.
Karen
6 * 3 = 18, and 18 + 2 / 100 = 18 due to integer division. Believe it or not, due to the order of operations and integer division, Karen ended up with the highest “score” at the end of the program’s execution.
Cady
Using the order of operations we have Mathlete Cady scoring 90 / 100. this would be the highest score… if it weren’t for integer division.
They all got 0’s.
Integer division rounds the quotient down to the nearest integer. Take a closer look at what is being divided on each line, because not everyone recieved a zero!
Suppose you want to find the volume of a cone. For reference, the formula is V = 1/3 pi * r^2 * h. For the sake of this question, we will use pi = 3.14. What is wrong with the following code?
double volume(r, h) {
return 1/3 * 3.14 * r * r * h;
}
semantic error
With integer division, 1 / 3 becomes 0. Multiplying 0 by the rest of the expression will always return 0, which is not what you want your program to do!
syntax error
There is nothing wrong with the structure of your program.
run-time error
There are no errors that will surface at run-time.
You can’t calculate and return on the same line!
You actually can, this is called composition.
Nothing! There is not an error.
This formula will return a volume, but is it correct?