Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that declares an array \"alpha\" of 50 elements of type \"double

ID: 3543459 • Letter: W

Question

Write a program that declares an array "alpha" of 50 elements of type "double".  Initialize 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 three times the index variable. Output the array so that 10 elements per line are printed.


Is there a different way of writing it then this way? If so please post a different example, don't want to use this one... Looking for a different way to write it.



public class TestClass

{

public static void main(String args[])

{

double[] alpha = new double[50];

/*Now initialize first 25 element the array*/

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

{

alpha[i]= i * i;

}

/*Now initialize last 25 element the array*/

for(int i = 25; i < 50; i++)

{

alpha[i]= 3 * i;

}


/*print the element of the array*/

TestClass tstClass = new TestClass();

tstClass.print(alpha);


}

/* Print method to display the element of the array*/

void print(double m_array[])

{

for(int i =1; i <= m_array.length; i++)

{

System.out.print(m_array[i-1] +" ");

if(i%10==0)

System.out.print(" ");

}

}

}


Explanation / Answer

public class TestClass

{

public static void main(String args[])

{

double[] alpha = new double[50];

/*Now initialize first 25 element the array*/

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

{

if(i<25)alpha[i]= i * i;   // initialize array to square of index if < 25

else alpha[i]= 3 * i; // or else to 3 times the index

}

int i =0;

for(; i <alpha.length;)      //running loop till i< alpha.length

{

System.out.print(alpha[i] +" ");

i++;    //increment i here only so tha we can print a space after every 10th element

if(i%10==0) System.out.println();   //printing a new line after every 10th element .system.out.println() prints just a newline

}


}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote