0

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;
    }
}
1
  • YOur code looks ok, you just have to declare the numbers array first (int[] numbers = new int[size]) Commented Feb 22, 2014 at 1:08

2 Answers 2

4

Firstly, you should ask user to input the size first, then you set the array size:

static int size;
static int marks;
static Scanner in=new Scanner(System.in);

public static void main(String[] args) {
    System.out.println("Enter number of student:");
    size = in.nextInt();
    int [] numbers=new int [size];

    //the rest of your code goes here
}

Note: Be careful with your for loop:

for(int i=0;i<=size;i++){
             ^
    numbers[i]=in.nextInt();
}

It will result ArrayIndexOutOfBoundException since you are trying to access the element at index size, which the max index of array with n size is n-1 . Should be < instead of <=

For method calculateAverage, it should looks like this:

public static double calculateAverage(int array[])
{
    double sum=0.0;
    for(int i=0;i<array.length;i++){
       sum+=array[i];
    }
    return sum / array.length;
}

note that this method should be static since you want to use it inside static method, and average always deals with double/float datatype, not int

From main method, you can call it like this:

System.out.println("Average marks:"+calculateAverage(numbers));
Sign up to request clarification or add additional context in comments.

12 Comments

Beyond this not compiling regardless of where it's placed, you've got the right idea.
hold on. I just copy paste from the question :D
@JohannesH. Those two declarations are present at the class level, which leads to the core issue of size being zero when the array is created. Beyond that, there's an off-by-one error where <= is used rather than < in the for loop.
@Vulcan Now (after the edit) they are. Before the edit however, they were used as if they were inside a function, but declared as if they were outside.
Thank You so much, @RafaEl for the advice for both the declaration and the for loop problem. It helped greatly! Now I have been able to print out the average of student scores, but all that is within the main function itself. I am looking to create a separate function(calculateArea(int array[]) that will calculate and return the array average to the main). It's there in the code which I'm including here, can you please help!
|
0
static int size;
static int marks;
static int [] numbers=new int [size];

At the time the array is created, size is still at the default value, which is zero. You always end up with an array of length zero here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.