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 );...
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);
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...
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...
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
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
I am trying to write a program in C language but keep running into errors. Any...
I am trying to write a program in C language but keep running into errors. Any help would be awesome. here is my code I have so far. #include <stdio.h> #include <conio.h> #include <string.h> int main(){    int lenght, i = 0, state = 0;    char line[100];    printf("enter the string for checking of comment line: \n");    gets(line);    while(i < strline(line)){        switch(state){            case 0: if (line[i] == '/'){               ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT