Adding to a sparse table

I'm a little stumped.

Page 1 of 1

4 Replies - 1734 Views - Last Post: 26 March 2009 - 01:41 PM Rate Topic: -----

#1 nunley0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-March 09

Adding to a sparse table

Post icon  Posted 25 March 2009 - 02:29 PM

All I'm looking for are some pointers in the right direction. I've been looking at this and three other assignments for entirely too long, and my brain has gone to mush lol. Any help will be greatly appreciated. PLEASE DO NOT GIVE ME CODE OR A COMPLETE OUTLINE! This is for an assignment, as I mentioned earlier, and I really want to feel like I've learned the material when it's complete.

Without further ado, my code:

SparseTable.h

class SparseTable
{
	public:
		SparseTable
				(int nStudents, int nCourses);
		bool add 
				(int nStudent, int nCourses);
	private:
		int numberOfStudents;
		int numberOfCourses;
		SparseTableNode** students;
		SparseTableNode** courses;
};



SparseTable.cpp

#include "master.h"

//
// Constructor
//

SparseTable::SparseTable (int nStudents, int nCourses)
//
// Description: Constructor
//
// input: nStudents (number of students in table), nCourses (number of courses in table)
//
// output: none
//
{
	numberOfStudents = nStudents;
	numberOfCourses = nCourses;

	students = new SparseTableNode* [numberOfStudents];
	courses = new SparseTableNode* [numberOfCourses];

	// initalize students and courses to NULL
	for (int i = 0; i < numberOfStudents; i++)
		students[i] = NULL;
	
	for (int i = 0; i < numberOfCourses; i++)
		courses[i] = NULL;

	return;
}

// todo: method add()



Is This A Good Question/Topic? 0
  • +

Replies To: Adding to a sparse table

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Adding to a sparse table

Posted 25 March 2009 - 06:18 PM

You haven't told us what you need help with. In add, I assume you want to dynamically allocate a pointer to a SparseTableNode, then assign the pointer to a location in the previously-allocated SparseTable array.
Was This Post Helpful? 0
  • +
  • -

#3 nunley0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-March 09

Re: Adding to a sparse table

Posted 25 March 2009 - 11:23 PM

View PostJackOfAllTrades, on 25 Mar, 2009 - 05:18 PM, said:

You haven't told us what you need help with. In add, I assume you want to dynamically allocate a pointer to a SparseTableNode, then assign the pointer to a location in the previously-allocated SparseTable array.


I guess what I'm stumped on is how do I make sure that the addition of the new node doesn't cause a memory fault by stepping out of the arrays students and courses? My solution can't use counter values for the number of unique course numbers and student id's, and I'd like to avoid traversing the arrays and doing a simulated bounds check every time I add a node with a new id or course number; that would be horribly wasteful. Is there a clean efficient solution to this problem that meets this criteria?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Adding to a sparse table

Posted 26 March 2009 - 04:26 AM

Unless I'm misunderstanding, you have the initial bounds of the array stored in numberOfStudents and numberOfCourses, so you check your student ID and input against those first. If it's in range, then you directly access the array at the ID provided.
Was This Post Helpful? 0
  • +
  • -

#5 nunley0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-March 09

Re: Adding to a sparse table

Posted 26 March 2009 - 01:41 PM

View PostJackOfAllTrades, on 26 Mar, 2009 - 03:26 AM, said:

Unless I'm misunderstanding, you have the initial bounds of the array stored in numberOfStudents and numberOfCourses, so you check your student ID and input against those first. If it's in range, then you directly access the array at the ID provided.

numberOfStudents and numberOfCourses hold an int from 0 to n where n is the number of elements in the arrays. It looks like I'll have no choice but do simulated bounds checking every time. Thanks though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1