Results 1 to 4 of 4
  1. #1
    asadzarrar is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default Urgent help Please!!!

    I need help with this program...there is an error and i have to fix it but i can'tget it to print the max and second max element in an array.
    thanks,

    // This program prints the maximum and the second maximum elemnet in an array

    class A3Q3b
    {
    public static void main (String[] args)
    {
    // DECLARE VARIABLES/DATA DICTIONARY
    int [] a; // GIVENS: the array of values
    // int index; // INTERMEDIATES: index through array
    int first = 0;
    int second = 0; // RESULTS: values of Max and Second Max

    System.out.println( "Please input the array: " );
    a = ITI1120.readIntLine();

    // BODY OF ALGORITHM
    // first = a[0]; second = a[0];
    //index = 0;
    for (int i = 0; i < a.length; i++)
    {
    if (a[i] > first)
    {
    second = first;
    first = a[i];
    }
    }
    for (int j = 0; j < a.length; j++)
    {
    if (first > a[j])
    {
    second = a[j];
    }
    }
    //index ++;
    System.out.println("The maximum number is " + first);
    System.out.println("The second number is " + second);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by asadzarrar View Post
    Urgent help Please!!!
    Please use meaningful specific subject headers
    Don't flag your question as "Urgent" even if it is for you


    I need help with this program...there is an error and i have to fix it but i can'tget it to print the max and second max element in an array.
    thanks,
    Be explicit about your question


    // This program prints the maximum and the second maximum elemnet in an array

    class A3Q3b
    {
    public static void main (String[] args)
    {//.....
    When asking about code

    Welcome to the forum. Your post has broken many of the "rules" that define what a good post is, what makes a question answerable. This is your first post here and as such we understand that asking a question is a skill that needs to be learned. Please read the links provided and it will help you to better formulate your questions here and in the process get better answers. Best of luck and welcome!

  3. #3
    asadzarrar is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default I see...

    Thanks I'll work on that.

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    I've cleaned the code up a bit for everyone.
    I also deleted the bit reading the array of integers (because I assume most people dont have access to that package)
    I replaced it with some sample integers

    Java Code:
    package test;
    
    class test {
        public static void main (String[] args) {
            // DECLARE VARIABLES/DATA DICTIONARY
            int [] a = { 5040, 1, 1, 2, 6, 24, 120, 720,  5039, 40, 7000}; // GIVENS: the array of values
            // int index; // INTERMEDIATES: index through array
            int first = 0;
            int second = 0; // RESULTS: values of Max and Second Max
    
            System.out.println( "Please input the array: " );
           
            // BODY OF ALGORITHM
            // first = a[0]; second = a[0];
            //index = 0;
            for (int i = 0; i < a.length; i++) {
                if (a[i] > first) {
                    second = first;
                    first = a[i];
                }
            }
            for (int j = 0; j < a.length; j++) {
                if (first > a[j]) {
                    second = a[j];
                }
            }
            //index ++;
            System.out.println("The maximum number is " + first);
            System.out.println("The second number is " + second);
        }
    }
    What is the J loop meant to do? I dont really see and point in it.

    Think about what you need to do. You only need to go through the loop once.
    For each value, check if its max.
    If its not, check if its the second max.

    hope this helps

Similar Threads

  1. [URGENT]SHA Encryption System...need urgent helps
    By java_idiot in forum New To Java
    Replies: 6
    Last Post: 05-02-2010, 10:04 AM
  2. Urgent
    By Anchal in forum Java Servlet
    Replies: 1
    Last Post: 04-10-2010, 05:35 PM
  3. Please Help me urgent help please
    By ravjot28 in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 01:59 PM
  4. [very urgent]...
    By mharblez in forum Advanced Java
    Replies: 2
    Last Post: 02-18-2009, 03:15 PM
  5. Hi, need some urgent help!
    By jdark in forum New To Java
    Replies: 2
    Last Post: 04-18-2008, 06:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •