Problem Solving With C++ (Looseleaf) - With Access
Problem Solving With C++ (Looseleaf) - With Access
9th Edition
ISBN: 9780133835267
Author: SAVITCH
Publisher: PEARSON
Question
Book Icon
Chapter 9, Problem 1P
Program Plan Intro

Program Plan:

  • Include required header files.
  • Declaration of function prototype.
  • Definition for function “main()”.
    • Declare and initialize the variable “var”.
    • Print the value before calling function
    • Call the function “addOne()”.
    • Print the value after calling function.
    • Return the value “0”.
  • Definition of function “addOne()”.
    • Increment the pointer variable.

Expert Solution & Answer
Check Mark
Program Description Answer

The given program is to add one to the integer referenced by “ptrNum” by using reference parameter syntax.

Explanation of Solution

//Include required header files

#include<iostream>

using namespace std;

//Declaration of function header

void addOne(int *ptrNum);

//Definition of function main()

int main()

{

    //Declare and initialize the variable "var".

    int var = 10;

    //Print the value before calling function

  cout << "Value before calling function = " << var << '\n';

    //Call the function

    addOne(&var);

    //Print the value after calling function

  cout << "Value after calling function = " << var << '\n';

    //Return the value "0"

    return 0;

}

//Definition of function "addOne()"

void addOne(int *ptrNum)

{

    //Increment the pointer variable

    *ptrNum = *ptrNum + 1;

}

Sample Output

Output:

Value before calling function = 5

Value after calling function = 6

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
For C++, why is it that reference values can change when given to a function? I understand that if you make a const reference parameter like: double functionname(const point& p1) then the function cannot attempt to make a change to the parameter.
Please solve the problem in C++ .   Write a function called neg() that reverses the sign of its integer parameter. Write thefunction in two ways -first by using a pointer parameter and then by using areference parameter. Include a short program to demonstrate their operation . (in C++)
Write in C Language Description Please finish function between. Function between have two parameter a and b which are both the pointer point to integer. Function between return thesummation of all integer after pointer a and before pointer b.   Function int between(int *a, int *b){}
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning