Ask Question
6 June, 08:02

9.6 Code Practice: Question 2

- Instructions

Add a method named "sumArray" to your last program that takes an array as a parameter and returns the sum of its values.

-Sample Run

How many values to add to the array:

8

[17, 99, 54, 88, 55, 47, 11, 97]

Total: 468

+4
Answers (1)
  1. 6 June, 11:31
    0
    A method named "sumArray" to your last program that takes an array as a parameter and returns the sum of its values.

    Explanation:

    The program is used to add a list of arrays.

    import java. util. Scanner;

    public class MethodsArrays {

    public static int[] fillArray () {

    Scanner scan = new Scanner (System. in);

    int size = scan. nextInt ();

    int array[] = new int[size];

    int pos=0;

    for (int i=0; i
    pos=i+1;

    System. out. println ("Enter element " + pos);

    array[i]=scan. nextInt ();

    }

    return array;

    }

    public static int sumArray (int [] array) {

    int sum=0;

    for (int i=0; i
    sum=array[i]+array[i+1];

    }

    return sum;

    }

    }

    public static void printArray (int [] array) {

    for (int i=0; i
    System. out. print (array[i] + " ");

    }

    }

    public static void main (String[] args) {

    fillArray ();

    System. out. println ("Sum=" + sumArray (array));

    printArray (array);

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “9.6 Code Practice: Question 2 - Instructions Add a method named "sumArray" to your last program that takes an array as a parameter and ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers