0

Coding Club challenge 2.

create and array of 5 integers and return the average number of all 5. bonus points for using user input.

30th Jul 2016, 8:07 PM
Barra
Barra - avatar
8 odpowiedzi
+ 2
import java.util.Scanner; public class sumArray { public static void main(String[] args) { // Program ID System.out.println(" [ sumArray.java] \n"); System.out.println("Sums any array of int numbers"); // Data Table Scanner keyboardIn = new Scanner(System.in); int size; int sumTotal; double average; size = 0; sumTotal = 0; average = 0.0; // Ask for size of array System.out.print("What is the size of the array?(): "); size = keyboardIn.nextInt(); // Make array of size int[] intArray = new int[size]; // Ask for input into the array System.out.print("Enter whole numbers: "); for(int index = 0; index < size; ++index) { intArray[index] = keyboardIn.nextInt(); sumTotal += intArray[index]; } System.out.println(); // Output sum of the numbers System.out.println("The sum of the array is " + sumTotal); // Calculate and output average of the numbers average = (double)sum / size; System.out.println("The average of the array is " + average); } // end of public static void main(String[] args) } // end of public class sumArray
30th Jul 2016, 10:21 PM
Dmitry
Dmitry - avatar
+ 1
Barra, 1) I think you should have a single scanner for the entire program. I mean, as you declared and initialized the scanner in your while loop after a loop is completed, the scanner is destroyed and another one is created. My suggestion is that declare and initialize the scanner where you have initialized the other variables. 2) While calculating average, you haven't upcasted anything. So, when you calculate average, int/int returns int even though you expect float or double. To get the float or double output, you have to upcast either of the numerator or denominator. If you need reference on how to do it, see my code
1st Aug 2016, 1:53 AM
Vamsi Manepalli
Vamsi Manepalli - avatar
0
Cool I'll go do it now 😀
30th Jul 2016, 9:10 PM
codingismylife145
codingismylife145 - avatar
0
//I've done it very manually, as I am new to coding. If anyone knows a simplified version, please tell me as I would assume there is one. public class Program { public static void main(String[] args) { int first = (1), second = (2), third = (3), fourth = (4), fifth = (5); int average = ((first + second + third + fourth + fifth)/5); System.out.println(average); } }
30th Jul 2016, 9:41 PM
codingismylife145
codingismylife145 - avatar
0
With regards to user input, I have never tried that using Java, only using Python so am unsure as to where to start. It would be a great skill, though.
30th Jul 2016, 9:42 PM
codingismylife145
codingismylife145 - avatar
0
there are properly some mistakes in this code as it wouldn't run on my phone. I tried it earlier on my laptop and it worked fine. so it might just be a small stupid mistake. I use import "java.util.Scanner"to get user input. as you can see in the code, I then call scanner and wait for user to submit something. import java.util.Scanner; public class ReturnAverage { public static void main(String[] args) { int numbs[] = new int[5]; int average = 0; int counter = 0; int holder = 0; while(counter < numbs.length){ // get user imput; Scanner userNum = new Scanner(System.in); holder = userNum.nextInt(); numbs[counter] = holder; counter++; } for(int i = 0; i < numbs.length; i++) { average += numbs[i]; System.out.println(average / numbs.length); } } }
30th Jul 2016, 10:01 PM
Barra
Barra - avatar
0
Nice. I like how the user gets to pick how many numbers he wants to enter. I shall use that
30th Jul 2016, 10:37 PM
Barra
Barra - avatar
0
import java.util.Scanner; public class ArrayAverage{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.print("Number of elements in the array? "); int size = scan.nextInt(); //Input array Integer[] array = new Integer[size]; System.out.println("Enter elements of array"); for(int i=0; i<size; i++){ array[i] = scan.nextInt(); } //Printing average System.out.printf("The average of the elements of array is %.2f\n", average(array)); } //Calculating average private static float average(Integer[] array){ int sum = 0; for(int i=0; i<array.length; i++) sum += array[i]; float avg = (float)sum / array.length; return avg; } }
31st Jul 2016, 5:57 AM
Vamsi Manepalli
Vamsi Manepalli - avatar