Jagged Array problem

building a custom jagged array for an assignment

Page 1 of 1

2 Replies - 2676 Views - Last Post: 13 November 2008 - 09:11 PM Rate Topic: -----

#1 Onker  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 15-July 08

Jagged Array problem

Post icon  Posted 13 November 2008 - 06:06 PM

Howdy again folks, managed to get through to this week without bothering anyone but now I've got an issue.

Mainly it's in the mechanics of doing this, and if someone could post a well notated version I'd probably be ok.

I'm attempting to create a "Jagged" array of student objects.

I can create the array, set the size of the first section, and I believe my method for setting the size of the array within the array is workable too.

My problem is adding the information that I want to add to that array. Here's my code.

class Students
	{
		public struct Student
		{// Structure to detail the student information
			public string firstName;
			public string lastName;
			public double GPA;

			public Student(string firstName, string lastName, double GPA)
			{
				this.firstName = firstName;
				this.lastName = lastName;
				this.GPA = grade;
			}
		}
	}
class CreateArrays
	{
		public static void Creation()
		{
			string firstName = "";
			string lastName = "";
			double GPA;
			Console.Write("How many groups of Students would you like to input: ");
			int quantity = Int32.Parse(Console.ReadLine());

			Students[][] JagStudent = new Students[quantity][];

			for (int i = 0; i < JagStudent.Length; i++)
			{
				Console.Write("How many students would you like to input for group {0}: ", i);
				int numStudents = Int32.Parse(Console.ReadLine());

				JagStudent[i] = new Students[numStudents];

				for(int j = 0; j < numStudents; j++)
				{
					Console.Write("Group {0} student {1} first name: ", i, j);
					firstName = Console.ReadLine();
					//how to fill it in?
					Console.Write("Group {0} student {1} last name: ", i, j);
					lastName = Console.ReadLine();

					Console.Write("Group {0} student {1} GPA: ", i, j);
					GPA = double.Parse(Console.ReadLine());

				}
			}
		}
	}




If I can get everything added to the array, I then need to figure out how to pull information out, such as the GPA's so that I can determine the median and average GPA per group.

Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Jagged Array problem

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Jagged Array problem

Posted 13 November 2008 - 07:23 PM

Jagged arrays are usually a bad idea. In fact, unless your list size is fixed, an array is probably a bad idea.

The class Students { public struct Student this is confusing. Why not just have a class Student?

Why static method? This useful how?

Here's kind of what I think you're aiming for.

// why use struct?  Don't.  C# is object oriented.  Unless you're a incurable C programmer, there's no excuse
public class Student {
	// make the value private
	// don't let other parts of your program mess with this unless you want it to
	private string firstName;
	private string lastName;
	private double gpa;
	
	// I don't know why you need a "group", 
	// but we'll use this object to store it
	private int group;

	// this all looks fine.
	public Student(int group, string firstName, string lastName, double gpa) {
		this.group = group;
		this.firstName = firstName;
		this.lastName = lastName;
		this.gpa = gpa;
	}
	
	// expose your values
	public string FirstName { get { return firstName; } }
	public string LastName { get { return lastName; } }
	public double GPA { get { return gpa; } }
	public int Group { get { return group; } }
	
}

// make a Student collect from a list
// add some functionality here
public class Students : List<Student> {
	private int groupCount;
	
	// this method was "Creation()"
	public void UserEntry() {
		Console.Write("How many groups of Students would you like to input: ");
		this.groupCount = Int32.Parse(Console.ReadLine());

		// normaly i and j are fine for loops
		// here, it helps to be clearer
		for (int groupNum = 0; groupNum < groupCount; groupNum++) {
			Console.Write("How many students would you like to input for group {0}: ", i);
			int numStudents = Int32.Parse(Console.ReadLine());
			
			for (int studentNum = 0; studentNum < numStudents; studentNum++) {
				string label = string.Format("Group {0} student {1}", groupNum, studentNum);
				Console.Write(label + " first name: ");
				string firstName = Console.ReadLine();
				
				Console.Write(label + " last name: ");
				string lastName = Console.ReadLine();

				Console.Write(label + " GPA: ");
				double gpa = double.Parse(Console.ReadLine());
				
				// the work you're looking for
				this.Add( new Student(groupNum, firstName, lastName, gpa) );
			}
		}
	}
	
	// expose the group count
	public int GroupCount { get { return groupCount; } }
	
	// want students for groups?
	public Students GetStudentsForGroup(int groupNum) {
		Students outValues = new Students();
		foreach(Student item in this) {
			if (item.Group==groupNum) {
				outValues.Add(item);
			}
		}
		return outValues;
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 Onker  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 51
  • Joined: 15-July 08

Re: Jagged Array problem

Posted 13 November 2008 - 09:11 PM

baavgai,

I'm not sure why the insistence was placed on using a jagged array. It seems overly clumsy for what he's trying to do.

the static was out of a bad habit that I need to break, just "plopping" it down there isn't really a good idea I know. I'll think twice about it now, as well as making sure my private variables remain private.

As to the struct...I did something similar for him before and to be honest I was reusing some old, and probably not so good, code to do it.

Thank you by the way, the notes in your code were very helpful. I've still got to work through this with the whole "jagged/ragged" thing but I appreciate the steps you've taken to point me in the right direction.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1