I have to write a method that will output the following: Please enter 10numbers:
ID: 3615453 • Letter: I
Question
I have to write a method that will output the following: Please enter 10numbers: 5 3 2 8 14 22 5 3 20 3Sum of numbers is:85
New array contents is: 86 5 11 17 25 8 6 23 6 the method must add all integers in array and also display thearray with each element incremented by the last elementamount. My program is not summing the array properly and also notputting spaces while displaying new array. Here is what Ihave so far. import java.util.*;
public class Spring2004Tues
{
public static void main(String[] args) {
Scanner keyboard = newScanner(System.in);
// declare the array ofintegers
int[] theArray = newint[10];
int i; //loop counter
int sum = 0; // willstore sum of array values
System.out.println("Name: Christopher Kaplan, final exam problem 1,CS 107");
System.out.print("Pleaseenter 10 numbers: ");
for (i=0; i<10; i++){
// write the statement using cin to read in each number into arrayelement i
theArray[i] = keyboard.nextInt();
}
sum(theArray); // Call the function to getthe sum
// Function should also increment array values
System.out.println("Sum of numbers is: " + sum(theArray));
System.out.print("New array contents is: ");// +Arrays.toString(theArray));
for (i=0; i<10; i++) {
// display the contents of the array
System.out.print(theArray[i]);
}
}
public static int sum(int[] array) {
int sum = 0;
for (int i=0;i<array.length;i++) {
array[i] = array[i] + array[array.length - 1];
sum = sum + array[i];
}
return sum;
}
}
I will rateASAP if someone can help me resolve this issue. I have to write a method that will output the following: Please enter 10numbers: 5 3 2 8 14 22 5 3 20 3
Sum of numbers is:85
New array contents is: 86 5 11 17 25 8 6 23 6 the method must add all integers in array and also display thearray with each element incremented by the last elementamount. My program is not summing the array properly and also notputting spaces while displaying new array. Here is what Ihave so far. import java.util.*;
public class Spring2004Tues
{
public static void main(String[] args) {
Scanner keyboard = newScanner(System.in);
// declare the array ofintegers
int[] theArray = newint[10];
int i; //loop counter
int sum = 0; // willstore sum of array values
System.out.println("Name: Christopher Kaplan, final exam problem 1,CS 107");
System.out.print("Pleaseenter 10 numbers: ");
for (i=0; i<10; i++){
// write the statement using cin to read in each number into arrayelement i
theArray[i] = keyboard.nextInt();
}
sum(theArray); // Call the function to getthe sum
// Function should also increment array values
System.out.println("Sum of numbers is: " + sum(theArray));
System.out.print("New array contents is: ");// +Arrays.toString(theArray));
for (i=0; i<10; i++) {
// display the contents of the array
System.out.print(theArray[i]);
}
}
public static int sum(int[] array) {
int sum = 0;
for (int i=0;i<array.length;i++) {
array[i] = array[i] + array[array.length - 1];
sum = sum + array[i];
}
return sum;
}
}
I will rateASAP if someone can help me resolve this issue.
Explanation / Answer
I found the problem.