954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

multi dimensional arrays

i am new to java and i was trying to solve a program,
a) i wanted to print marks scored by 3 students in 3 subjects.
b) i wanted to get the total marks scored by each student and find out the highest total marks scored.
c)I also want to find the highest mark in each subject and by whom.
i have done a coding i was able to get a) done and b) partly done,can anyone help me with the coding so i can complete the problem.

<strong>class Exam1
{
  public static void main(String args[])
  {
   int [][]subject= {{1,50,66,55},{2,40,42,48},{3,70,90,68}};

  // display subject array//
   for(int i=0;i<3;i++)
    {
     for(int j=0;j<4;j++)
      {
       
       System.out.print(subject[i][j] +"\t");
      }
       System.out.println( "  ");
       
       }
    //to print total marks//
     for(int i=0;i<3;i++)
       {
         int sum=0;

        for(int j=1;j<4;j++)
        { 
         sum= sum+ subject[i][j];
           
       }
       System.out.println("total marks of candidate "+ subject[i][0]	 + "=" +  sum);
     }
    
  }
 }</strong>
newtechie
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

please use code tags.

to find the max of three i would use Math.max(double,double) to find the max of two numbers then do another to find the max of the third number and the max of the first two.

i wrote this method that will take any amount of doubles separated by commas and return the max of all of them.

public static double maxMany(double... doubles){
    if(doubles.length<=0){
        throw new IllegalArgumentException();
    }
    double m = Double.MIN_VALUE;
    for(int i=0;i<doubles.length;i++){ 
       m=Math.max(m,doubles[i]);
    }
    return m;
}
sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 

Here's a Generic extension of your max-value finder where anything that implements Comparable can be compared--

public class Manip{

	public static void main(String... args){


		System.out.println(Manip.<Integer>maxMany(1, 5, 4, 9, 2));
		System.out.println(Manip.<Double>maxMany(2.2, 1.7, 11.2, -7.8, 2.0));
		System.out.println(Manip.<String>maxMany("Joe", "Bob", "Mark", "Luke", "John"));


	}

	public static <T extends Comparable<T> > T maxMany(T... args){
	    if((args.length==0) || args == null){
			throw new IllegalArgumentException();
	    }
	    T t = null;
	    for(int i=0;i<args.length - 1;i++){
	      if(args[i].compareTo(args[i + 1]) > args[i + 1].compareTo(args[i]))
		  	t=args[i];
	    }
	    return t;
	}
}
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

cool, i wouldn't have tried thought to do this with objects, i have never written a compare to() method myself.

doesn't using generics mean that you cannot use primitive data types (int, double, long, short, byte, float, char, and boolean)? won't you have to create instances of the wrapper type first?

public static void main(String... args)


not many people know that you can declare main like this, or at least not many people do, good use of it considering the context of the rest of the code

sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 

cool, i wouldn't have tried thought to do this with objects, i have never written a compare to() method myself.

doesn't using generics mean that you cannot use primitive data types (int, double, long, short, byte, float, char, and boolean)? won't you have to create instances of the wrapper type first?

not many people know that you can declare main like this, or at least not many people do, good use of it considering the context of the rest of the code

If you look carefully at the code, the integers and doubles are used without directly creating objects of their type. The automatic boxing is done on the fly.

But I do believe there are primitive types without a wrapper class, and you would be correct. I would probably make a maxMany method that was specialized for those classes, in that case.

Edit: The code needs a minor edit. Instead of t being null it should initially be the first argument.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

thank you all for your replies

newtechie
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

do they help?

sciwizeh
Posting Pro in Training
457 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 

yes and i also tried a new coding which helped.

public class Exam3
 {
 public static void main(String args[]) 
{
  int[][] subject = { { 1, 50, 66, 55 }, { 2, 40, 42, 78 }, { 3, 70, 92, 68 }, { 4, 90, 42, 44 }, { 5, 43, 45, 41 } };

  // display subject array//
  for (int i = 0; i < subject.length; i++) 
{
   for (int j = 0; j < subject[i].length; j++)
  {
    System.out.print(subject[i][j] + "\t");
   }
   System.out.println("  ");
  }
  //to print total marks//
  for (int i = 0; i < subject.length; i++)
 {
   int sum = 0;
   for (int j = 0; j < subject[i].length; j++)
 {
    if(j!=0) {
     sum = sum + subject[i][j];
    }
   }
   System.out.println("total marks of candidate " + subject[i][0] + "=" + sum);
  }
  //To Find the Maximum,.
  
    //Initialize a variable to find the High Total.
  int maxMarks = 0;
  int studentNumber = 0;
  int []_subjectArray = new int[(subject[0].length -1)];
  int []_studentArray = new int[(subject[0].length -1)];
 
  for(int i=0; i < subject.length; i++)
 {
      
   int sum = 0;
   for(int j=0; j<subject[i].length; j++)
 {
    if(j!=0) 
  {
     sum = sum + subject[i][j];
     
     if(_subjectArray[(j-1)] < subject[i][j]) 
   {
      _subjectArray[(j-1)] = subject[i][j];
      _studentArray[(j-1)] = (i+1);
     }
    }
   }
   //Find the Max Total.
   
   if(maxMarks < sum) 
 {
    //Then Make the Sum as the Maximum at any given time.
    maxMarks = sum;
    studentNumber = (i+1);
   }
  }//End of Number of Rows.
  System.out.println("Maximum Marks : " + maxMarks + " And the Student Number : "+studentNumber);
  for(int i=0; i<_subjectArray.length; i++) 
{
   System.out.println("Max in a Subject # " + (i+1) + " is =" + _subjectArray[i] + " And the Student is : "+_studentArray[i]);
  }
 }
}
newtechie
Light Poster
33 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Please place all posted code inside [code] [/code] or [code=java] [/code] tags.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You