Does not work.

what do you mean by this? is there an error or you can't get the desired output?
could you post those errors or output.

hmm.. well I tried the program and did what I suggested and I got it to work...

Member Avatar

Can you post your code? I really do not understand why I do not get the desired output.

Can you post your code?

That sounds like I'd be spoon feeding you so sorry but I'll pass

I really do not understand why I do not get the desired output.

Print the values of odd[count] and even[count] in the while loop and you'd see that eventually they'd get the values of:
odd[count] with {1,2,3,4,5,6,7,8,9} and
even[count] with {2,3,4,5,6,7,8,9,10}
so simple iteration won't work

at the odd numbers it should remain as the value of i
and the same at even numbers i+1

then use

sum[count] = odd[count] + even[count];

then after count+=1; , iterate using i+=2;

that should give the right values for the odd and even numbers at your code

Member Avatar

Here is my 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]=odd[count]+even[count];
count+=1;
i+=2;
for(int sumCount:sum)
{
System.out.printf("%d ",sumCount);
}
 }
  } // end arrayCombiner
   } // end class ArrayTest

I do not get the desired output.

the for loop for printing the value of sumCount should be outside/after the while loop

Member Avatar

Still cannot get it to work.

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]=odd[count]+even[count];
count+=1;
i+=2;
}
for(int sumCount:sum)
{
System.out.printf("%d ",sumCount);
}
 }
  }
   } // end arrayCombiner
    } // end class ArrayTest

The brackets at line 26 and 27 are not needed

Member Avatar

Thank you.

I'm glad that It's finally solved, congrats ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.