Advertisement
rushdie

Function Home Work 3rd exercise

Nov 27th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. package HWPres5;
  2. import java.util.Scanner;
  3.  
  4. public class array3Val {
  5.  
  6. //This function receive two array of type integer and merge them to get a third one
  7. //has the same values with a count for the number that match.
  8. public static int[] mergetwoArray (int[] arrOne, int[] arrTwo){
  9.  
  10. int [] equals = new int[Math.min(arrOne.length, arrTwo.length)];
  11. int count=0;
  12.  
  13. for(int i:arrOne){
  14. for(int j: arrTwo){
  15. if(i==j){ // if the values in index are equal, Array Equals created
  16. equals[count]=i;
  17. count+=1;
  18. }
  19. }
  20. }
  21. return makeThird(equals,count);
  22. //call for make new function to create an array of the proper length
  23. // the count allow me to only add the right values no empty indexs
  24. }
  25.  
  26. // Function that gets an array and the number of items in it
  27. public static int[] makeThird (int[] arrOne,int howMany){
  28.  
  29. int[] merged= new int[howMany];
  30. for(int i=0; i<howMany; i+=1){
  31. merged[i]=arrOne[i];
  32. }
  33. return merged; // Return a third array with only the values equal to count
  34. }
  35.  
  36.  
  37. //This function gets an array and converts it to Sting array
  38. public static String intToString (int[] merged){
  39. String str="";
  40. for(int i: merged){
  41. str+=i+" ";
  42. }
  43. return str;
  44. }
  45.  
  46.  
  47. public static void main(String[] args) {
  48. Scanner scan = new Scanner(System.in);
  49.  
  50. //Created 3 arrays of type integer and intilized their size to be
  51. //allow future changes int change control the size
  52. int elements1=0, elements2=0, elements3=0;
  53. int[] arr3 = new int[elements3];
  54.  
  55. System.out.printf(" Please, Enter the Numbers of elements in your FIRST array? ", elements1);
  56. elements1 = scan.nextInt();
  57. int[] arrOne= new int[elements1] ;
  58.  
  59. //Read the prompted values into the Arrays
  60. System.out.printf(" Please, Enter %d Elements in your First Array:%d", elements1,arrOne.length);
  61. for (int i = 0; i < arrOne.length; i++) {
  62. arrOne[i] = scan.nextInt();}
  63.  
  64.  
  65. System.out.printf(" Please, Enter the Numbers of Elements in your SECOND array? ", elements2);
  66. elements2 = scan.nextInt();
  67. int[] arrTwo= new int[elements2];
  68.  
  69. System.out.printf(" Please, Enter %d Elements in Second Array:%d ",elements2,arrTwo.length);
  70. for (int j = 0; j < arrTwo.length; j++){
  71. arrTwo[j] = scan.nextInt();
  72.  
  73.  
  74. }
  75.  
  76. // creates the third array by calling function mergeTwoArray
  77. arr3 = mergetwoArray(arrOne, arrTwo); // create a third array arr3 with equal digits in arr1 and arr2
  78. System.out.println(" The Digits Equal in both arrays after the merge are "+intToString(arr3));
  79. // After calling Function convert integer to String
  80. //print the third array with the results if any!
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement