Question

Write functions squareByValue, squareByRef, squareByPointer that find the square of an integer, in three different ways:...

Write functions squareByValue, squareByRef, squareByPointer that find the square of an integer, in three different ways:

a)squareByValue should return the square of the number, passing the argument by value.

b)squareByRef should modify the variable in the input to its square, passing by reference.

c)squareByPointer should also modify the variable in the input to its square, but passing the argument by pointer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

square.cpp

#include<iostream>
using namespace std;

//n has value of x
int squareByValue(int n)
{
   //return square of n
   return n*n;
}

//reference to y
void squareByRef(int &n)
{
   //update n
   n=n*n;
}

//point to the z
void squareByPointer(int *n)
{
   //update value pointed by pointer
   *n = *n * *n;
}

int main()
{
   int x=5;
   //x will be passed
   cout<<"Square by Value: "<<squareByValue(x)<<endl;
  
   int y=6;
   //y will be passed
   squareByRef(y);
   cout<<"Square by Reference: "<<y<<endl;
  
   int z=7;
   //address should of z be passed
   squareByPointer(&z);
   cout<<"Square by Pointer: "<<z<<endl;
   return 0;
}

output screenshot:

Add a comment
Know the answer?
Add Answer to:
Write functions squareByValue, squareByRef, squareByPointer that find the square of an integer, in three different ways:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write three functions that compute the square root of an argument using three different methods. The...

    Write three functions that compute the square root of an argument using three different methods. The methods are increasingly sophisticated, and increasingly efficient. The square root of a real number is the values such that x . For us, the values will be double precision variables and so may not be perfectly accurate. Also, for us, assume that is in the range 0.0 to 100.0 You program should have a main() that asks the user for x and an accuracy...

  • 14. Write two functions that take an integer parameter and return the square and the cube...

    14. Write two functions that take an integer parameter and return the square and the cube of this number, correspondingly. Write a C program that reads an integer and uses the functions to display the sum of the number's square and cube.

  • 1. String Length Write a function that returns an integer and accepts a pointer to a...

    1. String Length Write a function that returns an integer and accepts a pointer to a C-string as an argument. The function should count the number of characters in the string and return that number. Demonstrate the function in a simple program that asks the user to input a string, passes it to the function, and then displays the function’s return value. c++

  • 1. Palindrome Write a program that prompts the user for a positive integer number and output whet...

    Language is C 1. Palindrome Write a program that prompts the user for a positive integer number and output whether the number is a palindrome or not. A palindrome number is a number that is the same when reversed. For example, 12321 is a palindrome; 1234 is not a palindromoe. You will write a function named as palindrome. The function will have two arguments, number and isPalindrome. Both arguments are pass-by-reference. The argument number is a pointer to a variable...

  • Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you...

    Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you will be implementing all three, along with three minor functions that can be passed to them. Map The map function takes as arguments a function pointer and a integer vector pointer. It applies the function to every element in the vector, storing the results in-order in a new vector. It then returns a pointer to a new vector. You should pass your square function...

  • Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven-...

    Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven- takes an integer input (one argument of type int) and return true or false. 2. IsPositive- takes a float input (one argument of type double) and return a Boolean value. The function returns the true if its argument is positive and false if its argument is zero or negative. 3. IsPrime- takes an integer input and return true or false. 4. NumOfDigits- takes an...

  • In C: Write a function "MinMax" that takes as an argument an integer array, an integer...

    In C: Write a function "MinMax" that takes as an argument an integer array, an integer for the size of the array, and two integer pointers. The function should print nothing and return nothing but change the value of the first pointer to the minimum value in the array and change the value in the second pointer to the max value in the array.

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • Your code must approximate the square root of an integer between 0 and 2^31-1 Using integer...

    Your code must approximate the square root of an integer between 0 and 2^31-1 Using integer maths is sufficient (no floating point or fractions are required); your code will return the truncated (integer portion) of the square root. Your code must be in an assembly language subroutine which is called by a C function for testing. Be sure to use registers according to the ARM calling convention. Base your software on the following pseudocode: Approximate square root with bisection method...

  • Assume that integer array b[5] and integer pointer variable bPtr have been defined. Write a statement...

    Assume that integer array b[5] and integer pointer variable bPtr have been defined. Write a statement to set bPtr equal to the address of the first element in array b. Write a statement using pointer expression to reference the array element b[3]. please make sure answer should be detailed ,  correct and in C language.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT