• 03-25-2011, 06:44 PM
snw19
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);
}
}
• 03-25-2011, 09:36 PM
FlipPoker@gmail.com
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.
• 03-25-2011, 09:59 PM
JosAH
Quote:

Originally Posted by FlipPoker@gmail.com
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
• 03-25-2011, 10:06 PM
FlipPoker@gmail.com
Quote:

Originally Posted by JosAH
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.
• 03-25-2011, 10:39 PM
ozzyman
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
• 03-26-2011, 12:02 AM
snw19
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!
• 03-26-2011, 12:09 AM
ozzyman
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++) {
• 03-26-2011, 12:27 AM
snw19
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?
• 03-26-2011, 12:38 AM
ozzyman
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:
Quote:

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:
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 }```