Java Help with average grade assingment

Started by Bad Voo-doo man, Sat 12/03/2005 00:10:08

Previous topic - Next topic

Bad Voo-doo man

I have to write an application that will continue to ask (prompt) a user for test scores until the user enters a
grade of -1 Then the program will calculate and print the users average score, their total number of
scores and the highest score entered. . It might look like this.
Enter test score #1 (-1 to quit) : 87
Enter test score #2 (-1 to quit) : 82
Enter test score #3 (-1 to quit) : 94
Enter test score #4 (-1 to quit) : 98
Enter test score #5 (-1 to quit) : 73
Enter test score #6 (-1 to quit) : 98
Enter test score #7 (-1 to quit) : 80
Enter test score #8 (-1 to quit) : -1
Total grades entered is 7
Your average grade is : 87
Your Highest grade is 98

So far I have:

Code: ags
public class AveGrade {
	
	public static void main(String args[]) {
		
		int TotalGrades = 0, TestScore = 0, average, highest, counter = 0;
		int stupid = 0;
		int grades[];
		int arrayCounter = 1;
			
		do {
			
			for (int i = 1; i > 0; i++) {
	    		
	    		TextIO.put("What is Test Score #" + i + ( (" (-1 to stop): ")));
	    		grades = new int[arrayCounter];
	    		grades[arrayCounter - 1] = TextIO.getInt();
	    		counter++;
	    		arrayCounter++;
	    			    			    	
	    		if (grades[arrayCounter - 2] == -1) {
	    			
	    			for (int j = 0; j < (arrayCounter - 2); j++)
	    				
	    				System.out.println(grades[j]);
	    			return;
	    		}
	    	}
	    }while(stupid == 0);
	 }
}


What I need help on is storing the value into the array and reading the value from the array, getting the highest score, etc.
<img>Image Comming Soon!</img>
The Fall of Reach (A Halo fan game)
STORY: 100%
GRAPHICS: 2%
CODE: .56%

edmundito

#1
Well, first of all, an array has a length property, so you don't need to have a counter.

you can just have:
Code: ags
for (...; j < grades.length - 1; ... )


and instead of having an extra variable that checks on the loop, you can just keep looping until the user enters -1, then do the calculations needed.

More importantly, though, when you have an unknown amount of input, an array is the worst thing to choose from, because arrays are usually of a fixed size unless you want to write a resize method. Then again, I don't know what programming level are you or anything...

Bad Voo-doo man

Im a beginner, i understand exactly what your saying...an array was the only way i could think of for storing the number into a new int for each time the user inputs a number...if that makes any sense at all  :)
<img>Image Comming Soon!</img>
The Fall of Reach (A Halo fan game)
STORY: 100%
GRAPHICS: 2%
CODE: .56%

MillsJROSS

Unless you need to print the number back to the screen, there is no reason to put the specific number into an array. Just think about it from a purely mathmatical perspectice. What is average? It's the sum of some numbers divided by how many numbers there are. So all you need is an integer that sums up all the grades after each entry. Or in other words add the grade entered into an integer, making sure the integer is set to zero in the begining of the program( So if 81 is the first grade, int sum = 81; 92 the second grade, int sum = (81 + 92 ), and so on ).  Highest score is easy. You just make another int, once again set to zero initially, and compare it with the new grades. If it's bigger or equal that nothing is done, if it's smaller, then set it to equal the large number.

I'd show you the code, but I think all the information above is more than enough. It's all pretty simple, and it's better if you figure out the code from your textbook/teacher/memory. Good luck!

-MillsJROSS

Kairus

If you are in a beginners course then I think they don't let you use vectors or lists which are what would be used to handle an unknown amount of values. Nonetheless, if that is the only information you must show about the given entry, you don't have to worry about keeping all the information in an array.

You must show the average, the total number and the highest. That information can be kept in accumulators rather than having to calculate it all in each step of the iteration.

You'll need three accumulators (integers), let's call them: total, sum and max. Initialize them as total = 0, sum = 0, max = -1;
In each iteration you should get the new number (num) from the user, add 1 to total, add num to sum, and if num > max then max = num.

You must do that in all cases except when the num is -1 which means the end of the iteration.
Once the iteration ends you just write the information:
total is the total number of scores
sum/total is the average number of scores (you should do a cast so the number gets converted into floating point)
max is the maximum score

With all that you wouldn't be using any array to keep all the numbers.
That's a pseudocode, if you get the idea you should go and write the code... you weren't expecting us to write it... right? :)
Download Garfield today!

DOWNLOADINFOWEBSITE

Bad Voo-doo man

lol..thanks A LOT you guys, and no i wasn't expecting YOU guys to write it  ;)
<img>Image Comming Soon!</img>
The Fall of Reach (A Halo fan game)
STORY: 100%
GRAPHICS: 2%
CODE: .56%

Bad Voo-doo man

Well thanks to all of you...especially Kairus, its finished...heres my code:
Code: ags

public class AveGrade {
	
	public static void main(String args[]) {
		
		int total = 0, sum = 0, max = -1, num, average = 0;
		
		for (int i=1; i>0; i++) {
			
			TextIO.put("Please enter score #" + i + " ");
			num = TextIO.getInt();
			TextIO.putln("");
			total += 1;
			sum += num;
			
			if (num > max) {
				max = num;
				
			}
			
			else if (num == -1) {
				total = (total - 1);
				average = (sum / total) + 1;
				System.out.println("Ammount of Test Scores: " + total);
				System.out.println("Highest Test Score: " + max);
				System.out.println("Average Grade: " + average);
				return;								
			}
		}
	}
}
<img>Image Comming Soon!</img>
The Fall of Reach (A Halo fan game)
STORY: 100%
GRAPHICS: 2%
CODE: .56%

Kairus

That's more or less what I was thinking but there's something that puzzles me about your code...
What's that for (int i = 1; i > 0; i++) doing???
Sorry to tell you, but if I were your professor I would kill you, despite the fact that the program would work with that.
That's not nice programming.
The for statement is used when you know how many steps the iteration will have before starting the iteration. If you don't know (like this case) you should use a while or do...while statement.

"for (int i = 1; i > 0; i++) {...}"
If we didn't know that the variable will be changed inside the iteration then that for would never end (it would end, but it's more likely to be a mistake than a planned ending).

Changing the value of the iterator variable inside a for may work in java but I can assure you it doesn't work in all languages, try doing it in Pascal and you'll have a lot of not very logical results.

So you should take as a general rule never to change the value of the iterator variable inside a for statement.

On second place, to avoid messing with the total -1 and sum/total + 1 thing that's in the end of your program, maybe you should check that the number entered is not -1 right after you receive it so you don't add 1 to total, etc. That way you can just use total, sum/total and max as correct values.

The sum/total + 1 doesn't give you the proper average, you must be thinking of something more like (sum + 1)/ total, but better don't add -1 to the sum and the problem would be already fixed.
Download Garfield today!

DOWNLOADINFOWEBSITE

Kweepa

A better way of writing an infinite loop is:
Code: ags

while (true)
{
  blah blah
}
Still waiting for Purity of the Surf II

edmundito

it doesn't need to be infinite:
Code: ags

while(num != -1)
{
  // code
}

Bad Voo-doo man

Thanks again you guys, i really like all of your suggestions..my teacher passed it off for full credit the way its done now...almost done with the class :)..but yea, thanks again you guys
<img>Image Comming Soon!</img>
The Fall of Reach (A Halo fan game)
STORY: 100%
GRAPHICS: 2%
CODE: .56%

SMF spam blocked by CleanTalk