ARRAY PROGRAM - NEED HELP WITH OUTPUT FORMATTING

Topics merged... avoid double posting

Page 1 of 1

2 Replies - 3013 Views - Last Post: 01 November 2008 - 06:19 PM Rate Topic: -----

#1 Sally1217801999   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-October 08

ARRAY PROGRAM - NEED HELP WITH OUTPUT FORMATTING

Posted 01 November 2008 - 05:49 PM

/**
* Programming Assignment # 2 studentScore.java
* I have designed this program to read (fictitious) student test scores
* in the range 0-100 and to have it print the following statistics to
* two decimal places:
* The average score; the number of students whose score is equal to or above
* the average; the student with the lowest score; and the student with the
* highest score.
*
* Emily Koopmann
* E00835771
* COSC 111 Section 5
* October 28, 2008
*/

import java.util.Scanner;// For keyboard input
public class studentScore

{
public static void main (String [] args)
{
Scanner in = new Scanner (System.in);
//User will be prompted for number of students.
System.out.print("Please enter the number of students in the class: ");
int numStudents = in.nextInt();

//Create 2-dimensional array

int[] ids = new int[numStudents];
double[] scores = new double[numStudents];

//Read IDs and scores

for(int i = 0; i <numStudents; i++)
{
System.out.print("Enter 1 ID number and 1 score for each student: ");
ids[i] = in.nextInt();
scores[i] = in.nextDouble();
}

//Calculate the average.

double sum = 0;
for (int i = 0; i< numStudents; i++)
{
sum = sum+scores[i];
}
double average = sum/numStudents;

//Calculate the difference between the average score and the student's score.

double[]difference = new double[numStudents];
for (int i = 0; i<numStudents; i++)
{
difference[i] = scores[i]-average;
}

//Finding the highest, lowest score.
int highIndex = 0;
int lowIndex = 0;

for (int i = 1; i < numStudents; i++)
{
if (scores[i] > scores[highIndex])
{
highIndex = i;
}

else if (scores[i] < scores[lowIndex])
{
lowIndex = i;
}
}

//Finding the number of students scoring over average
int studentsAboveAverage = 0;

for (int i = 0; i < numStudents; i++)
{
if (scores[i] > average)
studentsAboveAverage++;
}

//Finding the character grade of the numeric score.
System.out.println("\t\t\tIndividual Student Statistics \n\n\t ID No.\t Score\t\tDifference\t\t Grade");
char[] grade = new char[numStudents];
for (int i = 0; i < numStudents; i++)
{
if(scores[i] >=90)
grade[i]='A';
else if(scores[i] >=80 && scores[i] <89.99)
grade[i]='B';
else if(scores[i] >=70 && scores[i] <79.99)
grade[i]='C';
else if(scores[i] >=60 && scores[i] <69.99)
grade[i]='D';
else // scores[index]<60
grade[i]='E';
System.out.printf("\t%7d \t%8.2f \t\t%8.2f \t\t%5c \t%n", ids[i], scores[i], difference[i], grade[i]);
}
System.out.printf("The average (mean) score is: %.2f%n " , average);
System.out.printf("The highest scoring student is: %7d with a score of %.2f.%n", ids, scores[highIndex]);
System.out.printf("The lowest scoring student is: %7d with a score of %.2f.%n" , ids, scores[lowIndex]);
System.out.printf("The number of students scoring over average is %d.%n" , (studentsAboveAverage));
}
}
/*Program Output
* Please enter the number of students in the class: 10
Enter 1 ID number and 1 score for each student: 123 82
Enter 1 ID number and 1 score for each student: 234 78
Enter 1 ID number and 1 score for each student: 105 91
Enter 1 ID number and 1 score for each student: 371 66
Enter 1 ID number and 1 score for each student: 401 52
Enter 1 ID number and 1 score for each student: 273 67
Enter 1 ID number and 1 score for each student: 581 96
Enter 1 ID number and 1 score for each student: 344 77
Enter 1 ID number and 1 score for each student: 503 81
Enter 1 ID number and 1 score for each student: 422 63
Individual Student Statistics

ID No. Score Difference Grade
123 82.00 6.70 B
234 78.00 2.70 C
105 91.00 15.70 A
371 66.00 -9.30 D
401 52.00 -23.30 E
273 67.00 -8.30 D
581 96.00 20.70 A
344 77.00 1.70 C
503 81.00 5.70 B
422 63.00 -12.30 D
The average (mean) score is: 75.30
The highest scoring student is: 6 with a score of 96.00.
The lowest scoring student is: 4 with a score of 52.00.
The number of students scoring over average is 6.

*/

For the highest, lowest scoring students I need to output their id and their score. I have tried a number of different ways, but am getting an error message.

Is This A Good Question/Topic? 0
  • +

Replies To: ARRAY PROGRAM - NEED HELP WITH OUTPUT FORMATTING

#2 Sally1217801999   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-October 08

Re: ARRAY PROGRAM - NEED HELP WITH OUTPUT FORMATTING

Posted 01 November 2008 - 05:49 PM

/**
* Programming Assignment # 2 studentScore.java
* I have designed this program to read (fictitious) student test scores
* in the range 0-100 and to have it print the following statistics to
* two decimal places:
* The average score; the number of students whose score is equal to or above
* the average; the student with the lowest score; and the student with the
* highest score.
*
* Emily Koopmann
* E00835771
* COSC 111 Section 5
* October 28, 2008
*/

import java.util.Scanner;// For keyboard input
public class studentScore

{
public static void main (String [] args)
{
Scanner in = new Scanner (System.in);
//User will be prompted for number of students.
System.out.print("Please enter the number of students in the class: ");
int numStudents = in.nextInt();

//Create 2-dimensional array

int[] ids = new int[numStudents];
double[] scores = new double[numStudents];

//Read IDs and scores

for(int i = 0; i <numStudents; i++)
{
System.out.print("Enter 1 ID number and 1 score for each student: ");
ids[i] = in.nextInt();
scores[i] = in.nextDouble();
}

//Calculate the average.

double sum = 0;
for (int i = 0; i< numStudents; i++)
{
sum = sum+scores[i];
}
double average = sum/numStudents;

//Calculate the difference between the average score and the student's score.

double[]difference = new double[numStudents];
for (int i = 0; i<numStudents; i++)
{
difference[i] = scores[i]-average;
}

//Finding the highest, lowest score.
int highIndex = 0;
int lowIndex = 0;

for (int i = 1; i < numStudents; i++)
{
if (scores[i] > scores[highIndex])
{
highIndex = i;
}

else if (scores[i] < scores[lowIndex])
{
lowIndex = i;
}
}

//Finding the number of students scoring over average
int studentsAboveAverage = 0;

for (int i = 0; i < numStudents; i++)
{
if (scores[i] > average)
studentsAboveAverage++;
}

//Finding the character grade of the numeric score.
System.out.println("\t\t\tIndividual Student Statistics \n\n\t ID No.\t Score\t\tDifference\t\t Grade");
char[] grade = new char[numStudents];
for (int i = 0; i < numStudents; i++)
{
if(scores[i] >=90)
grade[i]='A';
else if(scores[i] >=80 && scores[i] <89.99)
grade[i]='B';
else if(scores[i] >=70 && scores[i] <79.99)
grade[i]='C';
else if(scores[i] >=60 && scores[i] <69.99)
grade[i]='D';
else // scores[index]<60
grade[i]='E';
System.out.printf("\t%7d \t%8.2f \t\t%8.2f \t\t%5c \t%n", ids[i], scores[i], difference[i], grade[i]);
}
System.out.printf("The average (mean) score is: %.2f%n " , average);
System.out.printf("The highest scoring student is: %7d with a score of %.2f.%n", ids, scores[highIndex]);
System.out.printf("The lowest scoring student is: %7d with a score of %.2f.%n" , ids, scores[lowIndex]);
System.out.printf("The number of students scoring over average is %d.%n" , (studentsAboveAverage));
}
}
/*Program Output
* Please enter the number of students in the class: 10
Enter 1 ID number and 1 score for each student: 123 82
Enter 1 ID number and 1 score for each student: 234 78
Enter 1 ID number and 1 score for each student: 105 91
Enter 1 ID number and 1 score for each student: 371 66
Enter 1 ID number and 1 score for each student: 401 52
Enter 1 ID number and 1 score for each student: 273 67
Enter 1 ID number and 1 score for each student: 581 96
Enter 1 ID number and 1 score for each student: 344 77
Enter 1 ID number and 1 score for each student: 503 81
Enter 1 ID number and 1 score for each student: 422 63
Individual Student Statistics

ID No. Score Difference Grade
123 82.00 6.70 B
234 78.00 2.70 C
105 91.00 15.70 A
371 66.00 -9.30 D
401 52.00 -23.30 E
273 67.00 -8.30 D
581 96.00 20.70 A
344 77.00 1.70 C
503 81.00 5.70 B
422 63.00 -12.30 D
The average (mean) score is: 75.30
The highest scoring student is: 6 with a score of 96.00.
The lowest scoring student is: 4 with a score of 52.00.
The number of students scoring over average is 6.

*/

For the highest, lowest scoring students I need to output their id and their score. I have tried a number of different ways, but am getting an error message.
Was This Post Helpful? 0
  • +
  • -

#3 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: ARRAY PROGRAM - NEED HELP WITH OUTPUT FORMATTING

Posted 01 November 2008 - 06:19 PM

what's the error message you are receiving?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1