# Dynamic Array Problem

• 03-03-2003
Dynamic Array Problem
Hello,

I am attempting to create a small gradebook application for a project. I am using dynamic 1-d arrays as well as one dynamic 2-d array. I'll try not to post too much code, but rather post the scope of the problem. The code (left out many parts of it):

Code:

```void main() {         apstring *grade_types, *last_names, name;         double *averages, *grade_weights;         int **grades, *ids, num_grades, period, size;         name = get_apstring("class name");         period = get_integer("class period");         size = get_integer("class size");                 averages = new double[size];         ids = new int[size];         last_names = new apstring[size];         for (int i = 0; i < size; ++i)         {                 cout << "Enter student # " << i + 1 << "'s last name." << endl;                 cin >> last_names[i];                 cout << "Enter " << last_names[i] << "'s ID number." << endl;                 cin >> ids[i];         }         num_grades = get_integer("number of grade categories");         grades = new int*[size];         for (i = 0; i < size; ++i) {                 grades[i] = new int[num_grades];                 for (int j = 0; j < size; ++j)                 {                         grades[i][j] = 0;                 }         }         grade_types = new apstring[num_grades];         grade_weights = new double[num_grades];         for (i = 0; i < num_grades; ++i)         {                 cout << "Enter grade type #" << i + 1 << "." << endl;                 cin >> grade_types[i];                 cout << "Enter weight for " << grade_types[i] << "." << endl;                 cin >> grade_weights[i];         }         for (i = 0; i < size; ++i)         {                 for (int j = 0; j < num_grades; ++j)                 {                         cout << "Enter a grade." << endl;                         cin >> grades[i][j];                         averages[i] += grades[i][j] * grade_weights[j];                 }         }         for (i = 0; i < size; ++i)         {                 cout << last_names[i] << setw(15 - last_names[i].length());                 cout << ids[i] << " ";                 for (int j = 0; j < num_grades; ++j)                 {                         cout << grades[i][j] << " ";                 }                 cout << averages[i] << " "; [Continues...]```
I have no syntax errors, but one logic error that I cannot seem to fix. For some reason the following code does not work the way it's supposed to:

Code:

`averages[i] += grades[i][j] * grade_weights[j];`
Which causes this code:

Code:

`cout << averages[i] << " ";`
...to print a strange float number (like 1.6245... or something like that). I'm thinking that averages[i] may be undeclared. But how could it be if I already declared it before?

Any assistance is appreciated.
• 03-03-2003