A
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] $
/* 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] $