Results of a call

If a =17 and b=3, what will happen if I call fi(a, b)?
1
2
3
4
5
void fi(int& a1, int a2){
a1 /= a2;
a2 += 2;
a2 = a1 % a2;
}

I tried to call it by saying x=17; and cout << fi(x,3); but it gives me errors. I put the main function I tried to make below.

1
2
3
4
5
6
int main(){
    int x=17
    cout << fi(x,3);
   return 0;
}
You can't cout a function without a return value. It looks like you want to return a2.

So:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int fi(int& a1, int a2)
{
a1 /= a2;
a2 += 2;
a2 = a1 % a2;
return a2;
}

int main()
{
    int x=17
    cout << fi(x,3);
   return 0;
}
Last edited on
I tried that and I get 0 as an answer now, is that right? The question was If a =17 and b=3, what will happen if I call fi(a, b)?

1
2
3
4
5
void fi(int& a1, int a2){
a1 /= a2;
a2 += 2;
a2 = a1 % a2;
}

I want to know how to code it so I can see the response myself on a compiler.
I want to know how to code it so I can see the response myself on a compiler.


??? The code is right there? I don't understand what you mean?
If a is 17 and b is 3 what will they be after the call fi(a,b) is the question. The code I have is below:
1
2
3
4
5
void fi(int& a1, int a2){
a1 /= a2;
a2 += 2;
a2 = a1 % a2;
}


So my question is, what is the call? Is 0 the correct answer? And how do I call this myself on the compiler, without editing the void fi function since it was given to me as a constant function(or do you absolutely have to edit this?)

@hellworld136

Yes, 0 is the answer. Also, you should have gotten errors when compiling, since the ending semi-colon is missing on line 15. Anyway, I added in cout's to show the results of each computation in the fi() function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using std::cout;
using std::endl;

int fi(int& a1, int a2)
{
a1 /= a2;
cout << "a1 = " << a1 << endl; // equals 5
a2 += 2;
cout << "a2 = " << a2 << endl; // equals 5
a2 = a1 % a2;
cout << "a1%a2 = " << a2 << endl;// a zero is left with this modular expression 
return a2;
}

int main()
{
    int x=17;
    cout << fi(x,3) << endl;
   return 0;
}
So is this right?
17/3=5.6, rounds down to 5
3+2=5
5/5, remainder is 0

So would my answer be a=0 and b=3? b remains unchanged since it is not pass by reference?

Also, if the math was more complex, what happens to pass by value and pass by reference values involved? For instance, if there was another line saying a1+=a2 and a2+=a1, would it use the original a1 and a2 values, or the ones edited within the code?

Last edited on
@hellworld136

How can 0 be the answer if I need a value for a and b?


???? You have an a1 and a2, not a and b. Also, you can only return one variable from the function, which is the a2.

a1 = 17
a2 = 3
17 / 3 = 5
a2 + 2 = 5
5 goes into 5, one time, with nothing left over, which is what modulus is about
So, a2 is a zero, which is returned, which was your answer.
The practice question I'm working on says: If A=17 and B=3, what will the values of a and b be after the call fi(a,b)?


Last edited on
For your code to work properly, you have to get rid of the semi-colons on lines 2 and 4. Semi-colons end a statement, so lines 3 and 5 don't execute, only line 6 will.

Right now, I can't continue helping. It's almost 1:30 in the morning, and I'm headed for bed. Good luck..
I was told it's worded correctly as a question, so it may be purposefully wrong since I just started programming and we're expected to find errors. So would I say a and b are both 0?

The question exactly says
If A=17 and B=3, what will the values of a and b be after the call fi(a,b)?
a=?
b=?
1
2
3
4
5
void fi(int& a1, int a2){
a1 /= a2;
a2 += 2;
a2 = a1 % a2;
}

This is all the information I have for the question and it's worded correctly, so would I just say both are 0?

Last edited on
It does seem like your question is answered by this:
1
2
3
4
5
6
7
8
int main()
{
  int x = 17;
  int y = 3;
  fi( x, y );
  cout << "a: " << x << ", b: " << y << '\n';
  return 0;
}

The real question that you should learn to answer is Why the result is what it is?
That's what I'm trying to do here, that is just a practice question. I know that
I believe it's because a=17/3=5.66=5, and b remains unchanged because it was pass by value. I'm not sure about why I need an int for the y, or the cout "a:" part though.
I'm not sure about why I need an int for the y


The question exactly says:
What will the values of x and y be after the call fi(x,y)?
y=?


You don't need the y because you can think through the function and based on the code say that the y, whatever it is, will not change because the function takes that parameter by value. You don't even need to call the function, compile, or run any program.

However, you did try to call the function, incorrectly. It is a bit worrying that you seem to understand the parameters of a function, yet failed to call the function with correct syntax.
Notice that a1 is passed by reference, but a2 is passed by value. That means the final value of a2 is thrown away when the function is done. So the last two lines have no effect and you can think of the function as:
1
2
3
4
5
void fi(int& a1, int a2){
a1 /= a2;
a2 += 2;
a2 = a1 % a2;
}


In fact, if you compile this with optimization turned on, you'll probably find that the compiler doesn't generate code for lines 3 and 4.

So if a=17 and b=3, calling fi(a,b) results in a=17/3 = 5 and b is unchanged.
Topic archived. No new replies allowed.