dynamic memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard

    #1

    dynamic memory

    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. I do not know how to dynamically
    allocate the number of record and the number of test scores. I can anyone
    show me this first step. The rest of the program should be easy for me.
    Thanks

    Here is my code for dynamcally allocated number of records and test scores
    in a structure.

    # include <iostream>

    # include <string>

    using namespace std;

    // structure declaration

    struct student_info

    {

    char name[20];

    int test_scores;

    float average_ts;

    };

    // function prototypes passing structure into function





    void main()

    {


    int student_record= 0, number_test_sco res, number_of_stude nt;


    cout<<"How many student do you want to average test scores? : ";

    cin >>student_recor d;

    student_info student[student_record];

    student_record = new int[number_of_stude nt];

    cout<<"How many test scores do you want to calculate average for this
    student? : ";

    cin >>number_test_s cores;

    }


  • Eric  Pruneau

    #2
    Re: dynamic memory


    "Richard" <nomail1235@yah oo.com> a écrit dans le message de news:
    xPKdna5seaUaKhb enZ2dnUVZ_sWdnZ 2d@comcast.com...[color=blue]
    > 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. I do not know how to dynamically
    > allocate the number of record and the number of test scores. I can anyone
    > show me this first step. The rest of the program should be easy for me.
    > Thanks
    >
    > Here is my code for dynamcally allocated number of records and test scores
    > in a structure.
    >
    > # include <iostream>
    >
    > # include <string>
    >
    > using namespace std;
    >
    > // structure declaration
    >
    > struct student_info
    >
    > {
    >
    > char name[20];
    >
    > int test_scores;
    >
    > float average_ts;
    >
    > };
    >[/color]

    You should have an array of test_scores. Your struct should look like

    struc student_info
    {
    char name[20]; // Personnaly I think you should use a std::string
    float* test_scores; // memory will be allocated dynamically
    float average_ts;
    }

    Note, I changed test scores type to be float instead of int...


    [color=blue]
    > // function prototypes passing structure into function
    >
    >
    >
    >
    >
    > void main()
    >
    > {
    >
    >
    > int student_record= 0, number_test_sco res, number_of_stude nt;
    >
    >
    > cout<<"How many student do you want to average test scores? : ";
    >
    > cin >>student_recor d;
    >
    > student_info student[student_record];[/color]

    This is illegal... student_record must be a compile time constant for this
    line to work.
    you should allocate with new.

    student_info* student = new student[student_record];

    now you have a "dynamic array" of student_info. it can be accessed this way:
    student[0], student[1], ..., student[student_record-1]
    [color=blue]
    > student_record = new int[number_of_stude nt];[/color]

    I dont understand what you wanna do with this line but it is obvioulsy
    wrong. delete that.
    student_record is a plain int, not a pointer.
    [color=blue]
    > cout<<"How many test scores do you want to calculate average for this
    > student? : ";
    >
    > cin >>number_test_s cores;
    >
    > }[/color]

    Ok now that you have the number of test score, you have to allocate memory
    for each student.
    for(unsigned i=0; i < student_record; ++i)
    student[i].test_scores = new float[number_test_sco res];

    now you have a memory allocated dynamically for each student.

    dont forget to delete all this memory when you are done.


    Eric



    Comment

    • roberts.noah@gmail.com

      #3
      Re: dynamic memory


      Richard wrote:[color=blue]
      > 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. I do not know how to dynamically
      > allocate the number of record and the number of test scores. I can anyone
      > show me this first step. The rest of the program should be easy for me.
      > Thanks
      >[/color]

      Eric gave a good answer. Here is one with a bit of a different spin.

      I don't see any requiremnt for dynamic memory management on _your_
      part. Why not use the tools at your disposal? The quoted text of the
      assignment doesn't seem to require your own memory management, it just
      says do this and leaves you with the task of finding a way - which will
      require memory management on *somebody's* part...

      #include <vector>
      #include <string>

      using namespace std;

      struct student_info
      {
      string name;
      vector<int> scores;
      float avg;
      };

      Now, interestingly enough there is a data item missing in your version
      that isn't in the above: the count of the scores (note the requirement
      above). Since vector provides .size() it is keeping this information
      for you - isn't that nice ;)

      now you can do:

      vector<student_ info> student_records ;

      then you can...
      student_info temp;
      cin >> temp.name;
      for (...) { int test; cin >> test; student_info.sc ores.push_back( test);
      /* may as well calc avg here too */ }
      student_records .push_back(temp );

      Now, this is definately how I would go about solving the problem *as
      given by you*. You have to decide which fits your teacher's
      requirements more and use it. Good luck.

      Comment

      Working...