Ask 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 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

+1
Answers (1)
  1. 7 May, 07:40
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers