Google

Monday, April 12, 2010

CSC202 Revision April 14 - Part 2

QUESTION:

Write the main program that will call the triplingByReference function. Create a
variable called number and assign it the value 5. Then call triplingByReference passing
the address of number as the parameter. The function will triple the value stored in
number then store the result back into number. Then print out the result by printing the
new value stored in number.

SOLUTION:

//(a)
void doublingByRef(int *nPtr);

//(b)
void triplingByReference(int *nPtr){
*nPtr = *nPtr * 3;
//*nPtr = 3 * *nPtr;
}

//(c)
void main(){
int number=5;
triplingByReference(&number);
printf("%d\n",number);
}