Help for Terminal Exams................

• September 20th, 2013, 11:22 AM
arko_chatterjee
Help for Terminal Exams................
Plz help:
1. Declare an array of int n and put the squares of the elements of the array at a 2nd array and the cubes of the original(1st array)
in a third array.
• September 20th, 2013, 11:43 AM
mstabosz
Re: Help for Terminal Exams................
How to use arrays in Java.

As for the calculating the squares and cubes, you can use the Math.pow() method if you want, but since it's just squares and cubes, you can calculate that out pretty easily without, since the exponents are pretty low. I'd probably do that if I were writing this program, but since either route is pretty simple, it's 6 of one, half dozen of the other.
• September 20th, 2013, 11:43 AM
GregBrannon
Re: Help for Terminal Exams................
What help do you need?
• September 21st, 2013, 06:37 AM
arko_chatterjee
Re: Help for Terminal Exams................
thank you very much...

--- Update ---

the help that i needed is that how can i move the squares into another array
• September 21st, 2013, 06:43 AM
GregBrannon
Re: Help for Terminal Exams................
There are 3 arrays required. You might call them:

rootArray[]
squaredArray[]
cubedArray[]

Then, for example, each element of squaredArray[] = rootArray[] squared. Similarly for cubedArray[], both of which can be filled in a single loop.

• September 21st, 2013, 06:45 AM
arko_chatterjee
Re: Help for Terminal Exams................
i hv written a code... it is working . but the factor is whenever i try to print the two arrays in a separate for loop it says array out of index
(IMPORTANT: The Code which i have written computes the odd in one array and even numbers in another array)

import java.util.*;
class Odd_Even
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Array Size");
int n=sc.nextInt();
int a1[]=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter Elements");
a1[i]=sc.nextInt();
}
int i,k,j;int e=0;int o=0;int t=0,m=0;
int a2[]=new int[n];
int a3[]=new int[n];
for(i=0;i<n;i++)
{
if(a1[i]%2==0)
{
//e++;
t= a1[i];
a2[i]=t;
// System.out.println("t="+t);
System.out.print("a2[]"+a2[i]);
System.out.println(" ");
}
else
{
//o++;
m= a1[i];
a3[i]=m;
//System.out.println("m="+m);
System.out.print("a3[]"+a3[i]);
System.out.println(" ");
}
}

// for(int s=0;s<=n;s++)
// { This for loop says array out of index

// System.out.println("a2[]"+a2[i]);
//System.out.println("a3[]"+a3[i]);

}
}
• September 21st, 2013, 06:52 AM
GregBrannon
Re: Help for Terminal Exams................

The problem is probably with your for loop condition, s <= n. Try s < n.
• September 21st, 2013, 09:31 AM
mstabosz
Re: Help for Terminal Exams................
Yeah like Greg said, your loop logic is wrong. If you declare an array of size 5, then its available elements are at 0, 1, 2, 3, and 4. If you loop through with something like for(i=0; i<=5; i++), then your loop looks at elements 0, 1, 2, 3, 4, and the nonexistent 5. It looks like you got that right in your first for loop but slipped up in the second one.

You can also use a for-each loop to iterate through an array. The syntax is different, but it prevents out of bounds errors. If you had an array named numbers of 5 integers, to loop through with a for-each, you would write

Code java:

```for(int each: numbers) System.out.println(each);```

You don't have to use "each" in there. You can call it whatever you want.

Also, it helps to give your variables names more meaningful to their purpose. Names like "number" and "squareArray" make the code easier to follow and debug.