Results 1 to 9 of 9
  1. #1
    snw19 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Question Finding minimum and maximum of 'n' numbers...please help!

    So I am new to programming and I am sorry if I am posting this is the wrong spot also. :o Hopefully someone will be able to help me figure out my problem! My problem is that I do not know how to find the minimum and maximum of 'n' numbers using and array or loop. The program is supposed to find the minimum, maximum, and average of 'n' number of test scores. I found out how to do the average but not the minimum of maximum. Here is my program so far:

    import java.util.Scanner;

    public class TestScores {

    // the number of tests
    final static int NUM_TESTS = 10;

    public static void main(String[] args){

    Scanner input = new Scanner(System.in);

    // input the number of tests
    System.out.print("Enter the Number of Tests: ");
    int numberOfTests = input.nextInt();

    // initializing the loop
    int testScore;
    int total = 0;
    int i = 1;
    while (i <= numberOfTests){

    // input test scores
    System.out.print("Enter Next Test Score: ");
    testScore = input.nextInt();
    total = total + testScore;
    ++i;
    }

    // calculate the average score
    double testAverage = total / numberOfTests;

    // output the minimum, maximum, and average score
    System.out.println("Test Minimum: " );
    System.out.println("Test Maximum: " );
    System.out.println("Test Average: " + testAverage);
    }
    }

  2. #2
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    Declare two more variables, one for maxTestScore and one for minTestScore. As you are adding up the test scores, keep track of the highest and lowest scores you've seen so far. You'll need a couple of if statements to test for the highest and lowest scores. You can initialize your maxTestScore to 0 and minTestScore to 100. When you're doing the comparisons, you'll see why.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by FlipPoker@gmail.com View Post
    You can initialize your maxTestScore to 0 and minTestScore to 100. When you're doing the comparisons, you'll see why.
    Great tip for the sequence [ -3, -2, -1 ] (the maximum is -1) or the sequence [ 101, 102, 103 [ (the minimum is 101).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Great tip for the sequence [ -3, -2, -1 ] (the maximum is -1) or the sequence [ 101, 102, 103 [ (the minimum is 101).Jos
    I didn't realize we were considering test scores less than 0 or greater than 100. Sorry.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    int largest;
    int[] intArray = {5,4,6,7,2,3,8,1,9,0};
    for (int i=0; i<intArray.length; i++) {
      if (largest < intArray[i]) {
        largest = intArray[i];
      }
    }
    you can do something similar for the smallest number too

  6. #6
    snw19 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    ozzyman, I tried using the code that you suggested and when I try to compile the program I get the following error:

    TestScores.java:41: i is already defined in main(java.lang.String[])
    for (int i = 0; i<intArray.length; i++) {
    ^

    Do you know how to fix that error? If i just take that "int i = 0" out, the program doesn't compile. And thanks for all the help everyone! I think i'm starting to get it!

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    it shouldn't matter that 'i' was defined before because for (int i=0....) is naming and using a Local variable 'i' and it shouldnt be a problem that you have a different variable named 'i'.

    anyhow, use a different variable name and tell me if there are any errors e.g.
    for (int x=0; x<intArray.length; x++) {

  8. #8
    snw19 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Well i changed i to z and it worked. It is very strange how like you said, it's a local variable and shouldn't have been a problem but oh well at least now it works. But I have another question. Since in my program I have a loop for the user to enter in how many tests they are entering then whatever amount they say, the program prompts them that many times to enter in a test Score. So the question I am getting to is in the array, how do I put the scores they entered in that string since the user could be putting in random numbers and not set numbers?

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    in future you need to put separate topics in separate posts to save confusion, get more people to answer your question, and help others who are finding these posts through search engines to answer their own questions.

    try to make crystal-clear what you are asking too. i got the part that you want to ask the user as many times as needed to input test scores. but the rest is unclear to me:
    how do I put the scores they entered in that string since the user could be putting in random numbers and not set numbers

    are you looking for some sort of validation? i.e. value entered must be between two values, e.g. scores cannot be below 0 and max score is 100, therefore:
    Java Code:
    int MIN_SCORE = 0;
    int MAX_SCORE = 100;
    if (scoreInput >= MIN_SCORE && scoreInput <= MAX_SCORE) {
       //code to input the score
    } else {
       System.out.println("Sorry - that is not a valid score. Would you like to try again?");
       //code for user options
    }

Similar Threads

  1. Finding maximum number at index position
    By Shyamz1 in forum New To Java
    Replies: 9
    Last Post: 10-27-2010, 08:14 PM
  2. Maximum Minimum Values
    By z6StringAssasin in forum New To Java
    Replies: 2
    Last Post: 05-09-2010, 12:36 AM
  3. Calculating Maximum and Minimum
    By Ejava in forum New To Java
    Replies: 6
    Last Post: 03-01-2010, 04:36 PM
  4. [SOLVED] Help Finding the Minimum value of an array
    By MSteinman in forum New To Java
    Replies: 18
    Last Post: 04-21-2009, 09:14 AM
  5. To find the Maximum and Minimum in an Array of Strings
    By luscious in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 07-31-2008, 01:51 PM

Tags for this Thread

Posting Permissions

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