#include <iostream> // need this header file to support the C++ I/O system
using namespace std; // telling the compiler to use namespace "std",
// where the entire C++ library is declared.
// function declarations - needed for the program to compile
int min_ii (int n, int m);
int min_pi (int *p, int * q);
int * min_pp (int *p, int * q);
int main()
{
int n = 6, m = 5;
// passing two integers to a function
int min = min_ii(n , m);
cout << min << endl;
// passing addresses of n and m to a function
min = min_pi(&n, &m);
cout << min << endl;
// passing addresses of n and m to a function
int * min_p = min_pp(&n, &m);
cout << *min_p << endl;
*min_p = *min_p - 1;
// changing m via a pointer
cout << m << endl;
// call to swap GOES HERE
cout << "m = " << m << " n = " << n << endl;
return 0;
}
// passing pointers to functions, returning a pointer
int * min_pp (int *p, int * q) {
// fill in the code for this function
}
// passing two integers to a function, returning an int
int min_ii (int n, int m) {
// fill in the code for this function
}
// passing pointers to functions, returning an int
int min_pi (int *p, int * q) {
// fill in the code for this function
}
g++
myprogram.cpp
(replace myprogram by the actual program
name). To run it, type ./a.out
, this will run the
executable produced by the compiler. Note: as is, the program doesn't
compile since the functions don't return the right type.
min_ii
takes two integers and returns the smaller of
the two integersmin_pi
takes two pointers to integers and returns the
value of the smaller of the two integersmin_pp
takes two pointers to integers and returns a
pointer to the smaller of the two integers. Changing the variable at
the address returned by the function changes (in this case) the
variable m - the smaller of m and n.Note for those familiar with C++: while there is a different syntax for passing parameters to a function by reference, please use the C style (explicit pointers), as defined in the function prototypes.
Question 2 Write a void function swap
to swap the
values of two integers in the program. Use this function to swap the
values of m and n. Don't forget to add the function prototype in the
beginning of the program.