In: Computer Science
pseudocode please!
Assignment 5B: Moneyball: Part 2. Similar to the previous assignment, you’re going to read in the number of years the player played and the starting year of that player – followed by the statistics for those years. This time, however, you’re going to print out the years from worst to best in sorted order. Hint: this will require a second array to store years. If you can sort one array, can you sort both?
Sample Output #1: Enter the number of years: 5 Enter the starting year: 2003 Enter stat for year 2003: 5 Enter stat for year 2004: 4 Enter stat for year 2005: 7 Enter stat for year 2006: 1 Enter stat for year 2007: 3 2006|2007|2004|2003|2005| Sample Output #2: Enter the number of years: 6 Enter the starting year: 1879 Enter stat for year 1879: 70 Enter stat for year 1880: 89 Enter stat for year 1881: 111 Enter stat for year 1882: 65 Enter stat for year 1883: 105 Enter stat for year 1884: 98 1882|1879|1880|1884|1883|1881|
Pseudocode and sample source code:
Output:
Code in text format (See above images of code for indentation):
/****************PseudoCode************************ */
/*
import libraries
class definition begin
{
Main
{
Scanner object initalize to read data from input
initialize or declare a variables start,n etc
prompt the user to enter number of years
read n
prompt the user to enter start year
read start
declare arrays for stats[n] and years[n]
for(i=0 to n-1)do
{
read stat for year
add data to stat array
add start to years array
increment start++
}end for
for(i=0 to n-1)do
{
for(j=i+1 to n-1)do
{
compare
if(stat[i]>stat[j])
{
swap stat[i] and stat[j]
swap years[i] and years[j]
}
}end inner for
}end outer for
for(i=0 to n-1)do
print years[i]+"|"
}end main
}end class
*****************************************************/
import java.util.*;
/*class definition*/
public class Assignment5B
{
/*main method*/
public static void main(String[] args)
{
/*Scanner object to read data from the user*/
Scanner scnr=new Scanner(System.in);
int n,start;
/*read number of years from the user*/
System.out.print("Enter the number of years: ");
n=scnr.nextInt();
/*read starting from the user*/
System.out.print("Enter starting year: ");
start=scnr.nextInt();
/*arrays declarations*/
int[] stat=new int[n];
int[] years=new int[n];
for(int i=0;i<n;i++)
{
/*enter stats for year by year*/
System.out.print("Enter stat for year "+start+": ");
stat[i]=scnr.nextInt();
/*add year to years array*/
years[i]=start;
start++;
}
/*using nested loops sort the arrays*/
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
/*compare stats*/
if(stat[i]>stat[j])
{
/*swap years*/
int temp=years[i];
years[i]=years[j];
years[j]=temp;
/*swap stats for correct comparsion*/
temp=stat[i];
stat[i]=stat[j];
stat[j]=temp;
}
}
}
/*print years*/
for(int i=0;i<n;i++)
System.out.print(years[i]+"|");
}
}