-
What output do you get from the statement:
cout << "H" << "el" << 'l' << 'o' << endl;
-
Hello
-
H el l o
-
"H" << "el" << 'l' << 'o' << endl
-
What output do you get from the statement:
cout << "0.500 = " << 0.500 << endl;
-
0.500 = 0.500
-
0.5 = 0.5
-
0.500 = 0.5
-
What is the difference in output between the program segments
on the left and on the right:
cout << "Line 1\nLine 2\n"; cout << "Line 1\n";
cout << "Line 2\n";
-
Everything is printed on the same line by the left segment,
on two separate lines for the right.
-
The left segment single-spaces two lines, the right segment
double-spaces them.
-
There is no difference.
-
What is the difference in output between the program pieces on
the left and on the right:
cout << "Line 1\n"; cout << "Line 1" << endl
cout << "Line 2\n"; << "Line 2" << endl;
-
The Left segment single-spaces two lines, the right segment
double-spaces them.
-
There is no difference.
-
The segment on the right will not compile: the cout
is missing.
-
What should you get from the statement:
cout << "\a" << "Hi" << "there" << endl;
(Does not work on the PCs, but works on Unix.)
-
The program writes on the screen \a Hi there
-
The program writes on the screen \aHithere
-
The program writes on the screen Hithere
and the computer beeps.
-
Why is the program on the right much better than the one on
the left:
#include <iostream.h> #include <iostream.h>
void main(void) { void main(void) {
int a; int a;
... int b;
int b; ...
... ...
a=1; a=1;
b=3; b=3;
cout << a+b << endl; cout << a+b << endl;
... ...
} }
(The dots stand for lots of other statements that are not important.)
-
All declarations of variables must be at the start.
-
It is easier for someone to find out about a variable such
as b if all declarations are grouped together.
-
What is obviously wrong (except lack of neatness):
#include <iostream.h>
void main(void) {
int pi;
...
pi = 3.141593;
...
}
-
There should be a semi-colon at the end of the
include line.
-
The assignment statement pi = 3.141593; will crash.
-
Variable pi is set to 3, not to 3.141593
-
Why is the program on the left much better than the one on the
right:
#include <iostream.h> #include <iostream.h>
void main(void) { void main(void) {
float pi = 3.141593; float pi;
.... ....
.... pi=3.141593
.... ....
area = pi*r*r area = pi*r*r
.... ....
-
It is easier to check whether variable pi has been
given the right value.
-
Computers cannot store many lines of code. You worry about
every line.
-
If we give pi a value early, it will be sure to have
received the value by the time we reach the area=
statement.
-
Why is the comment on the left much better than the one on the
right:
#include <iostream.h> #include <iostream.h>
void main(void) { void main(void) {
// Declarations: // Declarations:
// Define pi: float pi = 3.141593; // pi is defined
float pi = 3.141593; ....
.... ....
-
You cannot put comments behind statements, they must be on
separate lines.
-
By the time the reader reads the comment in the program on
the right, he/she is either mystified or already has figured the
statement out by himself.
-
Which people can benefit from the comments that you put in
your program?
-
You right now.
-
You, when you try to reuse the program a year from now.
-
Someone who wants to use your program but does not know
exactly what it does.
-
Someone who wants to modify your program for a similar task.
-
All of the above.
-
Can you store arbitrarily large integers in an int? In a long int?
-
Yes. Yes.
-
No, there are limitations on the size. Yes.
-
No. No, but you can store larger integers.
-
Can you store arbitrarily large real numbers in a float?
In a double?
-
Yes. Yes.
-
No, there are limitations on the size. Yes.
-
No. No, but maybe you can store larger real numbers.
-
Can you store arbitrarily accurate floating point numbers such
as $\pi$ or $\sqrt 2$ in a float? In a double?
-
Yes. Yes.
-
No, there are limitations on the number of digits behind the
first nonzero one that are going to be stored. Yes.
-
No. No, but you can store more digits.
-
What do you get from:
cout << "3*3 = " << 3*3 << endl;
-
3*3 = 3*3
-
9 = 9
-
3*3 = 9
-
What do you get from:
float a; ...; a=1.5; cout << a << endl;
-
0
-
1
-
1.5
-
What do you get from:
int a; ...; a=1.5; cout << a << endl;
-
0
-
1
-
1.5
-
What do you get from:
cout << ".33 = " << 1/3 << endl;
-
.33 = 0
-
.33 = .33
-
.33 = .3333333
-
What do you get from:
cout << ".33 = " << 1./3 << endl;
-
.33 = 0
-
.33 = .33
-
.33 = .3333333
-
What do you get from:
cout << "Large: " << 300000*300000 << endl;
-
Large: 300000*300000
-
Large: 90000000000
-
Neither
-
What do you get from:
cout << "9 = " << 1+2*3 << endl;
-
9 = 1+2*3
-
9 = 9
-
9 = 7
-
What do you get from:
cout << ".25 = " << 1./2.*2. << endl;
-
.25 = 1./2.*2.
-
.25 = .25
-
.25 = 1.
-
What do you get from:
cout << "1 = " << 1./2./2. << endl;
-
1 = 0
-
1 = .25
-
1 = 1.
-
What do you get from:
int i; ...; i=1; cout << i++; cout << i << endl;
-
11
-
1 1
-
12
-
What do you get from:
int i; ...; i=1; cout << ++i; cout << i << endl;
-
11
-
2 1
-
22
-
What happens during the execution of:
int i,j; ...; cin >> i >> j;
-
The program prints out ij.
-
The program prints out the values of variables i and
j
-
The program waits for you to type in ij.
-
The program waits for you to type in i j.
-
The program pauses at the cin statement until you
enter two integer values from the keyboard. It stores these in
variables i and j.