I was wandering if I could get some help in refining my code for the following problem:

"Write a java program that declares an array alpha of 50 elements of type double. Initialise the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to 3 times the index variable. Output the array so that the first 10 elements are printed on each line."

my code so far:

import java.math;

void main(String[] args){

double[] alpha = new double(15);

int index = 0;

for(int i=0; i<24; i++){

alpha[i].length = pow(index,2);

for(int i=25;i<49;i++)
alpha[i].length = pow(index,3);

for(int i=0; i<10; i++)


Well, you want to actually set a value in index 24, right? So it is either <= 24 or < 25 (the same goes for the 49). You also want to set the value at that index so what is the length reference doing there? Also, the last half should be three times the index, right? Not the index Cubed (i.e. to the power of three), right?

Also, the array should consist of 50 elements, not 15 and you use square brackets [ ] not parens ( ) to declare an array length.

Also, you should be printing all 50 values, not just 10. But, you should be using print, not println (and don't forget to include some spacing between the numbers) and just use println at every 10th index (google java modulus).

You also need to declare a class to contain this main method, not just the method, and the main method has a specific signature (I'm sure you've been taught what that is) that you are not adhering to.

Edited 6 Years Ago by masijade: n/a

Thanks masijade, I now know what the problem is with my code and what should be modfied to yield the correct result.

This question has already been answered. Start a new discussion instead.