Question

[C++ Language] Describe how the following call by reference works. Make a comment for each line....

  1. [C++ Language] Describe how the following call by reference works. Make a comment for each line.

void pxc(int& c, int& d)

{

    int k = c;

    c = d;

    d = k;

}

  

int main()

{

    int a = 15, b = 100;

    pxc(a, b);

}

Homework Answers

Answer #1

Solution :

void pxc(int& c, int& d) // here c & d represent a & b if changed value in c , d change the actual value in a , b

{

    int k = c; // store the value of c in variable k

    c = d; // store the value of d in variable c

    d = k; // store the value of k in variable d

} // It's actually swapping values of the two variables

  

int main()

{

    int a = 15, b = 100; // initialize the value of a and b

    pxc(a, b); // here after calling the function a= 100 and b= 15

}

Note : reference variable represent the same variable, with other name. It's just like represent same variable with two names. If we change value in reference variable, it will change the actual value in original variable.

Know the answer?
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
Similar Questions
C PROGRAMMING LANGUAGE The following code written in C programming language was run and displays 997705320....
C PROGRAMMING LANGUAGE The following code written in C programming language was run and displays 997705320. Explain why and how this program outputs this number. int main(void) { int a = 5, b =20; int* ptr = &a; int** ptr2 = &ptr; printf("%d\n", ((int)*ptr * (int)*ptr2)); return 0; }
In C language: Partially finished program is given below. The second and third parameters of the...
In C language: Partially finished program is given below. The second and third parameters of the count() function need to be pointer parameters. However, the programmer forgot to write the necessary ampersands and asterisks in the prototype, call, function header, and function code. Add these characters where needed so that changes made to the parameters adults and teens actually change the underlying variables in the main program. #include #define MAX 10 void count( int ages[], int adults, int teens );...
how can i write a function to call by reference ? using fopen, printf, fclose statements....
how can i write a function to call by reference ? using fopen, printf, fclose statements. #define _CRT_SECURE_NO_WARNINGS #define PI 3.14159 #include <stdio.h> void findArea(double *area, double r); int main(void) { double area, r = 100.5; findArea(&area, r); printf("area = %.2lf\n", area); return 0; }
Explain each line in main (you can put it in the comments) and explain why the...
Explain each line in main (you can put it in the comments) and explain why the output looks how it looks. #include <iostream> using namespace std; class A { public:        A()        {               n=0;        }        A(int x)        {               n=x;        }        void f()        {               n++;        }        void g()        {               f();               n=n*2;               f();        }        int k()        {               return n;        }       ...
Programming in C language (not C++) Write a main function with a function call to a...
Programming in C language (not C++) Write a main function with a function call to a function called Calculation which has two integer arguments/ parameters. The function returns a character. Declare and initialize the necessary data variables and assign balues needed to make it executable and to prevent loss of information. //input 2 integer values //returns the characterequivalent of the higher of the two integer values char Calculation(int, int);
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
2. Consider the following program written in C syntax: void swap(int a, int b) {   ...
2. Consider the following program written in C syntax: void swap(int a, int b) {    int temp;    temp = a;    a = b;    b = temp; } void main() {    int value = 2, list[5] = {1, 3, 5, 7, 9};    swap(value, list[0]);    swap(list[0], list[1]);    swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three...
Print the following pattern.in c language only using for loop only and please comment each step...
Print the following pattern.in c language only using for loop only and please comment each step to make it understandable 5 5 5 4 4 5 5 4 3 3 4 5 5 4 3 2 2 3 4 5 5 4 3 2 1 1 2 3 4 5
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...