Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

C++ Primer ex 7.5

 
 
arnuld
Guest
Posts: n/a
 
      08-10-2007
as usual , advices for improvement:


/* C++ Primer - 4/e
*
* exercise 7.5
* STATEMENT:
* write a funtion that take an int and a pointer to an int and
return the larger of the values. What type should you use for the
pointer ?
*/


#include <iostream>
#include <cassert>

/* we will use pointer to a const because we are interested in
only reading the values */
int return_bigger( int i, const int* ip) {
if( i > *ip )
{
return 1;
}
else if( i < *ip )
{
return -1;
}
else
{
return 0; /* if both vales are equal.t his will return Zero */
}
}


int main()
{
std::cout << "enter an initeger: ";
int i;
std::cin >> i;

std::cout << "enter an initeger: ";
int j;
std::cin >> j;

const int* pj = &j;

std::cout << "is "
<< i
<< " bigger than "
<< j
<< ":\t"
<< (return_bigger( i, pj ))
<< std::endl;

return 0;


}

====== OUTPUT =========
[arnuld@arch cpp] $ g++ -ansi -pedantic -Wall -Wextra ex_07-05.cpp
[arnuld@arch cpp] $ ./a.out
enter an initeger: 3
enter an initeger: 2
is 3 bigger than 2: 1
[arnuld@arch cpp] $ ./a.out
enter an initeger: -9
enter an initeger: -10
is -9 bigger than -10: 1
[arnuld@arch cpp] $ ./a.out
enter an initeger: 3
enter an initeger: 3
is 3 bigger than 3: 0
[arnuld@arch cpp] $



--
http://arnuld.blogspot.com

 
Reply With Quote
 
 
 
 
Frank Birbacher
Guest
Posts: n/a
 
      08-10-2007
Hi!

arnuld schrieb:
> /* we will use pointer to a const because we are interested in
> only reading the values */
> int return_bigger( int i, const int* ip) {


I'd place more "const":
int return_bigger(const int i, const int* const ip) {


BTW, I understand the "STATEMENT" and the function name "return_bigger"
to actually return the bigger value:

int return_bigger(const int i, const int* const ip) {
return i>*ip ? i : *ip ;
}

Frank
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-10-2007
* arnuld:
> as usual , advices for improvement:
>
>
> /* C++ Primer - 4/e
> *
> * exercise 7.5
> * STATEMENT:
> * write a funtion that take an int and a pointer to an int and
> return the larger of the values. What type should you use for the
> pointer ?
> */
>
>
> #include <iostream>
> #include <cassert>
>
> /* we will use pointer to a const because we are interested in
> only reading the values */
> int return_bigger( int i, const int* ip) {
> if( i > *ip )
> {
> return 1;
> }
> else if( i < *ip )
> {
> return -1;
> }
> else
> {
> return 0; /* if both vales are equal.t his will return Zero */
> }
> }
>
>
> int main()
> {
> std::cout << "enter an initeger: ";
> int i;
> std::cin >> i;
>
> std::cout << "enter an initeger: ";
> int j;
> std::cin >> j;
>
> const int* pj = &j;
>
> std::cout << "is "
> << i
> << " bigger than "
> << j
> << ":\t"
> << (return_bigger( i, pj ))
> << std::endl;
>
> return 0;
>
>
> }
>
> ====== OUTPUT =========
> [arnuld@arch cpp] $ g++ -ansi -pedantic -Wall -Wextra ex_07-05.cpp
> [arnuld@arch cpp] $ ./a.out
> enter an initeger: 3
> enter an initeger: 2
> is 3 bigger than 2: 1
> [arnuld@arch cpp] $ ./a.out
> enter an initeger: -9
> enter an initeger: -10
> is -9 bigger than -10: 1
> [arnuld@arch cpp] $ ./a.out
> enter an initeger: 3
> enter an initeger: 3
> is 3 bigger than 3: 0
> [arnuld@arch cpp] $


If you change your interpretation of the problem statement a little,
then you can implement the function using std::max().

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      08-10-2007
Frank Birbacher wrote:
> Hi!
>
> arnuld schrieb:
>> /* we will use pointer to a const because we are interested in
>> only reading the values */
>> int return_bigger( int i, const int* ip) {

>
> I'd place more "const":
> int return_bigger(const int i, const int* const ip) {
>

no need here I think, as the pointer variable `ip' is a local variable
in the function `return_bigger' scope, change what `ip' points to has
no effect on the outside of this function, so const pointer or not is
dependent on the function `return_bigger' itself.
>
> BTW, I understand the "STATEMENT" and the function name "return_bigger"
> to actually return the bigger value:
>
> int return_bigger(const int i, const int* const ip) {
> return i>*ip ? i : *ip ;
> }
>
> Frank

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-10-2007
> On Fri, 10 Aug 2007 13:21:35 +0200, Frank Birbacher wrote:

> BTW, I understand the "STATEMENT" and the function name "return_bigger"
> to actually return the bigger value:
>
> int return_bigger(const int i, const int* const ip) {
> return i>*ip ? i : *ip ;
> }
>
> Frank


WOW Frank.... so compact, so smart way. i will use it


--
http://arnuld.blogspot.com

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      08-10-2007
> On Fri, 10 Aug 2007 13:31:38 +0200, Alf P. Steinbach wrote: If you
> change your interpretation of the problem statement a little, then you
> can implement the function using std::max().


i knew someone will mention it. i love to use standard library algorithms
and Templates and i do not possess any knowledge of them yet. i am at
chapter 7 and whn i try to read standard library's Templates and
Algorithms from Stroustrup, my head hurts

may be i need more experience with C++ and i am determined to have it


(BTW, if after 1 or 2 years, i will get to teach someone C++ theni surely
wil teach im 1st about std::max, std:stream 1st rathe rthan do-while or
if, just my opinion)

--
http://arnuld.blogspot.com

 
Reply With Quote
 
Frank Birbacher
Guest
Posts: n/a
 
      08-10-2007
Hi!

Barry schrieb:
> Frank Birbacher wrote:
>> I'd place more "const":
>> int return_bigger(const int i, const int* const ip) {
>>

> no need here I think, as the pointer variable `ip' is a local variable
> in the function `return_bigger' scope, change what `ip' points to has
> no effect on the outside of this function, so const pointer or not is
> dependent on the function `return_bigger' itself.


Correct. It's the same with "i". Well, "void foo(int)" and "void
foo(const int)" are even the same signature. It is referred to as "top
level const". And "top level const" does not change the signature. All
it does is to help you not to shoot yourself in the foot. If you do not
intend to deliberately change the parameter value (that is "i" or "ip",
not "*ip") you should make it "const".

Frank
 
Reply With Quote
 
Frank Birbacher
Guest
Posts: n/a
 
      08-10-2007
Hi!

Alf P. Steinbach schrieb:
> then you can implement the function using std::max().


Of course. But I think that is not the point of the excercise. Otherwise
the STATEMENT could be "Find a std function which returns the bigger of
two values." (yes, std::max is not bigger_value, but that's not my
point). Same with the other excercises.

Frank
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      08-10-2007
Frank Birbacher wrote:
> Hi!
>
> Barry schrieb:
>> Frank Birbacher wrote:
>>> I'd place more "const":
>>> int return_bigger(const int i, const int* const ip) {
>>>

>> no need here I think, as the pointer variable `ip' is a local variable
>> in the function `return_bigger' scope, change what `ip' points to has
>> no effect on the outside of this function, so const pointer or not is
>> dependent on the function `return_bigger' itself.

>
> Correct. It's the same with "i". Well, "void foo(int)" and "void
> foo(const int)" are even the same signature. It is referred to as "top
> level const". And "top level const" does not change the signature. All
> it does is to help you not to shoot yourself in the foot. If you do not
> intend to deliberately change the parameter value (that is "i" or "ip",


IMHO
yep, it's trivial, the formal param in the function works as two actor :
receive the passed-in value, also as an ordinary variable.

void f(int i) {
std::cout << i << std::endl;
// though we don't change i's value , we seldom make it const
}

IMHO
it's more sophisticated to make `i' const if the function is long enough
to remind the author that this i is not alterable within the function body


> not "*ip") you should make it "const".
>

 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      08-10-2007
Frank Birbacher wrote:
> Hi!
>
> Barry schrieb:
>> Frank Birbacher wrote:
>>> I'd place more "const":
>>> int return_bigger(const int i, const int* const ip) {
>>>

>> no need here I think, as the pointer variable `ip' is a local variable
>> in the function `return_bigger' scope, change what `ip' points to has
>> no effect on the outside of this function, so const pointer or not is
>> dependent on the function `return_bigger' itself.

>
> Correct. It's the same with "i". Well, "void foo(int)" and "void
> foo(const int)" are even the same signature. It is referred to as "top


Same signature?
I have no idea here

I don't know what exactly function signature is defined.

You meant they are the same maybe it's because you can't have overload
them at the same time.
But their `typeinfo' are not the same


> level const". And "top level const" does not change the signature. All
> it does is to help you not to shoot yourself in the foot. If you do not
> intend to deliberately change the parameter value (that is "i" or "ip",
> not "*ip") you should make it "const".
>

 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57