Please use pseudocode for this response, any additional advice for making this a
ID: 671827 • Letter: P
Question
Please use pseudocode for this response, any additional advice for making this as easy to understand as possible is greatly appreciated.
Problem Statement
Assume the Scores array is parallel to the Players array (both arrays are below).
Scores array
Scores[0] = 198
Scores[1] = 486
Scores[2] = 651
Scores[3] = 185
Scores[4] = 216
Scores[5] = 912
Scores[6] = 173
Scores[7] = 319
Scores[8] = 846
Scores[9] = 989
Players Array
Players[0] = "Joe"
Players[1] = "Ann"
Players[2] = "Marty"
Players[3] = "Tim"
Players[4] = "Rosy"
Players[5] = "Jane"
Players[6] = "Bob"
Players[7] = "Lily"
Players[8] = "Granny"
Players[9] = "Liz"
Write a looping program that presents the user with 3 options:
1) Sort Output by Players
2) Sort Output by Scores
3) Exit Program
When the first option is selected, sort the Players array in alphabetical order, keeping the Scores array parallel. Add code that determines the highest and lowest scores in the list. Include code to display each player’s score and name in the sorted order. Below the sorted list display the highest and lowest scores in the list and the name of the player who received that score.
Your sort by Player output display should look like this:
Scores Sorted by Player:
486 Ann
173 Bob
846 Granny
912 Jane
198 Joe
319 Lily
989 Liz
651 Marty
216 Rosy
185 Tim
-----------------------------------
989 Highest Score by Liz
173 Lowest Score by Bob
When the second option is selected, sort the Scores array in numerical order, keeping the Players array parallel. Add code that determines the average score of the entire list. Include code to display each player’s score and name in the sorted order. Below the sorted list display the average of all scores in the list. Your sort by Scores output display should look like this:
Players Sorted by Scores:
173 Bob
185 Tim
198 Joe
216 Rosy
319 Lily
486 Ann
651 Marty
846 Granny
912 Jane
989 Liz
---------------------------
498 Average Score
You may use either the Bubble Sort or the Selection Sort algorithms.
Option three is self explanatory. NEVER call "main" from inside your program. Use a loop that keeps your program running until the user chooses option 3.
Round the Average score to the nearest whole number, as shown in the output example above.
You MUST use Modular Programming techniques by using Sub Modules (Sub Charts in RAPTOR) in your program. Your "main" module should not be very large. Again, NEVER call "main" from inside your program. Also, do not use "recursion" in this program (submodules that call themselves). You are only allowed to use looping techniques to repeat sections of your submodules.
You may NOT "hard code" the numbers for highest and lowest scores. Nor simply sort the array by score and use the lowest and highest indexes. These must be discovered through algorithm that will work on an unsorted array. NOR may you "hard code" the number for the average score. Accumulate the scores in a loop then calculate the average. If the array data is changed, the Hi/Low/Avg scores should automatically be found or calculated with the new data.
Hard-code the values of the arrays into your program. Do NOT ask the user to input the values.
Explanation / Answer
SortExample.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.student;
import java.util.Scanner;// package
/**
*
* @author Kannarao
*/
public class SortExample {
public static void main(String args[]) {
int c, d, swap;//c for looping player names//d for looping scores
// declare a string scores with initial size
String[] players = new String[10];//players array
int scores[] = new int[10];//scores array
// asigning player names to players[] array
players[0] = "JOE";
players[1] = "ANN";
players[2] = "MARTY";
players[3] = "TIM";
players[4] = "ROSEY";
players[5] = "JAIN";
players[6] = "BOB";
players[7] = "LILY";
players[8] = "GRANNY";
players[9] = "LIZ";
// asigning scores to scores array
scores[0] = 198;
scores[1] = 486;
scores[2] = 651;
scores[3] = 185;
scores[4] = 216;
scores[5] = 198;
scores[6] = 912;
scores[7] = 173;
scores[8] = 319;
scores[9] = 989;
Scanner sc = new Scanner(System.in);//keyboard inputting
System.out.println("Enter your choice");
System.out.println("press 1 for ascending order of scores with players names");
System.out.println("press 2 for alphabitical order of players names with scores");
System.out.println("press 3 for exit");
System.out.println();
int choice = sc.nextInt();
switch (choice) {
case 1:
for (c = 0; c < (10 - 1); c++) {
for (d = 0; d < 10 - c - 1; d++) {
if (scores[d] > scores[d + 1]) /* For descending order use < */ {
swap = scores[d];
scores[d] = scores[d + 1];
scores[d + 1] = swap;
}
}
}
System.out.println("Sorted list of players through scores");
players[0] = "JOE";
players[1] = "ANN";
players[2] = "MARTY";
players[3] = "TIM";
players[4] = "ROSEY";
players[5] = "JAIN";
players[6] = "BOB";
players[7] = "LILY";
players[8] = "GRANNY";
players[9] = "LIZ";
for (c = 0; c < 10; c++) {
System.out.println(players[c] + " scored " + scores[c]);
int least = scores[0];
int highest = scores[0];
for (int i = 1; i < scores.length; i++) {
if (scores[i] > highest) {
highest = scores[i];
} else if (scores[i] < least) {
least = scores[i];
}
}
if (scores[c] == highest) {
System.out.println(players[c] + " scored highest runs : " + highest);
}
if (scores[c] == least) {
System.out.println(players[c] + " scored least runs : " + least);
}
}
break;
case 2:
sortStringBubble(players);
scores[0] = 198;
scores[1] = 486;
scores[2] = 651;
scores[3] = 185;
scores[4] = 216;
scores[5] = 198;
scores[6] = 912;
scores[7] = 173;
scores[8] = 319;
scores[9] = 989;
System.out.println("sorting by players in alphabitical order");
for (c = 0; c < 9; c++) {
System.out.println(players[c] + " scored " + scores[c]);
}
break;
case 3:
System.out.println("you have quitted");
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
break;
}
}
public static void sortStringBubble(String x[]) {
int j;
boolean flag = true; // will determine when the sort is finished
String temp;
while (flag) {
flag = false;
for (j = 0; j < x.length - 1; j++) {
if (x[j].compareToIgnoreCase(x[j + 1]) > 0) { // ascending sort
temp = x[j];
x[j] = x[j + 1]; // swapping
x[j + 1] = temp;
flag = true;
}
}
}
}
}
output
run:
Enter your choice
press 1 for ascending order of scores with players names
press 2 for alphabitical order of players names with scores
press 3 for exit
1
Sorted list of players through scores
JOE scored 173
JOE scored least runs : 173
ANN scored 185
MARTY scored 198
TIM scored 198
ROSEY scored 216
JAIN scored 319
BOB scored 486
LILY scored 651
GRANNY scored 912
LIZ scored 989
LIZ scored highest runs : 989
run:
Enter your choice
press 1 for ascending order of scores with players names
press 2 for alphabitical order of players names with scores
press 3 for exit
2
sorting by players in alphabitical order
ANN scored 198
BOB scored 486
GRANNY scored 651
JAIN scored 185
JOE scored 216
LILY scored 198
LIZ scored 912
MARTY scored 173
ROSEY scored 319
run:
Enter your choice
press 1 for ascending order of scores with players names
press 2 for alphabitical order of players names with scores
press 3 for exit
3
you have quitted
run:
Enter your choice
press 1 for ascending order of scores with players names
press 2 for alphabitical order of players names with scores
press 3 for exit
5
Invalid choice.
BUILD SUCCESSFUL (total time: 3 seconds)