Reputation: 15
#include <iostream>
using namespace std;
int fun(int *k);
int main() {
int i = 10, j = 10, sum1,sum2;
sum1 = (i / 2) + fun(&i);
sum2 = fun(&j) + (j / 2);
cout<< sum1<< " "<<sum2<<endl;
}
int fun(int *k) {
*k += 4;
return 3 * (*k) - 1;
}
I have to convert my code to Java Code I already converted but i couldn't find my mistake i cannot point variables to each other..
public class System{
public static void main(String[] args) {
int i = 10;
int j = 10;
int sum1 = (i / 2) + fun(k.value=i);
int sum2 = fun(k.value=j) + (j / 2);
System.out.println("%d%d",sum1,sum2 );
}
public static int fun(int k) {
intobj k;
int k= new k();
k.value += 4;
return 3 * (k.value) - 1;
}
}
This is my java code when i look at the int sum1 = (i / 2) + fun(k.value=i); int sum2 = fun(k.value=j) + (j / 2); part isn't true about point to true values. How can i solve that pointers problem. Thank you.
Upvotes: 1
Views: 6881
Reputation: 1653
The problem is that you're using int
s instead of intobj
s where you want to pass things around by reference (&
in c++).
In your main function, you should try declaring i
and j
as intobj
s and your parameter for fun
, k
should also be an intobj
.
public class System{
public static void main(String[] args) {
intobj i = new intobj();
i.value = 10;
intobj j = new intobj();
j.value = 10;
int sum1 = (i.value / 2) + fun(i);
int sum2 = fun(j) + (j.value / 2);
System.out.println("%d%d",sum1,sum2 );
}
public static int fun(intobj k) {
k.value += 4;
return 3 * (k.value) - 1;
}
}
Upvotes: 1