Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: I need help in finding the max and min value of total of 2d array if possible please

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help in finding the max and min value of total of 2d array if possible please

    Hi everyone
    I thank you a lot for taking the time to read my code
    I need to display a two dimensional array, using two classes, calculate the total of each row, and display the maximum total and the minumum total( I was able to do everything except for the maximum and minimum) and I have to submit my project on wednesday, tried my best and thats what I could formulate, please help
    This is my code for the first class:
    package companyproject;
    public class company {      
        private int [][] arrayname={{88,45,73},{33,45,58},{58,45,27},{28,45,55}};
        private int max=0;
        private int min=0;
        public void Output ()
        {  for (int x=0; x<3; x++)
           {System.out.printf("\tProduct %d \t", x+1);  }
           System.out.printf("Total");
           for (int row=0; row<arrayname.length; row++)
           {System.out.println("");
            System.out.printf("Salesperson %d\t", row+1);
            for (int column=0; column<arrayname[row].length; column++)
                System.out.printf("%d\t\t",arrayname[row][column]);
            int total = totalx (arrayname[row]);
            System.out.printf("%d", total);
           }
        }
        public int totalx (int [] arrayname)
        {  int total=0;  for (int aaa=0; aaa<arrayname.length; aaa++)
           {total=total+arrayname[aaa];}
           if (total > max) {max=total;  }
           System.out.printf("The maximum number is %d", max);
           if (total < min) {min=total;}
           System.out.printf("The minimum number is %d", min);
           return 0;
        }
    }
    Thats the code for the second class in which I should display the first class:
    package companyproject;
     
    public class companytext {
        public static void main (String [] args)
     
    {
     
    company totally = new company ();
     
    totally.Output();
     
    }
     
     
    }
    Unfortunately thats what Im getting as an output

    The maximum number is 206The minimum number is 00
    after each row (Under total), while I should get the sum of the row

    I should get the maximum number and minimum number under the 2d array once
    I thank you a lot again for dedicating your time to help me


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need help in finding the max and min value of total of 2d array if possible ple

    You should try debugging your code by adding println statement to show the values of variables as they change and to show logic flow.

    Can you show what the output should look like? I don't understand your comments.

    Cross posted at
    I need help in finding the max and min value of total of 2d array if possible please - Dev Shed
    Last edited by Norm; May 23rd, 2011 at 08:14 PM.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help in finding the max and min value of total of 2d array if possible ple

    Yes dear, I posted this post in another java forum but MrFujin was not able to help because that other forum didn't allow the attachment of images

  4. #4
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help in finding the max and min value of total of 2d array if possible ple

    That should be the output dear

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I need help in finding the max and min value of total of 2d array if possible ple

    One trick that is useful in finding the max and the min of a value is to initialize the starting value for the max to a number smaller than the smallest data value and the starting value for the min to a number BIGGER than the biggest data value.

    If you change this variable so that the totals are changed:

    private int [][] arrayname={{88,45,73},{33,45,58},{158,45,27},{28,4 5,55}};

    and look at the output, you'll get some info on how your program is working.


    You need to step thru your logic and see why the output is being printed the way it is.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: I need help in finding the max and min value of total of 2d array if possible ple

    Quote Originally Posted by Norm View Post
    You should try debugging your code by adding println statement to show the values of variables as they change and to show logic flow.
    Debugging With System.out.println()
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Finding a substring from an array within a string argument
    By sitruz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2011, 10:33 AM
  2. Finding the length of a two dimensional array
    By petemyster in forum Java Theory & Questions
    Replies: 2
    Last Post: December 12th, 2010, 10:21 PM
  3. Find total number less than average
    By maximus20895 in forum Collections and Generics
    Replies: 2
    Last Post: December 1st, 2010, 01:46 PM
  4. finding specified NUMBERS in an array
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 10:35 PM
  5. Finding the highest number in an array?
    By halfwaygone in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 17th, 2010, 03:56 PM