two parallel arrays

• 09-06-2010, 02:25 PM
two parallel arrays
Happy Labor Day!

This is a portion of the homework I am trying to do today. I just have a question on how to make this work.......

Specifically, construct a method that will take 2 parameters: an array of student names and an array of students grades where the arrays are parallel in the sense that a student's name and the student's grade are stored at the same index in the two arrays (at index i of the name array is the student's name and at index i of the grade array is that same student's grade.

Now, the question is.......... can I use a for loop UNDER a for loop to make the indexes equal? Here is what i have, I just want to know if this is on the right track....

Code:

```for ( int b = 0; b < s.length; b ++)  // outer loop for String array                 {         for ( int c = 0; c < g.length; c++) // using same loop for [] array at same time of String array         {         SameIndexg = g[c]; // stored the variable         SameIndexs = s[b]; // same thing```
• 09-06-2010, 02:48 PM
Fubarable
No need for two loops. Instead use a single loop and use the same index for each array. In fact nested for loops like you're doing will guarantee to mess you up.

Code:

```for ( int b = 0; b < s.length; b ++)  {         gItem = g[b];         sItem = s[b]; // same thing }```

Also, do yourself and us a favor and change your variable names so that they have meaning, so that they self-comment your code. i.e.,
Code:

```for ( int i = 0; i < studentNames.length; i ++)  {         String studentName = studentNames[i];         String studentGrade = studentGrades[i];  // or is this a numeric array such as int[] or double[]?         // do what you must with the name and grade.... }```
Also (2), realize that parallel arrays are something you should generally try to avoid as it's easy to make mistakes such as removing one item from an array and not doing the same for its counterpart in the other array. Of course you can't avoid using them right now since it is part of your assignment requirements, and what the teacher says, goes.

Luck!
• 09-06-2010, 10:28 PM
Ok. thanks. Do you mind looking at what I have come up with to see if it logically makes sense? The assignment again is in bold in the first post.

Code:

```public static void SortbyGrade( String[] s, int [] g)         {                                 if (s == null)                         throw new IllegalArgumentException ("null array");                 if ( g == null)                         throw new IllegalArgumentException ("null array");                                 if ( s.length != g.length)                         throw new IllegalArgumentException ("must be same size");                                                                 int SameIndexg;                 String SameIndexs;                 int CurrentMax;                 double CurrentMaxIndex;                                                 for ( int i = 0; i < s.length; i ++)  // setting index in loop                 {                                             SameIndexg = g[i]; // stored the variable                     SameIndexs = s[i]; // same thing with same index                                                 }       // trying to switch elements of array in descending order below                 for ( int j = 0; j < g.length;  j++)                 {                         if ( j > 1)                         {                                 CurrentMax = g[j];                                 CurrentMaxIndex = j;                         }                 }```
• 09-06-2010, 10:58 PM
Norm
Code:

```      // trying to switch elements of array in descending order below                 for ( int j = 0; j < g.length;  j++)                 {                         if ( j > 1)    // For every j except the first two???                         {                                 CurrentMax = g[j];    // save value of current element                                 CurrentMaxIndex = j; // save index to current element                         }                 }```
Where in the loop do you compare elements and then switch them?

Code:

```                for ( int i = 0; i < s.length; i ++)  // setting index in loop                 {                                             SameIndexg = g[i]; // stored the variable                     SameIndexs = s[i]; // same thing with same index                                                 }```
This loop does no useful work. The values of the variables are continually overstored each loop and have the last values when the loop exits.
• 09-06-2010, 11:22 PM
for ( int i = 0; i < s.length; i ++) // setting index in loop
{

SameIndexg = g[i]; // stored the variable
SameIndexs = s[i]; // same thing with same index

}
Quote:

This loop does no useful work. The values of the variables are continually overstored each loop and have the last values when the loop exits.
I see. Thanks Norm for pointing that out. How do I store the same index for each array? It has to be the same index, right?
• 09-06-2010, 11:26 PM
Norm
Quote:

How do I store the same index for each array
Can you explain what you are trying to do in the loop?
If the arrays are in parallel then you only need one index value. That would be the meaning of in parallel.
• 09-06-2010, 11:46 PM
Specifically, construct a method that will take 2 parameters: an array of student names and an array of students grades where the arrays are parallel in the sense that a student's name and the student's grade are stored at the same index in the two arrays (at index i of the name array is the student's name and at index i of the grade array is that same student's grade.

I am trying to find a way to store the same index in two arrays. I tried it above (in the example you commented on) and I did it wrong. That is why I am trying to ask for some help/ hints on how can I store the same index in two arrays. The parallel portion will come later, as evidenced in my code above, but I am not there yet......... I hope that sheds some more light on this.
• 09-06-2010, 11:50 PM
Could this be part of the solution?

for ( int i = 0; i < s.length && i < g.length; i ++) // setting index in loop
• 09-07-2010, 12:14 AM
Norm
Quote:

i < s.length && i < g.length;
if the arrays are in parallel then by definition, their lengths are the same. No need to test against both.

Quote:

how can I store the same index in two arrays.
I don't understand what you are asking? An index is used to find an element in an array.
What do the two arrays you are working with contain? One has the name and one the grade. So name[7] is the name and grade[7] is the grade for student at index 7.
• 09-07-2010, 12:36 AM
This might clarify it a little bit. The two arrays are parallel, but then I am going to switch the grade array into DESCENDING order, rather than leave it as an array of random grades.
The teacher wants the same "relationship" maintained. Here is the assignment wording:

Write a method that will sort student names based on their test grades. Specifically, construct a method that will take 2 parameters: an array of student names and an array of students grades where the arrays are parallel in the sense that a student's name and the student's grade are stored at the same index in the two arrays (at index i of the name array is the student's name and at index i of the grade array is that same student's grade). The method will sort the 2 arrays so that the "parallel relationship" is maintained (student's name and student's grade are stored at the same index in the 2 arrays) and the array containing the student's grades is in descending order.
• 09-07-2010, 01:03 AM
Fubarable
In whatever sort you do, whenever you swap two grades, just make sure to swap the corresponding name Strings. In other words, if you swap grades[3] with grades[8], make sure to swap names[3] with names[8]. Do you have a swap method or something equivalent by the way? Also what type of sorting will you be doing?
• 09-07-2010, 01:39 AM
I would love to use the swap method in the Java library, yet once again, I am not allowed to use it. I have to create my own (sigh........)

I was thinking of using the selection sort. Here is that part of the code below:

I am trying.......

Code:

```for ( int j = 0; j < g.length;  j++)  // using selection sort to descend grades         {                 if ( j > 0)                 {                 CurrentMax = g[j];                 CurrentMaxIndex = j;                 }                                         if (CurrentMaxIndex != j) // doing switch in descending order                 {                         g[CurrentMaxIndex] = g[j];                         g[j] = CurrentMax;                 }                 }```
• 09-07-2010, 01:45 AM
Norm
In sort logic you need to compare the contents of two array elements. I don't see where you do that.

A comment on variable names in Java.