sum are printed in the calling function.
//
void
sumTwo(
int
n1,
int
n2,
int
*n3)
{
*n3 =
n1 + n2;
}
/*
Please input two integer numbers here ==> 5 10
i = 5
j = 10
sum = 5 + 10 = 15
*/
17

Lesson 08 – ARRAYS and Pointers Sample Programs
//SP8_17.cpp
// Program Description
// ___________________
//
// This program invokes the function getInput() which prompts the user for
// two integer numbers and returns the pointers to these numbers. The main()
// function then invokes function sumTwo() and passes three addresses to it,
// where the sum of the first two numbers is computed and the pointer to the
// sum is returned to the function main(), where they all get printed.
// Header files used
#include
<iostream>
#include
<iomanip>
using namespace
std;
// User-defined function prototypes
void
sumTwo(
int
*n1,
int
*n2,
int
*n3);
// function sumTwo() is declared
void
getInput(
int
*n1,
int
*n2);
// function getInput() is declared
int
main(
void
)
{
int
i, j, sum;
getInput(&i, &j);
// function getInput() is invoked
sumTwo(&i, &j, &sum);
// function sumTwo() is invoked
cout <<
"\n\n\ti = "
<< i <<
"\tj = "
<< j <<
"\tsum = "
<< i <<
" + "
<< j <<
" = "
<< sum << endl;
return
EXIT_SUCCESS;
}
//
//
Function sumTwo() receives three parameters which are pointers to integer
//
variables. The function computes the sum of the first two numbers and returns a
//
pointer to their sum. The numbers and their sum are printed in the calling
//
function.
//
void
sumTwo(
int
*n1,
int
*n2,
int
*n3)
{
*n3 =
*n1 + *n2;
}
18

Lesson 08 – ARRAYS and Pointers Sample Programs
//
//
The function getInput() receives two addresses from the calling program,
//
prompts the user to get two integer numbers, and then returns two pointers
//
to these numbers to the calling function.
//
void
getInput(
int
*n1,
int
*n2)
{
cout <<
"Please input two integer numbers here ==> "
;
cin >> *n1 >> *n2;
}
/*
Please input two integer numbers here ==> 5 10
i = 5
j = 10
sum = 5 + 10 = 15
*/
19

Lesson 08 – ARRAYS and Pointers Sample Programs
//SP8_18.cpp
// Program Description
// ___________________
//
// This program
invokes the function getInput() which prompts the user for
// two integer numbers and returns the pointers to these numbers. The main()
// function then invokes function sumTwo() and passes three addresses to it,
// where the sum of the first two numbers is computed and the pointer to the
// sum is returned to the function main(). The main() function then invokes
// the function display() and passes three addresses to it. The function
// display() then uses pointers to display their values.
// Header files used
#include
<iostream>
using namespace
std;
// User-defined function prototypes
void
sumTwo(
int
*n1,
int
*n2,
int
*n3);
// function sumTwo() is declared
void
getInput(
int
*n1,
int
*n2);
// function getInput()
is declared
void
display (
int
*n1,
int
*n2,
int
*n3);
// function display() is declared
int
main(
void
)
{
int
i, j, sum;
getInput(&i, &j);
// function getInput() is invoked
sumTwo(&i, &j, &sum);
// function sumTwo() is invoked
display(&i, &j, &sum);
// function display() is invoked
return
EXIT_SUCCESS;
}
//
//
Function sumTwo() receives three parameters which are pointers to integer
//
variables. The function computes the sum of the first two numbers and returns a
//
pointer to their sum. The numbers and their sum are printed in the calling
//
function.


You've reached the end of your free preview.
Want to read all 29 pages?
- Fall '15
- Marinolent