Results 1 to 13 of 13
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default 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....

    Java 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

    Java 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.,
    Java 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!
    Last edited by Fubarable; 09-06-2010 at 03:01 PM.

  3. #3
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    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.

    Java 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;
    			}
    		}

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    Java 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?

    Java 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.
    Last edited by Norm; 09-06-2010 at 11:00 PM.

  5. #5
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    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.
    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?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    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.

  7. #7
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    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.

  8. #8
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Could this be part of the solution?

    for ( int i = 0; i < s.length && i < g.length; i ++) // setting index in loop

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    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.

    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.

  10. #10
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    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.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  12. #12
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    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.......

    Java 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;
    		}
    		}

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default

    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.
    Standard is for variables to start with lowercase.
    Class names and final variables start with uppercase.

    Another comment on coding style, use self documenting variable names. 'g' or 's' doesn't say what it is or contains.

Similar Threads

  1. Sorting Multiple Parallel Arrays
    By Pyrexkidd in forum New To Java
    Replies: 7
    Last Post: 05-12-2010, 06:34 AM
  2. How to create parallel arrays
    By Roselicious in forum New To Java
    Replies: 6
    Last Post: 04-18-2010, 12:10 PM
  3. I need examples using parallel arrays
    By dangerzone9k in forum New To Java
    Replies: 10
    Last Post: 04-04-2009, 04:11 PM
  4. [SOLVED] Parallel Arrays with Choice ComboBox - need assistance
    By Judoon_Platoon in forum Java Applets
    Replies: 14
    Last Post: 10-01-2008, 09:07 PM
  5. parallel taglibs
    By zohar in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-04-2007, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •