I'm trying to have the user enter an array of numbers(=marks of students). Also, the user himself will decide the size of the array, ie, the number of students in class. Having accepted the marks of each student, i want to store them in an array, display the marks of all students, and compute the average of the class. Will get to the average part later. Can someone help me with this (apparently wrong) piece of code, thank you!!
package chapter2.Arrays;
import java.util.*;
public class Arrays_ExampleOne {
static int size;
static int marks;
static int [] numbers=new int [size];
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Cincinnati Elementary");
System.out.println("Class report details for ");
System.out.println("Enter the number of students in the class");
size=in.nextInt();
System.out.println("Enter the marks of each student");
for(int i=0;i<=size;i++)
{
numbers[i]=in.nextInt();
}
/* System.out.println("The marks of all students are"+ " ");
{
for(int i=0;i<=size;i++)
{
System.out.print(numbers[i]);
}
System.out.println();
}
//int avg=
*/}
}
===========================================================
Revised code, looking to create a function outside of main to calculate an array's average:
package chapter2.Arrays;
import java.util.*;
public class Arrays_ExampleOne {
/*static int size;
static int marks;
static int [] numbers=new int [size];*/
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Cincinnati Elementary");
System.out.println("Class report details for ");
System.out.println("Enter the number of students in the class");
int size;
//static int marks;
//static int [] numbers=new int [size];
size=in.nextInt();
int [] numbers=new int [size];
System.out.println("Enter the marks of each student");
for(int i=0;i<size;i++)
{
numbers[i]=in.nextInt();
}
System.out.println("The marks of all students are:"+ " ");
{
for(int temp:numbers)
{
System.out.print(temp+ " ");
}
System.out.println();
}
int arraySize=numbers.length;
int arraySum=0;
//Calculate the sum of array elements
for(int temp1:numbers)
{
arraySum+=temp1;
}
int average=arraySum/arraySize;
System.out.println("The average of all the students scores is:"+ " "+average);
}
/*public int calculateAverage(int array[])
{
int arraySize=array.length;
}*/
}
============================================================== Added separate code for calculating Average, outside of main.
package chapter2.Arrays;
import java.util.*;
public class Arrays_ExampleOne {
/*static int size;
static int marks;
static int [] numbers=new int [size];*/
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
Arrays_ExampleOne obj=new Arrays_ExampleOne();
System.out.println("Welcome to Cincinnati Elementary");
System.out.println("Class report details for ");
System.out.println("Enter the number of students in the class");
int size;
//static int marks;
//static int [] numbers=new int [size];
size=in.nextInt();
int [] numbers=new int [size];
System.out.println("Enter the marks of each student");
for(int i=0;i<size;i++)
{
numbers[i]=in.nextInt();
}
System.out.println("The marks of all students are:"+ " ");
{
for(int temp:numbers)
{
System.out.print(temp+ " ");
}
System.out.println();
}
/* int arraySize=numbers.length;
int arraySum=0;
//Calculate the sum of array elements
for(int temp1:numbers)
{
arraySum+=temp1;
}
int average=arraySum/arraySize;
System.out.println("The average of all the students scores is:"+ " "+average);*/
int average=obj.calculateAverage(numbers);
System.out.println("The average of all the students is:"+ " "+average);
}
public int calculateAverage(int array[])
{
int arraySize=array.length;
int arraySum=0;
for(int temp: array)
{
arraySum+=temp;
}
int average=arraySum/arraySize;
return average;
}
}
numbersarray first (int[] numbers = new int[size])