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.
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
Members have full access to the forums. Advertisements are removed for registered users.
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.
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.
arko_chatterjee (September 21st, 2013)
What help do you need?
thank you very much...
--- Update ---
the help that i needed is that how can i move the squares into another array
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.
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]);
}
}
Please post your code in format tags.
The problem is probably with your for loop condition, s <= n. Try s < n.
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.
arko_chatterjee (September 21st, 2013)