how can i find the total and the The highest marks in 2d array

1.5k Views Asked by At

I have 2 tables, the first one displays the name of the student, and the second one displays the marks for the student. each student has 2 semesters. I wrote it like this:

import java.util.Scanner;    

public class Main    
{    
    public static void main (String args[])    
    {   
        Scanner Sc=new Scanner(System.in);   
        int Number,i,j,q,n;

        System.out.println(" how many student in your group ?");
        Number=Sc.nextInt();
        
        //create the Array
        String [] tab =new String [Number];
        for(n=0;n<Number;n++)
        {
            System.out.println(" Enter studnt name : "+(n + 1) );
            tab[n]=Sc.nextLine();
        }
         
        int [][] marks=new int [2][Number];
        for(j=0;j<Number;j++) 
        {
            for (i=0;i<1;i++)
            {
                System.out.println(" Enter the marks of the first semster : "+(j + 1));
                marks [i][j]=Sc.nextInt();
            }
            for (q=1;q<2;q++)
            {
                 System.out.println(" Enter the marks of the Second semseter  : "+(j + 1));
                 marks [q][j]=Sc.nextInt();
            }
        }    
    }
}

i = for the first rows which will display the marks for the first semester for each student

q = for the second rows which will display the marks for the second semester for each student

Now I want to find the total marks for each student and the highest marks. ( highest marks for one student mean he/she gets the highest marks in which semester and the total also for the one student total = first semester + second semester). But I don't know how I will write it if anyone can help me I would be grateful to him.

2

There are 2 best solutions below

0
On BEST ANSWER

Prerequisite

Before calculating highest mark and total marks for each student, you need two create two arrays to store that data. The size of the array will be the number of students.

int[] totalMarks = new int[studentsCount];
int[] highestMark = new int[studentsCount];

Logic

Once you have these, total and highest marks can be calculated in two different loops or one single loop.

For any student i,

highestMark[i] = max(marks[0][i], marks[1][i])
totalMarks[i] = marks[0][i] + marks[1][i]

To do the same process for all the students, use a simple for loop from i = 0 to studentCount.


Improvements

Your program can also be improved. Start giving meaningful name to your variables. Instead of NUMBER use studentCount. Even though Sc works okay, scanner is better. Another improvement is:

for(j=0;j<studentCount;j++) 
{
    System.out.println ("For student " + (j+1) + ": ");

    System.out.println("Enter the marks of the first semster: ");
    marks [0][j]= scannner.nextInt();
        
    System.out.println("Enter the marks of the second semseter: ");
    marks [1][j]= scanner.nextInt(); 
}    

You do not need for loop to input the mark for any student i for two semester.


I have not provided any code. I have given many hints which should help you solve the problem on your own. However, if you still face any problem. Do comment.

0
On

It took me a while to understand your data structure and I'd recommend using Objects like, having a class for a student. This class holds attributes about marks and semester and so on, that would make it easier to operate with the values and easier to understand.

Variables should be camelCase, which means they should start with a lower case character, e.g. Number should be number.

I assume the marks are treated more like points, if the value is higher, the mark is better.

For getting the accumulated score of marks:

int totalScore = Stream.of(marks).flatMapToInt(IntStream::of).sum();

The sum() method returns the accumulated score of all values in the stream.

For getting the highest score of a semester:

int highestScore = Stream.of(marks[semester]).flatMapToInt(IntStream::of).max().orElseGet(0);

The max() method returns an Optional. If the stream was empty (if the student did not have any marks in that semester) it would return 0, because of the orElseGet(0). In all other cases it returns the highest value of the semester.