How do I generate an array with the output of 1,0,0,0,0.....?

How do I generate an array with the output of  1,0,0,0,0.....?
public class chap7quiz9
{
public static void main(String[] args)
{


int a[] = new int[24];
for(int i = 0; i<24;i++)
{
a =1/(1+i);
System.out.println(0);
}
}
}
 

today230.JPG
waltbaby315Asked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
and with the fuirst class line:

public class chap7quiz9
{ 
public static void main(String[] args)
{


float a[] = new float[24];
for(int i = 0; i<24;i++)
{
a[i] =1.0f/(1.0f+i);
System.out.println(a[i]);
}
}
}

Open in new window

0
 
for_yanCommented:
Your code as you pasted it cannot compile,
a= 1/(1+i);
cannot work through compiler array cnnot be assihgned to integere or rather
integere to the arrai
0
 
for_yanCommented:
Even if you type it proably wwhta you meant
a[i]=1/(1+i)
you'll get 1 for the first element
a[0] = 1/1;
but 
a[1] = 1/2;
and when you assign to int you get truncation of the
decimal part, so it wioll 
a[1] = 0;

and all of the rest the same

Open in new window

0
 
for_yanCommented:
In order to print something sensible
you need to declare array a as float

{ 
public static void main(String[] args)
{


float a[] = new int[24];
for(int i = 0; i<24;i++)
{
a[i] =1.0f/(1.0f+i);
System.out.println(a[i]);
}
}
}

Open in new window


This will give you reasonable numbers
(in addition you also had
System.out.println(0) in your original code
instead of
System.out.println(a[i]);)

Open in new window


0
 
for_yanCommented:
Actually this is correct code
(forgot to chang int to float in array declaration)

{ 
public static void main(String[] args)
{


float a[] = new float[24];
for(int i = 0; i<24;i++)
{
a[i] =1.0f/(1.0f+i);
System.out.println(a[i]);
}
}
}

Open in new window

0
All Courses

From novice to tech pro — start learning today.