0

Hi so i am working on a program to calculate 10 students grades using arrays and methods the marks from the students have been given as seen below. the problem i am having is displaying the name average and grade in 3 columns next to each other. the current code im using to print out the arrays prints them likes this

 The Average is
 Average: 82.4
 Average: 81.2
 Average: 66.6
 Average: 72.2
 Average: 52.2
 Average: 59.2
 Average: 63.4
 Average: 90.0
 Average: 73.4
 Average: 68.8
 The grade is
 Grade = B
 Grade = B
 Grade = C
 Grade = C
 Grade = D
 Grade = D
 Grade = D
 Grade = A
 Grade = C
 Grade = C
 BUILD SUCCESSFUL (total time: 0 seconds)

here is the code with name marks and averages i have done any help is really appreciated

import java.util.Arrays;


public class Question2Assignment {

public static double [][] Marks;
public static char [] graded;
public static String [] Name;
public static double [] avg;

public static void main(String[] args) {

  inMarks() ;
  SNames();  

 System.out.println("The Average is"); 
 for (double number : AverageMarks(Marks))
 {

     System.out.print("Average: " + number);
     System.out.println("");
 }


 System.out.println("The grade is");
 for (char grade : Grade(graded))
  {
     System.out.print("Grade = " + grade);
     System.out.println("");

 }
} 

private static void inMarks()
{
    Marks = new double [][] {{85,83,77,91,76},{80,90,95,93,48},{78,81,11,90,73},{92,83,30,69,87},{23,45,96,38,59},{60,85,45,39,67},{77,31,52,74,83},{93,94,89,77,97},{79,85,28,93,82},{85,72,49,75,63}};
}

private static void SNames()
{
    Name = new String [] {"Jognson","Aniston","Cooper","Gupta","Blair","Clark","Kennedy","Bronson","Sunny","Smith"};
}


public static double[] AverageMarks(double[][] Marks)
{

double avg[] = new double [Marks.length];

for (int row = 0; row < Marks.length; row++){
    int sum=0;
    for (int col = 0; col < Marks[row].length; col++){
        sum += Marks[row][col];
    }
    avg[row] = 1.0*sum / Marks[row].length; // calc average

}
return avg;
 }
      public static char [] Grade(char[] Graded)
  {
 char [] graded = new char [10];
 int count = 0;
 for (double number : AverageMarks(Marks)) 
 {

     if(number <50)
     {
         graded [count] = 'F';
     count++;
     }
     else if (number <65)
     {
         graded [count]= 'D';
         count++;
     }
     else if (number <75)
     {
         graded [count] = 'C';
         count++;
     }
     else if (number <85)
     {
         graded [count] ='B';
         count++;
     }
     else   
     {
         graded [count]='A';
         count++;
     }
    }//first for loop
  return graded;
  }


   }//class end
14
  • 1
    You need to think about the flow of what you are doing and what you are trying to do. What you are doing is printing all of the averages and then printing all of the grades. What you want to do is print the right avg and grade with the correct student. You need one for loop to do this and don't use the for each for loop. Commented Feb 13, 2017 at 6:54
  • its obvious it won't print them column wise ! one solution is you need to declare 2 arrays or lists and store all results in them and then use loop to print both elements in arrays. Commented Feb 13, 2017 at 6:57
  • i am not sure how to type it out in 1 for loop using for (double number : AverageMarks(Marks)) cause when i try add (char grade : Grade(graded)) it just doest work which is expected Commented Feb 13, 2017 at 7:00
  • 1
    @SedrickJefferson , I posted the answer before seeing your post. Sorry and we both had similar thoughts. Commented Feb 13, 2017 at 7:12
  • 1
    @SedrickJefferson , I guess we should direct him towards classes and objects. Then he will get the gist of what you are trying to say Commented Feb 13, 2017 at 7:27

2 Answers 2

2
for (int i = 0; i<Name.length; i++) {
    System.out.println(Name[i]+" "+avg[i]+" "+graded[i]);
}
Sign up to request clarification or add additional context in comments.

Comments

0
`double[] averageMarks = AverageMarks(Marks);
char[] grades = Grade(graded);
for (int i = 0; i<Name.length; i++) {
        System.out.println(Name[i]+" "+averageMarks[i]+" "+grades[i]);
    }`

the above given answer points you to proper direction but gives wrong results for this case

1 Comment

There is changes to occur ArrayIndexOutOfBoundsException so that is not able to accepted.

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.