Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I need some code here, i'm so confused in arrays

+8 votes
asked May 17 by Lexus Guevara (940 points)

Example Scenario
 Dr. Dela Cruz teaches three sections of his Intro to programming class. 
He has
12 students in section 1, 8 students in section 2, and 10 students in section 3. 
 You will find the following exam scores of each student in every section: 
  • Section1 - 80, 85, 90, 85, 94, 93, 80, 80, 93, 82, 80, 82 (12 integer scores) 
  • Section2 - 85, 90, 87, 88, 90, 92, 88, 92 (8 integer scores) 
  • Section3 - 90, 92, 83, 85, 88, 90, 93, 80, 82, 81 (10 integer scores) 

 Dynamic Exam Scores Calculator:

Create an application that can dynamically accept a number of sections, the number of students in a section, and can stores their exam scores then
determine the following: 
 1. The average exam score for each individual section 
 2. The average exam score for all the students in all sections 
 3. The highest exam score among all sections. 
4. The lowest exam score among all sections. 
5. The exam score that appears most frequently among all three sections
(Mode).
 6. The middle exam score among all three sections (Median). 

Note: The generation of the above task should be carried out by value-returning
methods.  


Example Program output:

Enter the number of section: 3
Enter the number of students in section 1: 12
Enter the number of students in section 2: 8
Enter the number of students in section 3: 10
Enter the exam score of student number 1 in section 1: 80
Enter the exam score of student number 2 in section 1: 85
Enter the exam score of student number 3 in section 1: 90

Enter the exam score of student number 12 in section 1: 82
Enter the exam score of student number 1 in section 2: 85
Enter the exam score of student number 2 in section 2: 90
Enter the exam score of student number 3 in section 2: 87

Enter the exam score of student number 8 in section 2: 92
Enter the exam score of student number 1 in section 3: 90
Enter the exam score of student number 2 in section 3: 92
Enter the exam score of student number 3 in section 3: 83

Enter the exam score of student number 10 in section 3: 81
The average score in section 1: 80.33
The average score in section 2: 89.00
The average score in section 3: 86.40
The average score in all sections: 86.66
Highest score in section 1: 94
Highest score in section 2: 92
Highest score in section 3: 93
Highest score in all sections: 94
The lowest score in section 1: 80
The lowest score in section 2: 85
The lowest score in section 3: 81
The lowest score in all sections: 80
Mode: 80, 90
Median: 87.50

1 Answer

0 votes
answered May 17 by Peter Minarik (58,190 points)
selected May 19 by Lexus Guevara
 
Best answer

You should really attempt to solve your homework before asking someone else to do it for you.

We're happy to review your solution and give you tips, and find bugs, but it should be yours.

commented May 18 by Peter Minarik (58,190 points)
Your loop to calculate the average is wrong. Your loop condition only has one iteration as scores[sectionIndex][studentIndex] is the score of a single student; it is one element, not a collection of elements.

It's easiest to use the same loop logic as before:

        for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)
        {
            for (int studentIndex = 0; i < numStu[sectionIndex]; studentIndex++)
            {
                // TODO: Calculate average for the given sectionIndex
            }
        }
commented May 18 by Lexus Guevara (940 points)
Im still doing it wrong, Im still confused on how of the each scores will per section will get the sum then divide by the number of students to get the average
I'm sorry

int[][] scores = new int[numSec][];
        for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++){
            scores[sectionIndex] = new int[numStu[sectionIndex]];
            for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++){
                System.out.print("Enter the exam score of student " + (studentIndex+1) + " of section " + (sectionIndex+1) + " : ");
                scores[sectionIndex][studentIndex] = input.nextInt();
            }
        }
        for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)
            {
            for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)
            {
               // TODO: Calculate average for the given sectionIndex
                int sum = 0;
                sum += scores[sectionIndex].length;
                average = sum / studentIndex;
            }
            System.out.println("The average score in section " + (sectionIndex+1) + " : " + average);
            }
commented May 18 by Peter Minarik (58,190 points)
You should not set the sum to 0 in every step in the inner loop, rather before you enter the inner loop.

Also, I don't see why you increment the sum with the number of students (scores[sectionIndex].length) when you should use the scores for each student.

Last, but not least, dividing the sum with the student index has nothing to do with the average. And it will also result in a division by zero runtime error.

Try something like this instead:

        int[] average = new int[numSec];
        for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)
        {
            int sum = 0;
            for (int studentIndex = 0; i < numStu[sectionIndex]; studentIndex++)
            {
                sum += scores[sectionIndex][studentIndex];
            }
            average[sectionIndex] = sum /= numStu[sectionIndex];
        }
commented May 18 by Lexus Guevara (940 points)
i don't still know why but the averages are still not working, I tried making it double [] average so that it can accept decimal points: here is the updated code from your teachings, There is a lot work to go but Thank you for understanding

import java.util.Scanner;

public class Act7
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of sections: ");
        //inputting the number into numSec
        int numSec = input.nextInt();
       
       
        //inputting the number of students per section
        //creating an Array called numStu to create a series of containers to store the number of students
        int[] numStu = new int[numSec];
        for (int i = 0; i < numSec; i++){
            System.out.print("Enter the number of students in section " + (i+1) + " : ");
            numStu[i] = input.nextInt();
        }
       
        // new 2d Array to put the exam scores of students per section
        int[][] scores = new int[numSec][];
        for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++){
            scores[sectionIndex] = new int[numStu[sectionIndex]];
            for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++){
                System.out.print("Enter the exam score of student " + (studentIndex+1) + " of section " + (sectionIndex+1) + " : ");
                scores[sectionIndex][studentIndex] = input.nextInt();
            }
        }
        double[] average = new double[numSec];
        for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)
        {
            int sum = 0;
            for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)
            {
                sum += scores[sectionIndex][studentIndex];
            }
            average[sectionIndex] = sum /= numStu[sectionIndex];
            System.out.println("The average score in section " + (sectionIndex+1) + " : " + average[sectionIndex]);
        }
       
    }
}
commented May 20 by Peter Minarik (58,190 points)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...