Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Functions {
- // ------------------ EX1 -----------------------------
- /*
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // declare values
- int number, digit, len;
- int[] nArr;
- // Part 1 of ex1
- System.out.println("Please enter a large number and a digit: ");
- number = sc.nextInt();
- digit = sc.nextInt();
- System.out.printf("The digit %d appears %d times in the number %d\n", digit, checkNum(number, digit), number);
- System.out.println("\n---------------------------------------");
- // Part 2 of ex1 - value from array
- System.out.println("Please enter the size of the numbers array and a digit: ");
- len = sc.nextInt();
- digit = sc.nextInt();
- nArr = new int[len];
- initArray(nArr);
- System.out.printf("The digit %d appears %d times in the given number array", digit, checkNumInArr(nArr, digit));
- }
- public static void initArray(int[] arr) {
- System.out.printf("Please enter %d integers: ", arr.length);
- Scanner s = new Scanner(System.in);
- for(int i = 0; i < arr.length; i++) {
- arr[i] = s.nextInt();
- }
- }
- public static int checkNum(int num, int d) {
- int n = 0;
- do {
- if(num % 10 == d) {
- n += 1;
- }
- num /= 10;
- } while(num != 0);
- return n;
- }
- public static int checkNumInArr(int[] numArr, int d) {
- int sum = 0;
- for(int i = 0; i < numArr.length; i += 1) {
- sum += checkNum(numArr[i], d);
- }
- return sum;
- }
- */
- // ---------------------- EX2 ------------------------
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- //Initializing arrays
- int[] arr1 = {87, 13, 12, 56, 32}, arr2 = {19, 87, 13, 21, 56, 32}, res, util;
- int resLen = arr1.length <= arr2.length ? arr1.length : arr2.length;
- util = new int[resLen];
- // Since the length is unknown the 'checkDuplicates' function
- // copies the duplicate values to the util array and return
- // the number of duplicate numbers in the given arrays
- int len = checkDuplicates(arr1, arr2, util);
- // The res array is initialized with the correct length
- res = new int[len];
- // copyToResultArray function copies the duplicate values to the result array
- copyToResultArray(util, res);
- // printResult displays the duplicate values to the screen
- printResult(res);
- }
- /**
- * The checkDuplicates function gets 3 arrays as an input. The function checks for duplicate numbers
- * in the first two arrays and copies them to the third array (The third array's is initialized
- * in accordance with the shortest array.
- * The function returns the number of duplicates found between the first two arrays
- */
- public static int checkDuplicates(int[] a1, int[] a2, int[] r) {
- int l = 0, index = 0;
- for(int i = 0; i < a1.length; i += 1) {
- for(int j = 0; j < a2.length; j += 1) {
- if(a1[i] == a2[j]) {
- r[index++] = a1[i];
- l += 1;
- }
- }
- }
- return l;
- }
- /**
- * A function for copying the values from the first array to the second given array with the right length
- */
- public static void copyToResultArray(int[] u, int[] r) {
- for(int i = 0; i < r.length; i++) {
- r[i] = u[i];
- }
- }
- /**
- * A utility function for printing the values from the result array to the terminal / command line
- */
- public static void printResult(int[] resArr) {
- System.out.print("[");
- for(int n = 0; n < resArr.length - 1; n += 1) {
- System.out.print(resArr[n] + ", ");
- }
- System.out.printf("%d]", resArr[resArr.length - 1]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement