Thread: Dynamic Array Problem

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    6

    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.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    6
    nm fixed it myself...

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    Just curious?
    Was the problem in casting your variables?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 01:51 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Dynamic array problem
    By new here in forum C++ Programming
    Replies: 9
    Last Post: 03-12-2003, 06:39 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Dynamic array problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2002, 09:55 AM