Proud Sone
Proud Sone

Reputation: 25

How to Match 2 Array and Make A Graph using Java

I'm working on a school project where the user will input Number of Students, Number of Subject ,Subject Names, names of Students and grades of students per subject entered that will be saved into an array. The last part shall make a graph using asterisk * of how many passed. I was able to solve the project except the graph. Here's the exact question for you to understand:

The BIT-CT department asked you to create an application that would stores data to an array. Your program must ask the number of students, number of subjects and subject name. Enter the student name and grade per subject, as average grade per student is displayed. Display a graph that will indicate how many students passed the subject by using asterisk (), passing grade is 75.00, and finally displays the highest average grade among the student.*

SAMPLE OUTPUT

MIDTERM EXAM: STUDENT RANKING

Enter # of Students:  3
Enter # of Subjects:  2
-----------------------------------------------
Enter your 2 subjects: 
1. Java
2. Python
-----------------------------------------------
1. Name: Jan
   Grade in Java: 90
   Grade in Python: 100
Average: 95.0
-----------------------------------------------
2. Name: Jen
   Grade in Java: 100
   Grade in Python: 65
Average: 82.5
-----------------------------------------------
3. Name: Jane
   Grade in Java: 100
   Grade in Python: 90
Average: 95.0
-----------------------------------------------

Highest average score is: 95.0

----GRAPH-----
Java: ***
Python: **

Can you please help me with the graph part? I can't post the whole code since it's a project for atleast 5 sections(60+ students per section) and I'm sure they all be searching for the answer but here's the code for the graph part I'm working, assuming the inputs were same as the sample output,

for (int count = 0; count < noOfSubject; count++){
                System.out.print(subjects[count] +": ");
            for (int counter = 0; counter < allGrades.length; counter ++){
                    if (allGrades[counter] >= 75){
                        System.out.print("*");}
                    else{
                        System.out.print("");}}
            System.out.println("");}

Wrong Output:

 ----GRAPH-----
 Java: *****
 Python: *****

Right Output:

 ----GRAPH-----
 Java: ***
 Python: **

noOfSubject is obviously, number of subjects entered by the user.

subjects is the name of subjects entered by the user.

The allGrades variable is an array consisting of all grades entered regardless of subject. so based on the sample output: allGrades = {90, 100, 100, 65, 100, 90}

I already asked a question about this project but not the last part i'm working of so please don't confuse it as the same question. Thank you!

Upvotes: 1

Views: 192

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

Since you dumped all grades into a single array allGrades, your nested loop should be counting by noOfSubject starting at subject's count, like this:

for (int counter = count ; counter < allGrades.length ; counter += noOfSubject) {
    ...
}

Note: Placing all grades in a single array is not a good idea. You would be better off organizing it in a 2D array, either by student or by subject.

Upvotes: 2

Related Questions