- #1
glebovg
- 164
- 1
Homework Statement
Create a Java class named ArrayTest with a method called arrayCombiner that creates three arrays of size 10. The first array should contain the values 1, 3, 5, 7, 9, 11, 13, 15, 17, and 19. The second array should contain the values 2, 4, 6, 8, 10, 12, 14, 16, 18, and 20. Using loops, populate the third array with the sum of the value of the number at the same index location from the other two arrays. For example, the first element will be 3 (1+2), the second will be 7 (3 + 4) etc. Write another loop that loops through the third array and prints out its contents.
The attempt at a solution
Create a Java class named ArrayTest with a method called arrayCombiner that creates three arrays of size 10. The first array should contain the values 1, 3, 5, 7, 9, 11, 13, 15, 17, and 19. The second array should contain the values 2, 4, 6, 8, 10, 12, 14, 16, 18, and 20. Using loops, populate the third array with the sum of the value of the number at the same index location from the other two arrays. For example, the first element will be 3 (1+2), the second will be 7 (3 + 4) etc. Write another loop that loops through the third array and prints out its contents.
The attempt at a solution
Code:
public class ArrayTest
{
public static void main(String[] args)
{
arrayCombiner();
} // end main
public static void arrayCombiner()
{
int[] odd=new int[10];
int[] even=new int[10];
int[] sum=new int[10];
int count=0;
int i=1;
while(count<10)
{
odd[count]=i;
even[count]=i+1;
sum[count]=i+(i+1);
count++;
for(int sumCount:sum)
{
System.out.printf("%d ",sumCount);
}
}
} // end arrayCombiner
} // end class ArrayTest