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

Write the word or phrase that best completes each statement or answer the questi

ID: 3782601 • Letter: W

Question

Write the word or phrase that best completes each statement or answer the question The following declaration is used throughout this example: double [] sales = new doublet [10]; int index; double largestSale, sum. average; Write the code to do the following: Initialize every component of the array to the value 0 Read data from the keyboard and store it in the array. You will be filling the whole array. Print all of the items in the array in reverse order. Find the sum and the average of the elements in the array. Determine the smallest and largest element of the array.

Explanation / Answer

a)

import java.util.Scanner;

public class Cray {

    public static void main(String[] args){

        final int SALESPEOPLE = 5;

        int[] sales = new int[SALESPEOPLE];

        int sum, maxperson, minperson;

        int max = sales[0];

        int min = sales[0];

        Scanner scan = new Scanner(System.in);

        //Fill the 5 element array of sales with the user's input

        for (int i=0; i<sales.length; i++)

            {

            System.out.print("Enter sales for salesperson " + i + ": ");

            sales[i] = scan.nextInt();

            //Calculate the max and min sales

            //How do I return the salesperson i with the the max and min sales?

            if(sales[i] > max){

                max= sales[i];

                                maxindex = i;

            }

            if(sales[i] < min){

                min = sales [i];

               maxindex = i;

                                }

            }

        System.out.println(" Salesperson   Sales");

        System.out.println("--------------------");

        sum = 0;

        for (int i=0; i<sales.length; i++)

           {

            System.out.println("     " + i + "         " + sales[i]);

            sum += sales[i];

            }

        System.out.println(" Total sales: " + sum);

        System.out.println("Average sales: " + sum/5);

        //WHere I want to print the max salesperson.

        System.out.println("Salesperson" + );

          }

        }

b)

import java.io.*;

class G5{

public static void main(String args[])throws Exception{

InputStreamReader r=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(r);

System.out.println("Enter your name");

String name=br.readLine();

System.out.println("Welcome "+name);

}

}

c)

import java.util.Scanner;

public class JavaProgram

{

   public static void main(String args[])

   {

       int size, i, j, temp;

       int arr[] = new int[50];

       Scanner scan = new Scanner(System.in);

                  

       System.out.print("Enter Array Size : ");

       size = scan.nextInt();

                  

       System.out.print("Enter Array Elements : ");

       for(i=0; i<size; i++)

       {

           arr[i] = scan.nextInt();

       }

                  

       j = i - 1;     // now j will point to the last element

       i = 0;         // and i will point to the first element

                  

       while(i<j)

       {

           temp = arr[i];

           arr[i] = arr[j];

           arr[j] = temp;

           i++;

           j--;

       }

                  

       System.out.print("Now the Reverse of Array is : ");

       for(i=0; i<size; i++)

       {

           System.out.print(arr[i]+ " ");

       }      

   }

}

d)

import java.util.Scanner;

public class Sum_Average

{

    public static void main(String[] args)

    {

        int n, sum = 0;

        float average;

        Scanner s = new Scanner(System.in);

        System.out.print("Enter no. of elements you want in array:");

        n = s.nextInt();

        int a[] = new int[n];

        System.out.println("Enter all the elements:");

        for(int i = 0; i < n ; i++)

        {

            a[i] = s.nextInt();

            sum = sum + a[i];

        }

        System.out.println("Sum:"+sum);

        average = (float)sum / n;

        System.out.println("Average:"+average);

    }

}

e)

class MinMaxExample {

public static void main(String args[]){

    int array[] = new int[]{10, 11, 88, 2, 12, 120};

    // Calling getMax() method for getting max value

    int max = getMax(array);

    System.out.println("Maximum Value is: "+max);

    // Calling getMin() method for getting min value

    int min = getMin(array);

    System.out.println("Minimum Value is: "+min);

}

// Method for getting the maximum value

public static int getMax(int[] inputArray){

    int maxValue = inputArray[0];

    for(int i=1;i < inputArray.length;i++){

      if(inputArray[i] > maxValue){

         maxValue = inputArray[i];

      }

    }

    return maxValue;

}

// Method for getting the minimum value

public static int getMin(int[] inputArray){

    int minValue = inputArray[0];

    for(int i=1;i<inputArray.length;i++){

      if(inputArray[i] < minValue){

        minValue = inputArray[i];

      }

    }

    return minValue;

}

}