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.
#include <iostream>
usingnamespace 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:
#include <iostream>
usingnamespace 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 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)"
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.