User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,015 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,660 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 478 | Replies: 8
Reply
Join Date: Jul 2008
Posts: 18
Reputation: newtechie is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
newtechie newtechie is offline Offline
Newbie Poster

multi dimensional arrays

  #1  
Jul 24th, 2008
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.

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);
     }
    
  }
 }
Last edited by Tekmaven : Jul 28th, 2008 at 2:35 pm. Reason: Code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 320
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: multi dimensional arrays

  #2  
Jul 24th, 2008
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;
}
Last edited by sciwizeh : Jul 24th, 2008 at 4:50 pm.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 776
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Rep Power: 4
Solved Threads: 76
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Master Poster

Re: multi dimensional arrays

  #3  
Jul 24th, 2008
Here's a Generic extension of your max-value finder where anything that implements Comparable can be compared--

  1.  
  2. public class Manip{
  3.  
  4. public static void main(String... args){
  5.  
  6.  
  7. System.out.println(Manip.<Integer>maxMany(1, 5, 4, 9, 2));
  8. System.out.println(Manip.<Double>maxMany(2.2, 1.7, 11.2, -7.8, 2.0));
  9. System.out.println(Manip.<String>maxMany("Joe", "Bob", "Mark", "Luke", "John"));
  10.  
  11.  
  12. }
  13.  
  14. public static <T extends Comparable<T> > T maxMany(T... args){
  15. if(args.length<=0){
  16. throw new IllegalArgumentException();
  17. }
  18. T t = null;
  19. for(int i=0;i<args.length - 1;i++){
  20. if(args[i].compareTo(args[i + 1]) > args[i + 1].compareTo(args[i]))
  21. t=args[i];
  22. }
  23. return t;
  24. }
  25. }
  26.  
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 320
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: multi dimensional arrays

  #4  
Jul 24th, 2008
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
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Join Date: Jun 2008
Location: WA, USA
Posts: 776
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Rep Power: 4
Solved Threads: 76
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Master Poster

Re: multi dimensional arrays

  #5  
Jul 24th, 2008
Originally Posted by sciwizeh View Post
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.
Last edited by Alex Edwards : Jul 24th, 2008 at 10:13 pm.
Reply With Quote  
Join Date: Jul 2008
Posts: 18
Reputation: newtechie is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
newtechie newtechie is offline Offline
Newbie Poster

Re: multi dimensional arrays

  #6  
Jul 25th, 2008
thank you all for your replies
Reply With Quote  
Join Date: Jun 2008
Location: new york
Posts: 320
Reputation: sciwizeh is on a distinguished road 
Rep Power: 1
Solved Threads: 17
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Whiz

Re: multi dimensional arrays

  #7  
Jul 25th, 2008
do they help?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote  
Join Date: Jul 2008
Posts: 18
Reputation: newtechie is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
newtechie newtechie is offline Offline
Newbie Poster

Re: multi dimensional arrays

  #8  
Jul 28th, 2008
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]);
  }
 }
}
Last edited by Tekmaven : Jul 28th, 2008 at 2:33 pm. Reason: Code tags
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,840
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 282
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: multi dimensional arrays

  #9  
Jul 28th, 2008
Please place all posted code inside [code] [/code] or [code=java] [/code] tags.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 1:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC