PDA

View Full Version : Computer Science HW Help Please


Princess-Snake
11-22-2011, 04:35 PM
Hey. Um... I'm having problems with one of my hw assignments (actually 2, but if you help me solve this one, you'll have helped me solve the other one.) and I really need help with it. I've asked the professor, but his suggestions are not helping me. I'm using Eclipse for my assignment, if that helps anybody.

Question: Write a program that reads an unspecified number of scores ( scores are between 0 and 100 inclusive) into an array and determines how many scores are above or equal to the average and how many scores are below the average. Do not allow scores outside the range to be entered. Assume the maximum number of scores is 30. Do not allow more than 30 scores to be entered. Enter a negative number to signify the end of the input. Please also print the average of the scores and the number of scores that were input.

My Code:
import java.util.Scanner;
public class Scores {
public static void main(String[] args)
{

Scanner scan = new Scanner (System.in);

double total = 0;
int aboveAverage = 0;
int average = 0;
int belowAverage = 0;
double mean; double score;
int count = 0;

double array[] = new double[31];

System.out.print("Please enter a score. Enter up to thirty scores. Enter a negative number to quit.");
score = scan.nextDouble();
for (int i = 0; i < array.length; i++)
while (score >= 0 && score <= 100 && count <= 30)
{
array[count] = score;
count++;
System.out.print("Please enter a score. Enter up to thirty scores Enter a negative number to quit.");
score = scan.nextDouble();
}

for (int i = 0; i < array.length; i++)
{
array[(int) score]++;
score = scan.nextDouble();
}

for (int i = 0; i < array.length; i++)
{
array[i] = scan.nextInt();
total = array[i];
}
mean = total / count;

for (int i = 0; i < array.length; i++)
{
if (score >= 80)
aboveAverage++;
else
{
if (score >= 70 && score <= 79)
average++;
else
{
if (score <= 69)
belowAverage++;
}
}
}

System.out.println("Number of scores input: " + count);
System.out.println("Above Average count: " + aboveAverage); System.out.println("Average count: " + average);
System.out.println("Under Average count: " + belowAverage); System.out.println("Average of scores: " + mean);
}
}

Problems: I receive an error when I input a negative number. When I ignore the negative number and just go on until the list is full, nothing is calculated! It all said "yadayada equals zero" and then after making adjustments, it won't even say "yadayada" anymore! Any help would be appreciated!

Geek King
11-22-2011, 05:06 PM
Problems: I receive an error when I input a negative number. When I ignore the negative number and just go on until the list is full, nothing is calculated! It all said "yadayada equals zero" and then after making adjustments, it won't even say "yadayada" anymore! Any help would be appreciated!

Well, firstly I'm not as familier with the language, but certain things in coding carry over.

By what you've given us of the assignment, you need to check for two special cases before anything else:

1) Is the new input number negative?
2) Is the new input outside of the 0-100 range?

Maybe it is my unfamiliarity with the coding language, but shouldn't you have a loop for filling the array, then move on to the calculation when a negative number is input? I also don't see where you are actually checking for (and handling) the special case of the negative.

Another point, but does "int" variables cover the use of negative numbers? At least one of the languages I used to program with required a seperate variable type to handle those. Keeping in mind, my programing languages included C and PASCAL, and were entered in UNIX vi editor. :lol: :/old:

Princess-Snake
11-22-2011, 05:19 PM
I assumed

System.out.print("Please enter a score. Enter up to thirty scores. Enter a negative number to quit.");
score = scan.nextDouble();
for (int i = 0; i < array.length; i++)
while (score >= 0 && score <= 100 && count <= 30)
{
array[count] = score;
count++;
System.out.print("Please enter a score. Enter up to thirty scores Enter a negative number to quit.");
score = scan.nextDouble();
}

Took care of the negative number input. Is that not correct? How should I fix it? And yes, in Eclipse, a negative number is an integer, or a double or float whatever the case may be.

Princess-Snake
11-22-2011, 05:51 PM
I can't be of much use....but when my son comes home from classes today I can run it past him. I think he mentioned knowing something about Eclipse - he's a 2nd year computer engineering student at the local university. He should be home in a couple of hours, I think, and hopefully he can give some insight for you!

Thank you! It's not due until Sun. but I would prefer to get it done before Thanksgiving.

Pedersen
11-22-2011, 05:58 PM
There's a lot of issues in what you've got there, but almost all of those issues are simply inexperience. You'll improve. In the meantime, allow me to provide an alternative implementation in pseudocode. It'll show some simpler methods, I hope, and help you through it.


main() {
int array = new int[31]
lastscore = 0
int index = 0
int i, above, below, sum
double mean

sum = 0
while (lastscore >= 0 and index < 30) {
lastscore = input("Please enter a score from 0 to 100, enter negative to end")
if (lastscore >= 0 and lastscore <= 100) {
array[index] = lastscore
sum = sum + lastscore
index = index + 1
} else if (lastscore > 100) {
print("Last score was too large, please try again")
}
}

mean = sum / index

for i = 0 to index-1 {
if array[i] < mean then below = below + 1
if array[i] >= mean then above = above + 1
}

print "Mean: " mean
print "Total scores entered: " index+1
print "At or above mean: " above
print "Below mean: " below
}

Princess-Snake
11-22-2011, 06:46 PM
This helps me a lot! Thank you! But, (and I wasn't clear here) by "scores" the professor meant grades. Average is 70 - 79, not the mean. But thank you for your help! This will help me a lot!
Edit
Another thing. Total scores input equals how many scores did you enter. Not the total of the scores. Thanks again!

Seshat
11-23-2011, 08:03 PM
Another point, but does "int" variables cover the use of negative numbers? At least one of the languages I used to program with required a seperate variable type to handle those. Keeping in mind, my programing languages included C and PASCAL, and were entered in UNIX vi editor. :lol: :/old:

Cardinal numbers are 0-and-positive.
Integers are negative-0-positive.

(At least in pure math.)


Since you've solved the initial question, I'll keep my nose out of it. :)

Princess-Snake: always write a draft in pseudocode or one of the drafting 'languages' (flowcharts, data flow diagrams, control flow diagrams, etc etc). Your instructors should be teaching you at least one of those.

You'll more easily catch basic logic errors in your program in the pseudocode/drafting 'language'/diagram, than in the actual code.

Or at least, that's my experience. :)