5 Replies - 1767 Views - Last Post: 06 September 2010 - 05:20 PM Rate Topic: -----

#1 Guest_nooby*


Reputation:

Array Parallel Relationship

Posted 06 September 2010 - 02:16 PM

I'm writing an array that takes student grades(array g)and puts them in descending order but also matches the grades with the other array(array s). So what i'm asking is how to sort array g but also have array s have the name array stay with the g array. Below is the requirements for the array and my program so far. Any help would be awesome.


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. Use the file TestSortStudentGrades.java. The specifications of this method are:
*

input: array s of student names and an array g of student grades
*

preconditions: both s and g are not null and they have the same length
*

postconditions: g is sorted in descending order and s has been modified so that the same "parallel" relationship exists between s and g.

public class sortStudentGrades 
{
	/**
	 * s or g cannot null
	 * @param s array of student names
	 * @param g array of student grades
	 * @throw IllegalArgumentException if s or g is null
	 * @throw IllegalArgumentException if s length != g length
	 */
	public static void sort(String[] s, int[] g)
	{
		if(s==null || g==null)
			throw new IllegalArgumentException("array is null");
		if(s.length != g.length)
			throw new IllegalArgumentException("arrays are different sizes");
		
		int i, count, temp;
		for(count=0; count<length-1; count++)
		{
			for(i=0; i<length-1-count; i++)
			{
				if(g[i]<g[i+1])
				{
				temp=g[i];
				g[i]=g[i+1];
				g[i+1]=temp;
				}
			}
		}
	}
}



Edited by macosxnerd101: Please, :code:.

Is This A Good Question/Topic? 0

Replies To: Array Parallel Relationship

#2 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1998
  • View blog
  • Posts: 4,150
  • Joined: 11-December 07

Re: Array Parallel Relationship

Posted 06 September 2010 - 03:04 PM

Basically whatever you do to one array, you have to do to the other. Swap Two items in the first array, swap them both in the second array.So when you

temp=g[i];
g[i]=g[i+1];
g[i+1]=temp;


You must also

String temp2=s[i];
s[i]=s[i+1];
s[i+1]=temp2;

Was This Post Helpful? 0
  • +
  • -

#3 Guest_nooby*


Reputation:

Re: Array Parallel Relationship

Posted 06 September 2010 - 03:31 PM

View Postcfoley, on 06 September 2010 - 02:04 PM, said:

Basically whatever you do to one array, you have to do to the other. Swap Two items in the first array, swap them both in the second array.So when you

temp=g[i];
g[i]=g[i+1];
g[i+1]=temp;


You must also

String temp2=s[i];
s[i]=s[i+1];
s[i+1]=temp2;

thanks. i feel dumb now that i just didnt consider that haha
Was This Post Helpful? 0

#4 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1998
  • View blog
  • Posts: 4,150
  • Joined: 11-December 07

Re: Array Parallel Relationship

Posted 06 September 2010 - 04:10 PM

No problem. There are lots of things in programming that seem obvious after someone has shown you them.

By the way, I know you have been given this as an assignment but parallel arrays are often a source of bugs. If you created a class with the name and score you could store everything in a single array.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_nooby*


Reputation:

Re: Array Parallel Relationship

Posted 06 September 2010 - 04:21 PM

View Postcfoley, on 06 September 2010 - 03:10 PM, said:

No problem. There are lots of things in programming that seem obvious after someone has shown you them.

By the way, I know you have been given this as an assignment but parallel arrays are often a source of bugs. If you created a class with the name and score you could store everything in a single array.

thanks for the advice.
Was This Post Helpful? 0

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,110
  • Joined: 27-December 08

Re: Array Parallel Relationship

Posted 06 September 2010 - 05:20 PM

If you are interested in reading more up on the classes and Objects route, I wrote a tutorial Moving Away From Parallel Arrays, which you may find helpful. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1