Pass by reference help

Hello all, I am faced with a challenge right now, I have an assignment that must be completed and I cannot seem to figure it out. The assignment is as follows:

"1.Write a function that finds the larger of two integers input (in the main program) but calls a function called change which allows you to change the input value of the integers (function must using pass by reference)"

Is it possible to completely change the integers as i am asked to do, I thought I could only swap them when I use a pass by reference statement.
So far I have attempted to code this as best as I can, it goes as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include <iostream>

using namespace std;

int change(int x, int y)
{
     int temp=0;
     cout << "Function(before change): " << x << " " << y << endl;
     temp = x;
     x = y;
     y = temp;
     cout << "Function(after change): " << x << " " << y << endl;
}
int larger ( int x, int y) // Here we define the function
{ 

	 if (x>=y)             // We state that if x is greater or equal to y we will return x
	 {
		 return x;
	 }
	 else                  // Otherwise we will return y
	 {
		 return y;
	 }
}
int main()
{
  int num1,num2;           // We ask for two integers from the user and we return the larger of the two
  int result;
  cout<<"Please Enter Two Integers and I Will Give You the Larger of the Two"<<endl;
  cout<<endl;
  cout<<"The First Integer? :" <<endl; // We ask for Integer 1
  cin >>num1;
  cout<<endl;
  cout<<"The Second Interger? : " <<endl; // We ask for integer 2
  cin >>num2;
  cout<<endl;

  result = change(num1,num2);
  
  system ("Pause");
  return 0;
}



you may notice I have two functions defined, which I have no idea if this is even possible, but in a previous assignment i had to use a function to find the greater of two integers input so I figured I could just use that program and edit it since they are basically the same except this one must include the option to change the inputs. The program I used goes as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std; 
int larger ( int x, int y) // Here we define the function
{ 

	 if (x>=y)             // We state that if x is greater or equal to y we will return x
	 {
		 return x;
	 }
	 else                  // Otherwise we will return y
	 {
		 return y;
	 }
}
int main()
{
  int num1,num2;           // We ask for two integers from the user and we return the larger of the two
  int result;
  cout<<"Please Enter Two Integers and I Will Give You the Larger of the Two"<<endl;
  cout<<endl;
  cout<<"The First Integer? :" <<endl; // We ask for Integer 1
  cin >>num1;
  cout<<endl;
  cout<<"The Second Interger? : " <<endl; // We ask for integer 2
  cin >>num2;
  cout<<endl;

  result = larger(num1,num2);

  cout<<"The Largest Integer is:  "<<result<<endl;
  cout<<endl;

  system("Pause");           // We pause the program upon execution so it stays on display
return 0;
}
I don't completely understand what you need to do but...

XzqtN wrote:
you may notice I have two functions defined, which I have no idea if this is even possible
Yes

1
2
3
4
5
6
//use an & for reference variables
int change(int &x, int &y) {
    //x is a reference to an int
    //y is a reference to a (most likely different) int
    ...
}
Last edited on
I dont completely understand either, this teacher does not explain ANYTHING. Is it possible to actually change the inputs of my values using a pass by reference(meaning can I actually input new values while the program is running), or do i just swap the values.
Ok so I have now coded the program with much less clutter and I believe it does what it is supposed to do, could anyone clarify this for me please?

so the assignment at hand is as follows : "Write a function that finds the larger of two integers input (in the main program) but calls a function called change which allows you to change the input value of the integers (function must using pass by reference)"


My new code is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

using namespace std;

int change(int& x, int& y)
{
     int temp=0;
     cout << "Function(before change): " << x << " " << y << endl;
     temp = x;
     x = y;
     y = temp;
     cout << "Function(after change): " << x << " " << y << endl;
}
int main()
{
int x,y;

    cout << "Enter your first number: ";
    cin >> x;
    
    cout << "Enter your second number: ";
    cin >> y;
    
    if (x > y)
       cout << x << " is greater than " << y << endl;
       
    if (x < y)
       cout << y << " is greater than " << x << endl;
    if (x == y)
       cout << x << " is equal to " <<y << endl;
       
       cout << "Main (before change): " << x << " " << y << endl;
       change(x, y);
       
       cout << "Main (after change): " << x << " " << y << endl;
       

       
       system("Pause");
       return 0;
       }
Yes, you can change a value while the program is running (unless the variable is const.) You use the assignment operator =
Last edited on
There is a shortcut for very simple if then else

1
2
3
4
5
6

int max(int x, int y){
x>y ? return x : return y

};


This is the ?: operator, and it's genral form is:

condition ? true statement : false statement

This is for fairly simple staements - the whole thing should fit on one line, no compound statements in braces.

TheIdeasMan
I had the same question and the same confusion...and thankful you posted here because I was up a crick without a paddle. We haven't learned the ?: operator yet, but that looks like a viable option. My main question was the same, can you change two inputs after they've already been entered...
You posting your 2nd code here helped me organize what I was doing. I too used the old assignment and then built off of that. Basically a shorter version of what you were showing with less grammar.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
using namespace std;

int change(int& num1, int& num2)
{
    int temp=0;
    cout << "Function(before change): " << num1 << " " << num2 << endl;
    temp = num1;
    num1 = num2;
    num2 = temp;
    cout << "Function(after change): " << num1 << " " << num2 << endl;
}

int main()
{
    // Prompt the user to enter two numbers
    int num1, num2;
    cout << "Enter two numbers: ";
    cin >> num1 >> num2;
    
    if (num1 > num2)
    cout << num1 << " is greater than  " << num2 << endl;
    
    if (num2 > num1)
    cout << num2 << " is greater than  " << num1 << endl;
    
    cout << "Main (before change): " << num1 << " " << num2 << endl;
    change (num1, num2);
    cout << "Main (after change): " << num1 << " " << num2 << endl;
    system("PAUSE");
    return 0;
}
Topic archived. No new replies allowed.