Student Record List With Parallel Arrays

From CompSciWiki
Revision as of 17:05, 7 December 2011 by RalviV (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Given two parallel arrays, where the first array stores the a list of student's names and the second array stores the list of grades, print out the name of the student and their corresponding grade. Find the student with the highest grade and the student with the lowest grade and print the results. Use a honoursRoll[] boolean array to store true if the corresponding student is eligible for honours (>=80) and print the results.

 

Problem Solving

Wiki bench01.jpg

Solution

In this example, you are given two parallel arrays with 10 elements in each array,

 String [] students = {"Joey Chase","Alan Gibbs","Ellen Kauffman","Thomas Lembeck",
"Alexa Bright","Seth Benson","Terry Logan","Keven Gail","Shelley Fryman","Jill Beckam"};

int [] grades = {90,63,77,85,91,54,82,55,53,89}; 

The first array stores a list of student names and the second array stores a list of the corresponding student's grades.


The first task is to print the current list of students and their corresponding grades. It is good programming ediquette to seperate sections of your code into methods so that your program is organized in a legible fashion. Create a new method (ex: printList()) and pass the two array as parameters (String [] students and int [] grades). Keep in mind that we are simply printing the arrays so there is no return value. You should have something that looks like this,

 public static void printList(String [] students, int [] grades) {

	:

} 

Start writing your print list method with a for loop and use the array's length function (ex: student.length) to get the size of an array. Arrays can be fixed sizes but in many cases the size may not be given, the length function is a useful tool that will cover a broader range of unknown possibilities.


As a reminder, arrays start with the index zero, so when you are aligning the index position and to the count value, the index position is one less than the count value (ex: First element in the array has an index position 0 and the size value is 1. Last element in the array has an index position of n - 1 and the size value is n). Thus, set the expression to loop while i < size of the array.

 for(int i = 0; i < students.length; i++) {

	:

} 


Take note that with parallel arrays, it doesn't matter which array you use to get the length function, they are synchronized and have the same size. Add your print statement to print the elements in both arrays inside the loop and your print method should look something like this,

 public static void printList(String [] students, int [] grades) {

	System.out.println("====Student List====");
	System.out.println("");
	for(int i = 0; i < students.length; i++) {
		System.out.println(students[i] + "\t" + grades[i]);
	}
	System.out.println("");
	System.out.println("");

} 


The next task is to print the student with the highest mark and the student with the lowest mark. In general, you are looking for the minimum and maximum values in a list. Use variables to store the position of the min value and the position of the max value. Loop through to compare the value of each element in the array with the values of the positions min and max. When a new smaller value is discovered, we replace the index position of the old min value with the new min value. The same thing applies for max, but we replace the max value when a new greater value is discovered.


Start off by creating a new method that is similar to the print method. Declare two int variables and initialize them to zero.

 int min = 0;
int max = 0; 

We initialize it to zero so we have a default starting position to start the comparison.

Next, create a for loop (similar to the previous for loop), where the loop will be used to go through each element in the array. During each iteration, we will compare the value of the iterated index position with the index postion stored in the min and max variable. In this example, the values that we will focus our comparisons on are located in the grades[] array. If a new smaller value is found, replace the index position of min with the new index position.

Just how it sounds, your if statement should look like this,

 if(grades[i] < grades[min]) {
	min = i;
} 


Similarily, do the same thing for finding the max value, but the expression will replace the position of the old max value if a new greater value is found.

 if(grades[i] > grades[max]) {
	max = i;
} 


When the loop is done, the variables min and max will hold the index positions of the smallest and largest values in the list. At the end of the method, add your print statements to display the results of the student with the highest grade and the lowest grade. Your method should look something like this,

 public static void findMinMax(String [] students, int [] grades) {
	int min = 0;
	int max = 0;

	for(int i = 0; i < grades.length; i++) {
		if(grades[i] > grades[max]) {
			max = i;
		}
		if(grades[i] < grades[min]) {
			min = i;
		}
	}
	System.out.println("====Student with Highest Grade====");
	System.out.println("");
	System.out.println(students[max] + "\t" + grades[max]);
	System.out.println("");
	System.out.println("");

	System.out.println("====Student with Lowest Grade====");
	System.out.println("");
	System.out.println(students[min] + "\t" + grades[min]);
	System.out.println("");
	System.out.println("");

} 

Keep in mind that when your program exits this method, your min and max variables will disappear along with the method (unless if you return the values or store them somewhere else).


The next task is to create a boolean array that stores true if the student is eligible for honours roll and false if they do not. The boolean array needs to have the same number of elements as the other arrays (students[] and/or grades[]). Check each student's grade, if their mark is 80 or above then they are eligible for honours roll.

In this example we want to store the boolean results for future use, so when you create the new method there will be a return value of a boolean[] array.

 public static boolean [] eligibleForHonours(String [] students, int [] grades) {

	:

} 


Remember that whenever this method is called an element will be returned. So wherever you are calling this method from, you need to have a variable of the same type as the return type variable to store it.

In this example, we will be calling this method from the main method, so you will need a variable in the main method that will store a boolean array type variable.

 boolean [] honourRoll;

	:

honourRoll = eligibleForHonours(students,grades); 


Note that in this case, it is not necessary to initialize honourRoll[] because a boolean array will be created and initialized in the eligibleForHonours method and will be passed to the honourRoll[] array in the main method.


Next we will declare a boolean array in the eligibleForHonours method. Initalize the size of the array to be the same size as the students[] and grades[] arrays by using the array .length function.

 boolean [] honoursRoll = new boolean[grades.length]; 


Since we want to check if the grade of each student, create a for loop to go through each element in the grades[] array. Examine each grade, if the grade is >= 80 then we will store true in the corresponding index position of the boolean array. If the grade is < 80, then we will store false. Just how it sounds, your loop should look like this,

 for(int i = 0; i < grades.length; i++) {
	if(grades[i] >= 80) {
		honoursRoll[i] = true;
	}
	else {
		honoursRoll[i] = false;
	}
} 


Now that our task is complete, write your return statement to return the boolean array to the main method. Your method should look something like this,

 public static boolean [] eligibleForHonours(String [] students, int [] grades) {
	boolean [] honoursRoll = new boolean[grades.length];

	for(int i = 0; i < grades.length; i++) {
		if(grades[i] >= 80) {
			honoursRoll[i] = true;
		}
		else {
			honoursRoll[i] = false;
		}
	}

	return honoursRoll;
} 


Now that our boolean array is filled with data, lets print it out. Our last task is to print a list of only students who are eligible for honours.

Create a new method and pass in the appropriate parameters. In this case, we will only need students[] and honoursRoll[] arrays.

 public static void printHonours(String [] students, boolean [] honoursRoll) {

	:

} 


Next, we want to go through the array and check the values stored in the honoursRoll[] array. Create your for loop to go through each element in the array. If the value stored is true, then we want to print the name of the student. If the value is not true (in other words, false), we just ignore it. Just like how it sounds, your loop should look like this,

 for(int i = 0; i < honoursRoll.length; i++) {
	if(honoursRoll[i]) {
		System.out.println(students[i]);
	}
} 


Now that our task is complete, your method should look something like this,

 public static void printHonours(String [] students, boolean [] honoursRoll) {

	System.out.println("====Honours Roll List====");
	System.out.println("");
	for(int i = 0; i < honoursRoll.length; i++) {
		if(honoursRoll[i]) {
			System.out.println(students[i]);
		}
	}
	System.out.println("");
	System.out.println("");

} 


Now that your methods are complete, make you have your method calls from the main method and pass the appropriate parameters.

 printList(students,grades);
findMinMax(students,grades);
honourRoll = eligibleForHonours(students,grades);
printHonours(students,honourRoll); 


Now you're done! In this tutorial, I have gone through a different ways in using parallel arrays, finding the min and max value, and using a 3rd parallel array to use as a filter when searching for specific data.

Code

Solution Code

Back to the Program-A-Day homepage