dynamic memory

R

Richard

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_scores, number_of_student;


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

cin >>student_record;

student_info student[student_record];

student_record = new int[number_of_student];

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

cin >>number_test_scores;

}
 
E

Eric Pruneau

Richard said:
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;

};

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...


// function prototypes passing structure into function





void main()

{


int student_record=0, number_test_scores, number_of_student;


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

cin >>student_record;

student_info student[student_record];

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]
student_record = new int[number_of_student];

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.
cout<<"How many test scores do you want to calculate average for this
student? : ";

cin >>number_test_scores;

}

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.test_scores = new float[number_test_scores];

now you have a memory allocated dynamically for each student.

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


Eric
 
R

roberts.noah

Richard said:
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

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.scores.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

dynamic memory 1
dynamic memory allocation 3
Dynamic programming 3
sorting 1
what am i doing wrong here?help 2
HELP PLEASE 4
Neel Help 3
Help in fixing this code 8

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,214
Latest member
JFrancisDavis

Latest Threads

Top