Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write and test a program that consists of the following functions: Initialize

ID: 3676000 • Letter: 1

Question

1) Write and test a program that consists of the following functions:

Initialize two one-dimension arrays as global variables

int main( ): in main function print out the two original arrays.

arrayAdd( ): do the addition for two one-dimension arrays and output the result.

arrayMulti( ): do the multiplication for one one-dimension array and one integer and output the result.

2) Initialize an array by two functions. The first function should initialize the array with same value. The second function should initialize the array in which each value is the square of the index. For example, array[i]=i*i; You must define the two functions with the same function name and the parameter in the two functions must be different. In other words, you are required to do the function overloading. You are also required to write a output function for the array.

Explanation / Answer

SOlution 1:

package com.nancy.chegg.qa;


public class ArrayTest {

   // initialization of arrays.
   static int arr1[] = {1, 2, 3, 4, 5};
   static int arr2[] = {1, 2, 3, 4, 5};
   public static void main(String[] args) {
       System.out.println("Elements of first array : ");
       for (int i = 0; i < 5; i++) {
           System.out.print(arr1[i] + " ");
       }
       System.out.println(" Elements of second array : ");
       for (int i = 0; i < 5; i++) {
           System.out.print(arr2[i] + " ");
       }
      
       arrayAdd();
       arrayMulti();
   }
   private static void arrayMulti() {
       int mulArr[] = new int[5];
       int tempInt = 5;
       for (int i = 0; i < 5; i++) {
           mulArr[i] = arr1[i] * tempInt;
       }
       System.out.println(" Array multiplication with 5 : ");
       for (int i = 0; i < 5; i++) {
           System.out.print(mulArr[i] + " ");
       }
   }
   private static void arrayAdd() {
       int sumArr[] = new int[5];
       for (int i = 0; i < 5; i++) {
           sumArr[i] = arr1[i] + arr2[i];
       }
       System.out.println(" Arrays sum : ");
       for (int i = 0; i < 5; i++) {
           System.out.print(sumArr[i] + " ");
       }
   }
}


Sample run:

Elements of first array :
1 2 3 4 5
Elements of second array :
1 2 3 4 5
Arrays sum :
2 4 6 8 10
Array multiplication with 5 :
5 10 15 20 25

Solution 2:

package com.nancy.chegg.qa;

public class OverloadingEx {

   static int arr[] = new int[5];

   public static void main(String[] args) {
       overloadedFun();
       System.out.println("Display elements: ");
       output();
      
       overloadedFun(true);
       System.out.println(" Display elements for i*i: ");
       output();
   }

   private static void output() {
       for (int i = 0; i < arr.length; i++) {
           System.out.print(arr[i] + " ");
       }

   }

   private static void overloadedFun() {
       for (int i = 0; i < arr.length; i++) {
           arr[i] = i;
       }
   }
  
   private static void overloadedFun(boolean b) {
       for (int i = 0; i < arr.length; i++) {
           arr[i] = i*i;
       }
   }

}


Sample output:

Display elements:
0 1 2 3 4
Display elements for i*i:
0 1 4 9 16