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, 2022 by Lexus Guevara (1,010 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, 2022 by Peter Minarik (84,720 points)
selected May 19, 2022 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 17, 2022 by Lexus Guevara (1,010 points)
I tried it actually, but im not good in making methods and also im not yet good in imports  like java.util.Random. There are some parts of my code that doesn't really in the question above but im just making sure that the process still works


import java.util.Scanner;

public class dyCal{
   
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the number of sections: ");
        int numSec = input.nextInt();
        //number of sections
        System.out.println(numSec);
       
        //inputting the number of students per section
        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();
        }
       
        // inputing the exam scores of students per section
        int exScore = 0;
        int [][] exScore1 = new int[exScore][];
            System.out.print("Enter the exam score of students in section : " );
            exScore = input.nextInt();
            System.out.print(exScore);
    }
}
commented May 17, 2022 by Lexus Guevara (1,010 points)
I can't move forward because I don't really know how array works, I already searched the internet and what im trying to do is like this

Supposed to say that one whole bracket has an elements of:

[Section 1[student 1[exam score of student 1]], until [student N [exam score of student N]]], until [Section X[student X[exam score of student X], until [student Y[exam score of student Y]]]

The examscores are inside the array of students, while the students array are inside of section array (i dont know the definition or what to call it but its like that)
the N, X, and Y are the last numbers inputed by the user.

lastly, im sorry if i hadn't put my code way back then, i just thought that it's too long to read and forgot to put it in the comments
commented May 17, 2022 by Peter Minarik (84,720 points)
edited May 18, 2022 by Peter Minarik
You can try this:

import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the number of sections: ");
        int numSec = input.nextInt();
        //number of sections
        System.out.println(numSec);
       
        //inputting the number of students per section
        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();
        }
       
        // inputing the exam scores of students per section
        int [][] scores = new int[numSec][];
        for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)
        {
            System.out.println("Section: " + sectionIndex);
            scores[sectionIndex] = new int[numStu[sectionIndex]];
            for (int studentIndex = 0; studentIndex < numStu[sectionIndex]; studentIndex++)
            {
                System.out.print("Score for student " + studentIndex + ": ");
                scores[sectionIndex][studentIndex] = input.nextInt();
            }
        }
    }
}


The point is, that when you do multi-dimensional arrays, you need to initialize them in multiple steps. First, you initialize the first dimension (new int[numSec][]), later, when you try to access the next dimension, you initialize them then for the first time (scores[sectionIndex] = new int[numStu[sectionIndex]];)

I hope it helps.

Keep on trying. Ask more questions if you get stuck.

Good luck!
commented May 17, 2022 by Lexus Guevara (1,010 points)
Thank you very much, I tried making an average per section but it is now midnight on my country and felt sleepy, I will try to comment here again tomorrow to put my codes for average per section. Goodnight!
commented May 18, 2022 by Lexus Guevara (1,010 points)
i wanna ask something here, where did inputed elements went? i'm kinda confused because there is no like "scores[i] =" before input.nextInt? or like there is no storage that i can access the scores from the student

System.out.print("Score for student " + studentIndex + ": ");
                input.nextInt();
commented May 18, 2022 by Peter Minarik (84,720 points)
Yeah, I missed that part (being abour 2 in the morning when I wrote that, lol).

Correctly it should have been

scores[sectionIndex][studentIndex] = input.nextInt();

I'll fix it in my previous comment.
commented May 18, 2022 by Lexus Guevara (1,010 points)
can I access each examscores per student per section? because I'm trying to access them but it is always the last exam score will be calculated to get the average. I tried also compute the average of all exam scores of all sections but somehow I cant access the scores if it is not inside the for loop. and I also revised the code that you gave to me yesterday like the System.out.print part because I want to combine them. Thank you for your patience and I'm sorry if I have so many questions

int[][] scores = new int[numSec][];
        for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++)
        {
            //new Array for section index
            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 i: scores[sectionIndex][studentIndex])
        {
            int sum = 0;
            sum += scores[sectionIndex][studentIndex];
            double average = 0;
            average = sum / scores[sectionIndex][studentIndex];
        }
        System.out.print(average);
commented May 18, 2022 by Lexus Guevara (1,010 points)
int[][] scores = new int[numSec][];
        for (int sectionIndex = 0; sectionIndex < numSec; sectionIndex++){
            //new Array for section index
            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();
                //new Array for scores
            int exScores[] = new int[scores[sectionIndex][studentIndex]];
                int sum = 0;
                for( int i = 0; i < scores.length; i++){
                    sum += exScores.length;
                }
                average = sum / exScores.length;
            }   
            x = sectionIndex;
        }
        System.out.print("The average score in section " + (x+1) + " : " + average);

These are my two versions that I used but I cant access each examscores and also the average is not yet finish. the x and average variables are already called outside the for loop that's why there is no int before them
commented May 18, 2022 by Peter Minarik (84,720 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, 2022 by Lexus Guevara (1,010 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, 2022 by Peter Minarik (84,720 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, 2022 by Lexus Guevara (1,010 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, 2022 by Peter Minarik (84,720 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.
...