Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Primer ex 7.3

Reply
Thread Tools

C++ Primer ex 7.3

 
 
arnuld
Guest
Posts: n/a
 
      08-09-2007
i wanted to know if that's a good design style (upto the knowledge i have
gained till chpater 7):

/* C++ Primer - 4/e
*
* exercise 7.4
* STATEMENT
* write a programme to take two int paramaters and generate the
result of raising the first to the power of the second. write a
programme to call your function passing it two ints. verify the
result.
*
*/

#include <iostream>

int raise_power(int x, int y)
{
int mult = 1;

while( y )
{
mult = mult * x;
--y;
}

return mult;
}

int main()
{
int i = 2;
int j = 3;
int mult_result = 8;

std::cout << static_cast<bool>(mult_result == raise_power(i, j))
<< std::endl;
/* well, never got converted to "true", it alwasy remains 1 */
return 0;
}




--
http://arnuld.blogspot.com

 
Reply With Quote
 
 
 
 
int2str@gmail.com
Guest
Posts: n/a
 
      08-09-2007
On Aug 8, 9:59 pm, arnuld <(E-Mail Removed)> wrote:
> i wanted to know if that's a good design style (upto the knowledge i have
> gained till chpater 7):
>
> /* C++ Primer - 4/e
> *
> * exercise 7.4
> * STATEMENT
> * write a programme to take two int paramaters and generate the
> result of raising the first to the power of the second. write a
> programme to call your function passing it two ints. verify the
> result.
> *
> */
>
> #include <iostream>
>
> int raise_power(int x, int y)


I would suggest using more meaningful variable names other than 'x'
and 'y'. 'base' and 'exponent' come to mind.

> {
> int mult = 1;
>
> while( y )


If 'y' is a negative number (which is allowed since it's defined as an
int), this while condition will cause a major bug n your program.

> {
> mult = mult * x;
> --y;


Your while condition is wrong. But if it was correct, you could have
simply written "while( y-- )" to achieve the same result without the
stand-alone decrement.

> }
>
> return mult;
>
> }
>
> int main()
> {
> int i = 2;
> int j = 3;
> int mult_result = 8;
>
> std::cout << static_cast<bool>(mult_result == raise_power(i, j))
> << std::endl;
> /* well, never got converted to "true", it alwasy remains 1 */
> return 0;
>
> }
>
> --http://arnuld.blogspot.com



Cheers,
Andre

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-09-2007
arnuld wrote:
> i wanted to know if that's a good design style (upto the knowledge i have
> gained till chpater 7):
>
> /* C++ Primer - 4/e
> *
> * exercise 7.4
> * STATEMENT
> * write a programme to take two int paramaters and generate the
> result of raising the first to the power of the second. write a
> programme to call your function passing it two ints. verify the
> result.
> *
> */
>
> #include <iostream>
>
> int raise_power(int x, int y)
> {
> int mult = 1;
>
> while( y )
> {
> mult = mult * x;
> --y;
> }
>
> return mult;
> }
>

That's OK for positive numbers, but what about negative?

> int main()
> {
> int i = 2;
> int j = 3;
> int mult_result = 8;
>
> std::cout << static_cast<bool>(mult_result == raise_power(i, j))
> << std::endl;


Be brutal,

assert( raise_power(i, j) == mult_result );

Don't forget to include <cassert>

--
Ian Collins.
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-09-2007
> On Aug 9, 10:14 am, Ian Collins <(E-Mail Removed)> wrote:
> > arnuld wrote:


> > ...[SNIP]...


> > std::cout << static_cast<bool>(mult_result == raise_power(i, j))
> > << std::endl;



> Be brutal,
>
> assert( raise_power(i, j) == mult_result );
>
> Don't forget to include <cassert>


that raises an error:

/home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
ex_07-03.cpp
ex_07-03.cpp: In function 'int main()':
ex_07-03.cpp:32: error: no match for 'operator<<' in 'std::cout <<
((raise_power(i, ((unsigned int)j)) != mult_result) ?
__assert_fail(((const char*)"mult_result == raise_power(i, j)"),
((const char*)"ex_07-03.cpp"), 32u, ((const char*)(&
__PRETTY_FUNCTION__))) : 0)'
/usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/
ostream:112: note: candidates are: std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT,
_Traits>:perator<<(std::basic_ostream<_CharT, _Traits>& (*)
(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits =
std::char_traits<char>]


line 32, is where i use "assert".

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-09-2007
> On Aug 9, 10:21 am, arnuld <(E-Mail Removed)> wrote:
> that raises an error:
>
> /home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
> ex_07-03.cpp
> ex_07-03.cpp: In function 'int main()':
> ex_07-03.cpp:32: error: no match for 'operator<<' in 'std::cout <<
> ((raise_power(i, ((unsigned int)j)) != mult_result) ?
> __assert_fail(((const char*)"mult_result == raise_power(i, j)"),
> ((const char*)"ex_07-03.cpp"), 32u, ((const char*)(&
> __PRETTY_FUNCTION__))) : 0)'
> /usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/
> ostream:112: note: candidates are: std::basic_ostream<_CharT,
> _Traits>& std::basic_ostream<_CharT,
> _Traits>:perator<<(std::basic_ostream<_CharT, _Traits>& (*)
> (std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits =
> std::char_traits<char>]
>
> line 32, is where i use "assert".


sorry, forgot to post code:

std::cout << assert(mult_result == raise_power(i, j))
<< std::endl;


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-09-2007
arnuld wrote:
>> On Aug 9, 10:21 am, arnuld <(E-Mail Removed)> wrote:
>> that raises an error:
>>
>> /home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
>> ex_07-03.cpp
>> ex_07-03.cpp: In function 'int main()':
>> ex_07-03.cpp:32: error: no match for 'operator<<' in 'std::cout <<
>> ((raise_power(i, ((unsigned int)j)) != mult_result) ?
>> __assert_fail(((const char*)"mult_result == raise_power(i, j)"),
>> ((const char*)"ex_07-03.cpp"), 32u, ((const char*)(&
>> __PRETTY_FUNCTION__))) : 0)'
>> /usr/lib/gcc/i686-pc-linux-gnu/4.2.1/../../../../include/c++/4.2.1/
>> ostream:112: note: candidates are: std::basic_ostream<_CharT,
>> _Traits>& std::basic_ostream<_CharT,
>> _Traits>:perator<<(std::basic_ostream<_CharT, _Traits>& (*)
>> (std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits =
>> std::char_traits<char>]
>>
>> line 32, is where i use "assert".

>
> sorry, forgot to post code:
>
> std::cout << assert(mult_result == raise_power(i, j))
> << std::endl;
>
>

No, just use what I wrote.

--
Ian Collins.
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-09-2007
> On Aug 9, 11:12 am, Ian Collins <(E-Mail Removed)> wrote:

> No, just use what I wrote.



i did use that, now i just modified it to:

std::cout << std::assert( raise_power(i, j) == mult_result )
<< std::endl;

and the error is:

ex_07-03.cpp:32: error: expected unqualified-id before '(' token


what now ?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-09-2007
arnuld wrote:
>> On Aug 9, 11:12 am, Ian Collins <(E-Mail Removed)> wrote:

>
>> No, just use what I wrote.

>
>
> i did use that, now i just modified it to:
>
> std::cout << std::assert( raise_power(i, j) == mult_result )
> << std::endl;
>
> and the error is:
>
> ex_07-03.cpp:32: error: expected unqualified-id before '(' token
>

Don't output it, just use the assert to validate your test. If the
assertion fails, your program will abort with an error message.

--
Ian Collins.
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-09-2007
On Aug 9, 11:59 am, arnuld <(E-Mail Removed)> wrote:

> i did use that, now i just modified it to:
>
> std::cout << std::assert( raise_power(i, j) == mult_result )
> << std::endl;
>
> and the error is:
>
> ex_07-03.cpp:32: error: expected unqualified-id before '(' token
>
> what now ?


ok i got it. "assert" is a preprocessor-macro that does not return
anything and hence causes conflict with the overloaded operator "<<"

but then i have to write more-code:

assert( raise_power(i, j) == mult_result );
std::cout << ( raise_power(i, j) == mult_result ) << std::endl;

but then, is writing more code a good idea ? (when i know that
expression works fine)

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-09-2007
arnuld wrote:
> On Aug 9, 11:59 am, arnuld <(E-Mail Removed)> wrote:
>
>> i did use that, now i just modified it to:
>>
>> std::cout << std::assert( raise_power(i, j) == mult_result )
>> << std::endl;
>>
>> and the error is:
>>
>> ex_07-03.cpp:32: error: expected unqualified-id before '(' token
>>
>> what now ?

>
> ok i got it. "assert" is a preprocessor-macro that does not return
> anything and hence causes conflict with the overloaded operator "<<"
>
> but then i have to write more-code:
>
> assert( raise_power(i, j) == mult_result );
> std::cout << ( raise_power(i, j) == mult_result ) << std::endl;
>
> but then, is writing more code a good idea ? (when i know that
> expression works fine)
>

No, the point was if your test passes, the program exits cleanly, if it
fails it aborts and you get an error message.

you could just write

assert( raise_power(i, j) == mult_result );
std::cout << "OK" << std::endl;

You can add more assertions,

assert( raise_power(2, 2) == 4 );

and so on.

I had assumed your book would have covered assert!

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SVG primer Captain Dondo HTML 1 01-22-2006 02:08 PM
HTPC Primer, Part I - Video and Audio Silverstrand Front Page News 0 10-21-2005 01:28 PM
PC TV Tuner Primer Silverstrand Front Page News 0 10-19-2005 01:47 PM
Primer needed: Java & Windows Security Chris Java 0 04-18-2005 09:08 PM
SSL / authentication primer Richard Java 2 07-25-2003 02:08 PM



Advertisments