A few brief beginner's questions

1. It says that cout of this code is 1 0 1. But can I get an explanation why the program's cout is 1 0 1?

1
2
3
4
5
6
int num = 45;
cout<<( ((float) num/2) > (num/2) )<<endl;
num++;
cout<<( ((float) num/2) > (num/2) )<<endl;
num++;
cout<<( ((float) num/2) > (num/2) )<<endl; 


2. Write a C++ program that given any letter (uppercase or lowercase) it will display the same letter as lowercase.
How am I supposted to do this program? For example, if I enter A, it enters a. But if I enter b, it enters b. (always show the lowercase letter)

3. Cutting a cube
Is it possible to cut a large cube with side 13 into 2012 smaller cubes with
sides 1, 2 or 3 units?

I'd be grateful for the solutions. :)
In your cout the program is testing if expression 1 is greater than expression 2.

If true it prints 1 and if false it prints 0.

the first test is 45/2 = 22.5 and 45/2 = 22 (one is float and the other is int) so this one is true.

Program now adds 1 to number = 46

The second test is 46/2 = 23 and 46/2 = 23. so this one is false.

Program adds 1 to number again -> number = 47

the third test is 47/2 = 23.5 and 47/2 = 23, so one is true.

Therefore your output is:

1
0
1

Hope this helps. If you don't understand the calculations you should read about '/' operator for int and float.
1. What actually gets printed is a bool i.e. true or false, which by default show as 1 and 0, respectively.

Why bool? That is what relational operator > returns.

Integer division keeps only the integer part and thus 3/2 is 1.

2. http://www.cplusplus.com/reference/cctype/tolower/ but that is boring and not as instructive as doing it self. Adapt the idea of:
1
2
if ( 'b' == letter ) return 'b';
if ( 'B' == letter ) return 'b';


3. Can you solve this pair of equations:
a*1 + b*4 + c*27 = 13*13*13
a + b + c = 2012
These seem awfully homework-y, so excuse me for being a bit vague with the responses.

1) Think about the statements. What happens when you divide a float and the result is not a whole number? What happens if you do the same for an integer?

Take a look at the output for this:
1
2
3
4
5
6
7
8
9
#include <iostream>

int main( int argc, char* argv[] )
{
    int num = 45;
    std::cout << "Float: " << ( float( num ) / 2 ) << std::endl;
    std::cout << "Int  : " << num / 2 << std::endl;
    return 0;
}


2) Take a look at the functions provided in the standard C++ library. Namely tolower, found in the cctype header. http://www.cplusplus.com/reference/cctype/tolower/

3) I don't understand this. A cube with side 13? What does that mean?
Last edited on
Topic archived. No new replies allowed.