Difference between revisions of "Student Record List With Parallel Arrays"

From CompSciWiki
Jump to: navigation, search
m
Line 34: Line 34:
 
<pre>
 
<pre>
 
for(int i = 0; i < students.length; i++) {
 
for(int i = 0; i < students.length; i++) {
...
+
:
 +
:
 
}
 
}
 
</pre>
 
</pre>
Line 113: Line 114:
 
</pre>
 
</pre>
 
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).
 
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.
 +
 +
<pre>
 +
public static boolean [] eligibleForHonours(String [] students, int [] grades) {
 +
:
 +
:
 +
}
 +
</pre>
  
  

Revision as of 12:08, 7 April 2010

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.

 

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 are 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) {
	:
	:
}


Dont forget to make your method calls from the main method and pass the appropriate parameters.

Code

Solution Code

Back to the Program-A-Day homepage