Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Yes, i know it's homework

Name: Anonymous 2005-11-18 22:25

But i still wanna ask you guys

here's the problem

/////////////////////////////////////////////////////////////////////
// Program 10                  POINTERS                  COSC 1436
/////////////////////////////////////////////////////////////////////

      Define a record structure to hold a set of test scores,
      number of tests, and the average score. The number of
      records are to be determined at runtime. Also the
      number of tests for each record is to be determined at
      runtime. Calculate the average test score for the set
      the set of test scores in each record. Display records.
      Demonstrate with a least five records.


This question must use pointers (couldn't do to class that day, car problem)
i want to ask, is there a way to create a array in the structure, with the size of the array to be defined later?
like

struct record
{
    int test[];
    int num_test;
    float average;
};

and ya, it didn't compile, where am i wrong?

Name: Anonymous 2005-11-19 9:29

>>1

My first impulse is to say >>3 ftw, but then I saw the title of your assignment was "pointers" and realized that your instructor doesn't want you to use the STL.  (Which is sad, but understandable if you don't know how to se pointers.)  Rather, you're being taught how to allocate and deallocate memory on the heap.

So, first of all, C or C++?  If you're using C, use malloc et al.  If you're using C++, use new et al.

Second, whether your instructor wants it or not, your record "structure" would probably be more useful, conceptually, as a class with intelligent construction and destruction semantics.

So:

(haven't tried to use code tags here before, see if the idiot that I am can get it right)

class TestScoreSet {
public:
   TestScore();
   ~TestScore();
   // Inspectors/Mutators, as appropriate

private:
   int* pScores;
   int  cntScores;
   double meanScore;
};

Your constructor or accessor methods should alloc up some memory on the heap and (if appropriate) free old memory.  Likewise, your destructor should free up any memory still alloc'd.  In your accessors, you'll also want to automatically re-calc your mean and score count so that it's always up to date.

Of course, I think that a more intelligent way to structure this would be to create a test score class or struct (note singular, not plural), create a container of that (like a vector), and wrap the container with this "set" class adding on the averaging logic.  Dangit, I still think >>3's on the money.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List