[cs13001] Questions in L04.

Mikhail Nesterenko mikhail at cs.kent.edu
Sun Sep 9 22:12:27 EDT 2012


> I wanted to ask a few questions regarding last lectures' slideshow.
> In the 4th slide, it states that a variable can hold one character,
> but can't a variable also store a word like "Pizza"? Or is "Pizza"
> considered a single character in this case?

So far, the only variable type that can hold characters that we
studied is _char_. It can hold a single character.  "Pizza" is a
string of characters, so a variable of type _char_ cannot hold it. To
be more specific, "Pizza" is a literal string constant.  However,
further in the course we will study variables, called string
variables, that can hold an entire string of characters. Just not
yet.


> Also, when I write the example codes in the slides they don't work,
> I have to add " using namespace std; " right below " #include
> <iostream> ", why is that? Is it because " using namespace std "
> pulls something from one of the libraries?

Yes, you have to add

   using namespace std;

to your programs. It is a mechanism of merging multi-file programs
into a single project. I'll talk about it mid-way through the course.

> In slide 13, it says that if we assign a double expression to an
> integer variable, it's fractional part is dropped, and vice
> versa. But isn't what happens in the example the opposite?

> double y = 2.7;
> int i = 15;
> int j = 10;
> i = y;                 // is now 2
> cout << i << endl;
> y = j;                 // y is now 10.0
> cout << y << endl;

> Shouldn't i be = 2.0 , 

No, _i_ is integer and cannot hold fractional part.

> and shouldn't Y be = 10? 

No. Likewise _y_ is a double, so before 10 (the value of _j_) is assigned
to _y_, a zero fractional part is added.


> Also, in the final slide, when I executed desiredNumber =
> desiredNumber + 5; it always returned 2686797, why that number in
> particular? Does it have a specific meaning to C++?

The reason you get this number is because _desiredNumber_ is not
initialized. That is, it is not explicitly assigned a value. For
example, with an assignment operator. However, in C++, every variable
holds a value. If a variable is not initialized, it holds an arbitrary
value. In your case, _desiredNumber_ held 2686792. After you added 5 to
it, it became 2686797. There is no significance to this number. If you
try running your program on a different computer, the number may be
different.

> Finally, how can we give a value to cout in the last slide? Isn't it
> a keyword for C++ like "while" and "int"?

_cout_ is a console output stream defined in _iostream_ include
file. It is not a variable. It cannot be given a value. It can,
however, be used to output information to the console with the
insertion operator.

Thanks,
-- 
Mikhail


More information about the cs13001 mailing list