1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
1
Rep Power
0

Optimize this code

import java.util.*;
import java.math.*;
class M
{

public static void main(String args[])
{
final int size=3;
Scanner s=new Scanner(System.in);
int a1[]=new int[size];
int a2[]=new int[size];
int i,num1,num2,sum;
i=0;
num1=0;
num2=0;
sum=0;
System.out.println("Enter elements for 1st array \n");
for(i=0;i<size;++i)
a1[i]=s.nextInt(); // scanf("%d",&a1[i]);
System.out.println("Enter elements for 2nd array \n");
for(i=0;i<size;++i)
a2[i]=s.nextInt(); // scanf("%d",&a2[i]);
num1 = convert(a1);
num2 = convert(a2);
sum = num1 + num2;
System.out.println("Sum of arrary \n"+sum);
}
static int convert(int arr[])
{
int i =0, num=0;
final int size=3;
try{
for(i = 0;;++i)
{
num += arr[i] * Math.pow(10,i);
System.out.println("Sum of array a[size] \n"+(int)Math.pow(10,i)+""+arr[i]);
}

}catch(Exception e){}
return num;
}
}
somebody optimize this code right now it is taking 246mb
2. > somebody optimize this code right now it is taking 246mb
Which real operating system are you using?
Which JavaVM are you using?
How did you arrive at 246Mb?

For me, your program uses about 15Mb (it uses 14Mb on just "hello world")
\$ java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~11.10.1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)