Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Help for Terminal Exams................

1. ## 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.

2. ## 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.

3. ## The Following User Says Thank You to mstabosz For This Useful Post:

arko_chatterjee (September 21st, 2013)

4. ## Re: Help for Terminal Exams................

What help do you need?

5. ## 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

6. ## 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.

If you still don't understand, please explain your confusion.

7. ## 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]);

}
}

8. ## Re: Help for Terminal Exams................

Please post your code in format tags.

The problem is probably with your for loop condition, s <= n. Try s < n.

9. ## 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

```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.

10. ## The Following User Says Thank You to mstabosz For This Useful Post:

arko_chatterjee (September 21st, 2013)