Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ programming!!! Don\'t forget to respond to the questions in the \"Run and su

ID: 3594099 • Letter: C

Question

C++ programming!!!
Don't forget to respond to the questions in the "Run and submission" part!!!

Instructions:

In this week’s lab, you will be defining four variables in main and passing them to three functions. You will be following the steps detailed below.  The challenge is to understand exactly what is happening and why.  Start by coding the following four methods in a .cpp file.  

Main method, Print method, ReadIn method

Start your main method with four integer variables:  a, b c, and d.  Call a function, called ReadIn, to read in the four values (input 10, 20, 30 and 40). Next, please print out the four variables: a, b, c, and d from main, also using a function, called Print.  Make sure that you label the values, like A = XX, B = XX, etc.  After you print these variables, please pass all four variables to a function that you will create below.  After you return from the call to this Change function, please print out the four variables again: a, b, c, and d in main using the call to the Print function, just as you did before the call to the Change function.  

Change method:

This method can be called whatever you want, however it will be a void function, like ReadIn and Print.  This method should take in four parameters w, x, y, and z. The first two will be pass by value variables and the last two will be pass by reference variables, using the &. Print out the 4 variables, w, x, y, and z, using just inline code, not a call to a function.  Now the Change function should change w to 1, x to 2, y to 3 and z to 4. Print out the 4 variables, w, x, y, and z again. This should be done in the same manner as you did earlier in the Change function – inline code.  Make sure to label each variable, W = XX, X = XX, Y == XX, Z = XX.

Run & Submission:

Compile, and run the program as normal.  However, look closely at your output.  Does it make sense as to what happened and why?  Write a small paragraph explaining, in detail, what happened to the data in the variables and why. Why did some of the data in main change after the call to the Change function?  Why did other pieces of data in main not change after the call to the Change function?

Turn in the source and output with a short paragraph explaining what happened in this program.

Explanation / Answer

/* The parameters which are passed by value does not effect on the function but those passed with refeence will show the effect of the function acting on them. */


#include<iostream>

using namespace std;

void Readln(int &a, int &b, int &c, int &d){

     cout << "Enter four integers a,b,c,d:";
     cin >> a >> b >> c >> d;
}

void Print(int a, int b, int c, int d){

     cout << "A= " << a << " " << "B= " << b << " " << "C= " << c << " " << "D= " << d << endl;
    
}

void change(int w , int x, int &y, int &z){

   cout << w << " " << x << " " << y << " " << z << endl;
   w = 1;
   x = 2;
   y = 3;
   z = 4;
   cout << w << " " << x << " " << y << " " << z << endl;

}

int main(){

   int a,b,c,d;
  
   Readln(a,b,c,d);
   Print(a,b,c,d);
   change(a,b,c,d);
   Print(a,b,c,d);
  
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote