Click here to Skip to main content
15,906,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
total noob here. I have this thing I am trying to do with arrays lets say I have two arrays

[0, 1, 2, 3] && [4, 5, 6, 7]

can anyone please help me by either providing some pseudo-code or actual code for a Java implementation to get it to look like this

[0,4, 1, 5, 2, 6, 3,7]

your help is much-appreciated many thank yous in advance

What I have tried:

so far some pseudo code that is leading me no where
Posted
Updated 2-Nov-18 0:19am
Comments
Patrice T 1-Nov-18 22:22pm    
Show your work to get help.
CodeMine 3-Nov-18 3:06am    
just see the solution i have provided

1 solution

hi i have this solution for you

import java.util.Scanner;  
public class Test 
{
   public static void main(String args[])
   {
   Scanner sc=new Scanner(System.in);  
   int [] ar = new int[4];
   int [] ar1 = new int[4];
   int [] res = new int[8];
   int i;
   int k;
   for (i=0;i<4;i++)
   {
      System.out.println("Enter a number");  
      ar[i]=sc.nextInt();  
   }
   for (i=0;i<4;i++)
   {
      System.out.println("Enter a number");  
      ar1[i]=sc.nextInt();  
   }
   for (k=0,i=0;i<4;i++,k=k+2)
   {
       res[k]=ar[i];
   }
   for (k=1,i=0;i<4;i++,k=k+2)
   {
       res[k]=ar1[i];
   }
   for (i=0;i<8;i++)
   {
      System.out.print(res[i]+" ");  
   }
 }
}



output

Enter a number
0
Enter a number
1
Enter a number
2
Enter a number
3
Enter a number
4
Enter a number
5
Enter a number
6
Enter a number
7
0 4 1 5 2 6 3 7 
 
Share this answer
 
v2
Comments
Richard MacCutchan 2-Nov-18 4:47am    
You do not help people by doing their homework for them.
CodeMine 7-Nov-18 23:11pm    
Ya u r right.
But for a beginner I think it is difficult. That is why they posted this question.
Richard MacCutchan 8-Nov-18 4:13am    
Of course it's difficult, like everything in life. But there are far too many people posting questions here who expect other people to do their work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900