DOC PREVIEW
UCLA COMSCI 31 - pointer example

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

// CS 31 Example Program// February 20, 2008// John A. Rohr// Pointer Function Examples#include <iostream>#include <iomanip>using namespace std;void exchangeV(int p1, int p2); // Exchange Function:Value Parametersvoid exchangeP(int* p1, int* p2); // Exchange Function: Pointer Parametersvoid exchangeR(int &p1, int &p2); // Exchange Function: Reference Parametersvoid greaterVP(int* p1, int *p2, int* gr); // Greater Function: Value Pointer Parametersvoid greaterRP(int* p1, int* p2, int* &gr); // Greater Function: Reference Pointer Parametersint* greaterPRV(int* p1, int* p2); // Greater Function: Pointer Return Valueint main(){int num1; // First numberint num2; // Second numberint ans; // Answer numberint *p1; // Pointer to first numberint *p2; // Pointer to second numberint *pans; // Pointer to answer number// Set valuesnum1 = 1;num2 = 2;// Display the valuescout << "Main Program Before Call: num1 = " << num1 << endl;cout << "Main Program Before Call: num2 = " << num2 << endl;// Call the exchange function with value parametersexchangeV(num1, num2);// Display the valuescout << "Main Program After Call: num1 = " << num1 << endl;cout << "Main Program After Call: num2 = " << num2 << endl;// Display a blank linecout << endl;// Set valuesnum1 = 3;num2 = 4;// Display the valuescout << "Main Program Before Call: num1 = " << num1 << endl;cout << "Main Program Before Call: num2 = " << num2 << endl;// Call the exchange function with pointer parametersexchangeP(&num1, &num2);// Display the valuescout << "Main Program After Call: num1 = " << num1 << endl;cout << "Main Program After Call: num2 = " << num2 << endl;// Display a blank linecout << endl;// Set valuesnum1 = 5;num2 = 6;// Display the valuescout << "Main Program Before Call: num1 = " << num1 << endl;cout << "Main Program Before Call: num2 = " << num2 << endl;// Call the exchange function with reference parametersexchangeR(num1, num2);// Display the valuescout << "Main Program After Call: num1 = " << num1 << endl;cout << "Main Program After Call: num2 = " << num2 << endl;// Display a blank linecout << endl;// Set valuesnum1 = 11;num2 = 12;ans = -1;// Set pointersp1 = &num1;p2 = &num2;pans = &ans;// Display the valuescout << "Main Program Before Call: num1 = " << num1 << endl;cout << "Main Program Before Call: num2 = " << num2 << endl;cout << "Main Program Before Call: ans = " << ans << endl;// Call the greater function with value pointer parametersgreaterVP(p1, p2, pans);// Display the valuescout << "Main Program After Call: num1 = " << num1 << endl;cout << "Main Program After Call: num2 = " << num2 << endl;cout << "Main Program Before Call: ans = " << ans << endl;// Display a blank linecout << endl;// Set valuesnum1 = 13;num2 = 14;ans = -1;// Set pointersp1 = &num1;p2 = &num2;pans = &ans;// Display the valuescout << "Main Program Before Call: num1 = " << *p1 << endl;cout << "Main Program Before Call: num2 = " << *p2 << endl;cout << "Main Program Before Call: ans = " << *pans << endl;// Call the greater function with reference pointer parametersgreaterRP(p1, p2, pans);// Display the valuescout << "Main Program After Call: num1 = " << *p1 << endl;cout << "Main Program After Call: num2 = " << *p2 << endl;cout << "Main Program Before Call: ans = " << *pans << endl;// Display a blank linecout << endl;// Set valuesnum1 = 15;num2 = 16;ans = -1;// Set pointersp1 = &num1;p2 = &num2;pans = &ans;// Display the valuescout << "Main Program Before Call: num1 = " << *p1 << endl;cout << "Main Program Before Call: num2 = " << *p2 << endl;cout << "Main Program Before Call: ans = " << *pans << endl;// Call the greater function with pointer return valuepans = greaterPRV(p1, p2);// Display the valuescout << "Main Program After Call: num1 = " << *p1 << endl;cout << "Main Program After Call: num2 = " << *p2 << endl;cout << "Main Program Before Call: ans = " << *pans << endl;// Display a blank linecout << endl;// Display a completion messagecout << "Done." << endl;// Returnreturn 0;}// Exchange Function: Value Parametersvoid exchangeV (int p1, int p2){cout << "Exchange Function: Value Parameters" << endl;int hold;hold = p1;p1 = p2;p2 = hold;cout << "In Function: p1 = " << p1 <<endl;cout << "In Function: p2 = " << p2 <<endl;}// Exchange Function: Pointer Parametersvoid exchangeP (int* p1, int* p2){cout << "Exchange Function: Pointer Parameters" << endl;int hold;hold = *p1;*p1 = *p2;*p2 = hold;cout << "In Function: p1 = " << *p1 <<endl;cout << "In Function: p2 = " << *p2 <<endl;}// Exchange Function: Reference Parametersvoid exchangeR (int &p1, int &p2){cout << "Exchange Function: Reference Parameters" << endl;int hold;hold = p1;p1 = p2;p2 = hold;cout << "In Function: p1 = " << p1 <<endl;cout << "In Function: p2 = " << p2 <<endl;}// Greater Function: Value Pointer Parametersvoid greaterVP(int* p1, int* p2, int* gr){cout << "Greater Function: Value Pointer Parameters" << endl;if (*p1 >= *p2){*gr = *p1;}else{*gr = *p2;}}// Greater Function: Reference Pointer Parametersvoid greaterRP(int* p1, int* p2, int* &gr){cout << "Greater Function: Reference Pointer Parameters" << endl;if (*p1 >= *p2){gr = p1;}else{gr = p2;}}// Greater Function: Pointer Return Valueint* greaterPRV(int* p1, int* p2){cout << "Greater Function: Pointer Return Value" << endl;int *p;if (*p1 >= *p2){p = p1;}else{p = p2;}return


View Full Document

UCLA COMSCI 31 - pointer example

Download pointer example
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view pointer example and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view pointer example 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?