4 Replies - 1566 Views - Last Post: 17 November 2006 - 12:41 PM Rate Topic: -----

#1 todd3428  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-October 06

Array Help!

Posted 17 November 2006 - 11:12 AM

I've written what i know for this assignment-it complies and runs fine, now what I need to do is manipulate this program in the following ways:

1) Initialize all elements of the array to -1. (dont know what that means)
2) Modify a single score by prompting the user for a new score and the index in which to store it. Do not allow the user to access beyond the bounds of the array.
3) Print all 10 scores and WITH THEIR ASSOCIATED LETTER GRADE sequentially to the screen using a loop.
4) Print a single score by prompting the user to input an index to display. Do not allow the user to access beyond the bounds of the array.
5) When printing the lowest score-it cant be -1

I've been working on this code for hours and your help would be greatly appreciated!
import java.util.Scanner;

public class TestScores
{
	public static void main (String[] args)
	{
	Scanner keyboard = new Scanner(System.in);
		int scores[] = new int[10];
		
		for ( int i = 0; i < scores.length; i++ )
		{
			System.out.println("Enter Score " + (i+1) + ": ");
			scores[i] = keyboard.nextInt();
		}
	 
		// print out exam scores
		System.out.println ("Exam Scores: ");
		for (int i = 0; i < scores.length; i++ )
			System.out.print(scores[i] + " ");
	System.out.println();
			
		// compute average
		double average = 0;
		for (int i = 0; i < scores.length; i++ )
			average = average + scores[i];
		average = average / scores.length;
		System.out.printf ("Average Score is: %-10.3f\n", + average);
	
		// find highest score
		int highest = scores[0];
		for (int i = 1; i < scores.length; i++ )
		{
			if (highest < scores[i] )
				highest = scores[i];
		}
		System.out.println("Highest Score is: " + highest);
	
		// find lowest score
		int lowest = scores[0];
		for (int i = 1; i > scores.length; i++ )
		{
			if (lowest > scores[i] )
				lowest = scores[i];
		}
		System.out.println("Lowest Score is: " + lowest);
	
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Array Help!

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 289
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Array Help!

Posted 17 November 2006 - 11:32 AM

View Posttodd3428, on 17 Nov, 2006 - 06:12 PM, said:

I've written what i know for this assignment-it complies and runs fine, now what I need to do is manipulate this program in the following ways:

1) Initialize all elements of the array to -1. (dont know what that means)
2) Modify a single score by prompting the user for a new score and the index in which to store it. Do not allow the user to access beyond the bounds of the array.

implement the complete specification by implementing and testing part 1), then when complete do part2) etc. To start
1) initialising all elements to -1 means that you loop thru the elements setting the value of each element to -1
for ( int i = 0; i < scores.length; i++ ) scores[i] = -1;



2) you read values for score and index (using keyboard.nextInt()) and store the value in scores[] so
scores[index] = score;


but before that you need to check the array bounds, i.e. index >= 0 and < scores.length

This post has been edited by horace: 17 November 2006 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 todd3428  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-October 06

Re: Array Help!

Posted 17 November 2006 - 11:47 AM

adding "scores[i] = -1"; to the end of my for statment now gives me an error:

TestScores.java:15: cannot find symbol
symbol : variable i
location: class TestScores
System.out.println("Enter Score " + (i+1) + ": ");
^
TestScores.java:16: cannot find symbol
symbol : variable i
location: class TestScores
scores[i] = keyboard.nextInt();
^
2 errors
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 289
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Array Help!

Posted 17 November 2006 - 12:14 PM

View Posttodd3428, on 17 Nov, 2006 - 06:47 PM, said:

adding "scores[i] = -1"; to the end of my for statment now gives me an error:

TestScores.java:15: cannot find symbol
symbol : variable i
location: class TestScores
System.out.println("Enter Score " + (i+1) + ": ");
^
TestScores.java:16: cannot find symbol
symbol : variable i
location: class TestScores
scores[i] = keyboard.nextInt();
^
2 errors

you need a seperate for statement to initialise the array, e.g. put it before the existing one
		for ( int i = 0; i < scores.length; i++ )scores[i] = -1;
		
		for ( int i = 0; i < scores.length; i++ )
		{
			System.out.println("Enter Score " + (i+1) + ": ");
			scores[i] = keyboard.nextInt();
		}
	


Was This Post Helpful? 0
  • +
  • -

#5 todd3428  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 10-October 06

Re: Array Help!

Posted 17 November 2006 - 12:41 PM

Thank you, now how do i print out the corresponding letter grade with the percentage?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1