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

Write a java program to create a 1-dimensional array with n elements; get the si

ID: 3766890 • Letter: W

Question

Write a java program to create a 1-dimensional array with n elements; get the size of the array as user input (validate!), max size should be 10 (declared as class constant). Your program should use modularity. Have methods to:

Take input from the user the elements of a 1-dimensional array.

Print a 1-dimensional array.

Compute the average value/1-dimensional array.

Display the difference between each value/1-dimensional array and the average.

Display all elements with odd subscripts.

Display all elements with even subscripts.

Compute the sum of all elements with odd subscripts.

Compute the sum of all elements with even subscripts.

Generate output similar to the sample output below.

Explanation / Answer

import java.util.*;
class arr_1d
{
   public static final int len=10;
   public static void arr(int a[],int n)
   {
       Scanner scan = new Scanner(System.in);
       int sum=0,i,av,se=0,so=0,su;
       System.out.println("Enter "+n +"Values");
       for(i=0;i<n;i++)
       {
           a[i]=scan.nextInt();  
           sum=sum+a[i];          
       }
       av=sum/n;
       System.out.println("Average is "+av);
       for(i=0;i<n;i++)
       {
           su=av-a[i];  
           System.out.println ("Difference of "+(i+1)+" element from average is "+su);

       }

       System.out.println("Elements at Odd subscript are");
       for(i=1;i<n;i=i+2)
       {
           System.out.println(a[i]);
           so=so+a[i];
       }
       System.out.println("Elements at Even subscript are");
       for(i=0;i<n;i=i+2)
       {
           System.out.println(a[i]);
           se=se+a[i];
       }
       System.out.println("Sum of Odd elements is "+so);

       System.out.println("Sum of Even elements is "+se);

   }

   public static void main(String args[])
   {

       Scanner scan=new Scanner(System.in);
       int i;
       int a[]=new int[100];
       System.out.println("Enter size of Array");
       int n=scan.nextInt();

       if(n>len)
       {
           System.out.println ("InCorrect");
       }
       else
       {
           arr(a,n);
       }
          

   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote